Editorial for COCI '15 Contest 2 #4 Savez


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.

In order to solve this task, two things need to be noticed:

  1. If a string x_i is a prefix of string x_j, and x_j prefix of string x_k, then string x_i is a prefix of string x_k (i < j < k).
  2. If a string x is a suffix of string y, then string x^R is a prefix of string y^R, where a^R denotes string a written backwards.

Now it is sufficient to build a prefix tree in a slightly modified form:

  1. The alphabet doesn't consist of 26 letters, but 26^2, because each pair of letters represents a single "letter".
  2. When inserting a word in the tree, the first letter in the pair is taken from the beginning, the other from the end.
  3. Each node stores the best solution for that "prefix" (1+\max(\text{node_children})).

If we start from the end of input data, we've changed the task from expanding to contracting one of the existing strings. By doing this, we ensure that the solution takes into account the order of words from the input data. The time complexity of this solution is \mathcal O(\text{total_length_of_words}).


Comments

There are no comments at the moment.