Editorial for COCI '10 Contest 6 #4 Abeceda


Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.
Submitting an official solution before solving the problem yourself is a bannable offence.

Consider every pair of consecutive words such that neither word is a prefix of the other. Let k be the first position at which the words differ. Let a be the k^\text{th} letter of the word that appears later in the input, and b the k^\text{th} letter of the other word. It follows that a comes after b in alphabetical order.

Let's define the binary relation greater than on the given set of letters. We'll say that a is greater than b if a comes after b in alphabetical order. Notice that this relation is transitive (if a > b and b > c, then a > c). We can easily compute its transitive closure using the Floyd-Warshall algorithm.

If the transitive closure suggests that a > a for some letter a, the ordering does not exist.

Next, if there are two letters a and b such that neither a > b nor b > a holds, the ordering is not unique.

Otherwise, the ordering does exist and it is unique. Let the number k be equal to the number of letters b such that a > b for some letter a. The letter a will take the k^\text{th} place in the sorted sequence of all letters in the alphabet, indexed from zero.


Comments

There are no comments at the moment.