#P6491. Determining the Alien Dictionary Order
Determining the Alien Dictionary Order
Determining the Alien Dictionary Order
You are given a list of n words that are sorted lexicographically according to an unknown alphabet order (often called an alien dictionary). Your task is to determine the order of the letters in this unknown alphabet.
To do this, compare each pair of consecutive words and find the first position where they differ. This difference gives you a relation between two letters. Combining all such relations yields the complete ordering of all distinct characters appearing in the list. If the ordering is ambiguous, any valid ordering will be accepted. You can assume that the input is such that a valid ordering exists (i.e. no cycles occur).
Note: If two words are identical up to the length of the shorter word, then the shorter word is considered to come before the longer word.
inputFormat
The input consists of multiple lines. The first line contains an integer n (n ≥ 1) denoting the number of words. The following n lines each contain a word. All words consist of lowercase English letters.
outputFormat
Output a single line containing a string that represents the order of the letters in the alien dictionary.
sample
5
wrt
wrf
er
ett
rftt
wertf