#P3065. Bessie's Lexicographical Order
Bessie's Lexicographical Order
Bessie's Lexicographical Order
Bessie has been playing with strings again. She discovered that by rearranging the alphabet she can influence the lexicographic order of a set of strings. In this problem, you are given a list of strings. Your task is to determine which strings can be made the lexicographically smallest by choosing an appropriate permutation of the alphabet.
To compare two strings X and Y under a custom alphabet order, find the smallest index i such that Xi and Yi differ. If such an index exists, then X is considered smaller than Y if the letter Xi appears earlier in the rearranged alphabet than Yi. If no such index exists, then the shorter string is considered lexicographically smaller.
For example, consider the strings:
- omm
- moo
- mom
- ommnom
Bessie observed that using the standard alphabet order, "mom" can be made the smallest, and by using the alphabet order "abcdefghijklonmpqrstuvwxyz", "omm" becomes the smallest. In contrast, no reordering can make "moo" or "ommnom" the first string.
Your goal is to output all strings from the input that can be made lexicographically smallest under some permutation of the alphabet.
Note: If one string is a prefix of another (and they are not equal), the shorter string is always lexicographically smaller regardless of the alphabet order.
inputFormat
The input begins with a positive integer n (n ≥ 2), representing the number of strings. Each of the following n lines contains a non-empty string consisting of lowercase letters.
outputFormat
Output each string that can be made the lexicographically smallest by some rearrangement of the alphabet. The strings should be printed in the same order as they appear in the input, each on a separate line.
sample
4
omm
moo
mom
ommnom
mom
omm
</p>