Editorial for COCI '12 Contest 3 #5 Herkabe
Submitting an official solution before solving the problem yourself is a bannable offence.
First solution
From the given words we can build a trie, also known as a prefix tree. Imagine that we are in the root of the tree and can see
If we have determined, for the
The prefix tree must be implemented carefully in order to be fast enough and not use up too much memory.
Second solution
Based on a similar idea, but simpler to implement (without prefix trees). We sort the words alphabetically. Next, we look at the first letter of all words and find
In the recursion, we need to find subblocks for each of the blocks. However, now we can ignore the first letter, since we know it is equal for all words in a block, so we can consider only the second letter. Analogously, in the deeper levels of recursion, we only need to consider the letters in positions corresponding to the current level. We conclude that the total complexity of the recursion is proportional to the total number of letters. Of course, the recursion here will be parameterized by the lower and upper bound of the current block and the index of the letter we need to observe.
Comments