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

  1. You may assume all letters are in lowercase.

  2. You may assume that if a is a prefix of b, then a must appear before b in the given dictionary.

  3. If the order is invalid, return an empty string.

  4. 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?