Editorial for IOI '06 P1 - Deciphering the Mayan Writing
Submitting an official solution before solving the problem yourself is a bannable offence.
The contestant should note that any solution that actually tries to match every possible permutation of within is doomed to failure. Even with a small alphabet, the total number of permutations for is very big. An optimal solution should take into account the fact that we are looking for every possible permutation of , in other words, we don't care for the order of the characters.
OPTIMAL SOLUTION: The optimal solution uses a queue where it pushes consecutive characters of until an error condition is reached. An error condition is reached whenever:
- A character that does not appear in is in the queue.
- The number of times a character , that appears in , is in the queue exceeds the number of times that character appears in .
After an error condition is reached, characters are popped from the queue until the error condition is cleared. In order to check for an error condition, the optimal solution keeps two vectors of length equal to the alphabet, each entry corresponds to a character in the alphabet. These two vectors maintain the number of times each character appears in and the number of times each character is in the queue. Whenever you push or pop a character from the queue, the entry for that character must be checked in both vectors.
If at any time the length of the queue is equal to the length of without having an error condition, then you have found a match.
Since every character in is pushed and popped once into the queue, the complexity of the resulting algorithm is .
SOLUTION 2: Instead of keeping two vectors to check for an error condition, every time a character is pushed or popped from the queue check every character in the alphabet to catch an error condition. This solution yields a complexity and should be able to score 70 points.
SOLUTION 3: Starting from every position in , look for a match by checking the number of times each character appears in , if an error is found, stop an advance to the next position in . This solution can yield a worst case complexity of and scored between 50 and 65 points depending on the implementation.