#C12350. Alien Dictionary
Alien Dictionary
Alien Dictionary
In an alien language, the words in the dictionary are sorted lexicographically. However, the order of the characters in the alien language is unknown. Given a list of words sorted according to the alien language, your task is to determine the order of the characters.
Assume the set of characters is the union of all characters in the input. In case there is no valid ordering (i.e. a contradiction or a cyclic dependency exists), output an empty string.
The problem can be formulated as discovering a topological order of a directed graph constructed as follows:
For each adjacent pair of words, find the first position i where they differ. This implies that there is a directed edge from the character at position i in the first word to the character at position i in the second word. Formally, if the two words are w1 and w2, and if they differ at index i, then we have an edge:
Your task is to determine a valid character order (i.e. a topological ordering of the graph).
inputFormat
The input begins with an integer n (1 ≤ n ≤ 1000) denoting the number of words. Following this, there are n lines each containing a word. All words consist of lowercase English letters.
Example:
5 wrt wrf er ett rftt
outputFormat
Output a single line string representing the order of characters in the alien language. If no valid ordering exists, output an empty string.
## sample5
wrt
wrf
er
ett
rftt
wertf