Alien Dictionary(topo)
There is a new alien language which uses the latin alphabet. However, the order among letters are unknown to you. You receive a list ofnon-emptywords from the dictionary, where words aresorted lexicographically by the rules of this new language. Derive the order of letters in this language.
Example
Given the following words in dictionary,
[
"wrt",
"wrf",
"er",
"ett",
"rftt"
]The correct order is:"wertf"
Given the following words in dictionary,
[
"z",
"x"
]The correct order is:"zx".
Notice
You may assume all letters are in lowercase.
You may assume that if a is a prefix of b, then a must appear before b in the given dictionary.
If the order is invalid, return an empty string.
There may be multiple valid order of letters,
return the smallest in lexicographical order
Input test data
(
one parameter per line
)
分析
相邻2个word比较,选长度短的for,比完就break
字典序,要用priority queue
初始化入度和图,防止漏初始点
Last updated
Was this helpful?