Editorial for COCI '10 Contest 6 #4 Abeceda
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 be the first position at which the words differ. Let
be the
letter of the word that appears later in the input, and
the
letter of the other word. It follows that
comes after
in alphabetical order.
Let's define the binary relation greater than on the given set of letters. We'll say that is greater than
if
comes after
in alphabetical order. Notice that this relation is transitive (if
and
, then
). We can easily compute its transitive closure using the Floyd-Warshall algorithm.
If the transitive closure suggests that for some letter
, the ordering does not exist.
Next, if there are two letters and
such that neither
nor
holds, the ordering is not unique.
Otherwise, the ordering does exist and it is unique. Let the number be equal to the number of letters
such that
for some letter
. The letter
will take the
place in the sorted sequence of all letters in the alphabet, indexed from zero.
Comments