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.
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:
- If a string is a prefix of string , and prefix of string , then string is a prefix of string .
- If a string is a suffix of string , then string is a prefix of string , where denotes string written backwards.
Now it is sufficient to build a prefix tree in a slightly modified form:
- The alphabet doesn't consist of letters, but , because each pair of letters represents a single "letter".
- When inserting a word in the tree, the first letter in the pair is taken from the beginning, the other from the end.
- Each node stores the best solution for that "prefix" ().
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 .
Comments