#C2045. Lexicographic Order Checker with Custom Alphabet
Lexicographic Order Checker with Custom Alphabet
Lexicographic Order Checker with Custom Alphabet
You are given a custom alphabet consisting of 26 distinct lowercase letters and a list of words. Your task is to determine whether the list of words is sorted in lexicographical order according to the given custom alphabet.
More formally, let \(\sigma\) be the custom alphabet, where \(\sigma[i]\) denotes the character at position \(i\) (0-indexed). A list of words \(w_1, w_2, \dots, w_n\) is sorted if for every \(1 \leq i < n\), the word \(w_i\) is not greater than \(w_{i+1}\) in the custom lexicographical order. When comparing two words, they are compared character by character using the order defined by \(\sigma\). If one word is a prefix of the other, then the shorter word is considered to be smaller.
For example, if the custom alphabet is given as:
[ \sigma = "zyxwvutsrqponmlkjihgfedcba" ]
and the list of words is:
[ ["cba", "baa", "aaa"] ]
then the list is sorted and you should output YES
.
inputFormat
The input is read from the standard input.
The first line contains a string of exactly 26 lowercase letters representing the custom alphabet.
The second line contains an integer \(n\) representing the number of words.
The following \(n\) lines each contain a single word composed of lowercase letters.
outputFormat
Output a single line to the standard output containing YES
if the words are sorted in lexicographical order according to the given custom alphabet, and NO
otherwise.
zyxwvutsrqponmlkjihgfedcba
3
cba
baa
aaa
YES