#C7937. Alien Dictionary Order Verification

    ID: 51863 Type: Default 1000ms 256MiB

Alien Dictionary Order Verification

Alien Dictionary Order Verification

You are given a dictionary of words written in an alien language and a string representing the order of the alien alphabet. Your task is to determine whether the words are sorted lexicographically as per the alien language order.

Formally, you are provided with a string \(O\) consisting of 26 distinct lowercase letters which represents the order of characters in the alien language. You are also given a list of \(n\) words. The dictionary is considered sorted if for every consecutive pair of words \(w_i\) and \(w_{i+1}\), either:

  • There exists an index \(j\) such that \(w_i[j] \neq w_{i+1}[j]\) and the order of \(w_i[j]\) in \(O\) is less than the order of \(w_{i+1}[j]\); or
  • \(w_i\) is a prefix of \(w_{i+1}\) (i.e. \(|w_i| \leq |w_{i+1}|\)).

Output YES if the dictionary is sorted, and NO otherwise.

Note: If the list of words is empty or contains a single word, consider it sorted.

\(\textbf{Example:}\)

Input:
abcdefghijklmnopqrstuvwxyz
3
apple
app
banana

Output: NO

</p>

inputFormat

The input is read from stdin and has the following format:

  1. The first line contains a string order which is a permutation of the 26 lowercase English letters representing the alien alphabet order.
  2. The second line contains an integer n, the number of words in the dictionary.
  3. The following n lines each contain one word composed of lowercase letters.

outputFormat

Output a single line to stdout containing either YES if the given list of words is sorted according to the alien dictionary order, or NO otherwise.

## sample
abcdefghijklmnopqrstuvwxyz
3
apple
app
banana
NO