首页 > 题解 > bzoj 3012 [Usaco2012 Dec]First!

bzoj 3012 [Usaco2012 Dec]First!

Description

Bessie has been playing with strings again. She found that by changing the order of the alphabet she could make some strings come before all the others lexicographically (dictionary ordering). For instance Bessie found that for the strings “omm”, “moo”, “mom”, and “ommnom” she could make “mom” appear first using the standard alphabet and that she could make “omm” appear first using the alphabet “abcdefghijklonmpqrstuvwxyz”. However, Bessie couldn’t figure out any way to make “moo” or “ommnom” appear first. Help Bessie by computing which strings in the input could be lexicographically first by rearranging the order of the alphabet. To compute if string X is lexicographically before string Y find the index of the first character in which they differ, j. If no such index exists then X is lexicographically before Y if X is shorter than Y. Otherwise X is lexicographically before Y if X[j] occurs earlier in the alphabet than Y[j].

给定n个总长不超过m的互不相同的字符串,现在你可以任意指定字符之间的大小关系。问有多少个串可能成为字典
序最小的串,并输出这些串。n <= 30,000 , m <= 300,000

Input

  • Line 1: A single line containing N (1 <= N <= 30,000), the number of strings Bessie is playing with.
  • Lines 2..1+N: Each line contains a non-empty string. The total number of characters in all strings will be no more than 300,000. All characters in input will be lowercase characters ‘a’ through ‘z’. Input will contain no duplicate strings.

Output

  • Line 1: A single line containing K, the number of strings that could be lexicographically first.

  • Lines 2..1+K: The (1+i)th line should contain the ith string that could be lexicographically first. Strings should be output in the same order they were given in the input.

Sample Input

4

omm

moo

mom

ommnom

INPUT DETAILS: The example from the problem statement.

Sample Output

2

omm

mom

OUTPUT DETAILS: Only “omm” and “mom” can be ordered first.

题解

首先的话,假如有个串的前缀也出现在这些串里,那么它肯定不可以是最小的。写个Trie树判下。

既然写了Trie树,那么就在上面搞搞事情。发现每个串到根的这个链上所有点的兄弟(同父亲)所在的串都不能比它大。那就把所有的关系用个图存下,Topsort一下,看看逻辑是否满足就ok了。


如果你觉的这篇文章不错,分享给朋友吧!

打开微信“扫一扫”,打开网页后点击屏幕右上角分享按钮

×