Editorial for COCI '14 Contest 2 #2 Utrka
Submitting an official solution before solving the problem yourself is a bannable offence.
A naive approach to solving this problem would be the following algorithm:
For each contestant from the list
if the contestant's name is on the ranking list
mark contestant
cross out contestant from the ranking list
Output unmarked contestant
This implementation has a time complexity of and was sufficient for of total points.
To win all points, it was necessary to notice that the name of the contestant that didn't manage to finish the race is going to occur an odd number of times in the input, whereas the names of all the other contestants are going to occur an even number of times.
If we think this through, we will see that an analogous claim holds for every letter in the name of the wanted contestant. For example, if we observe the number of occurrences of initial letters of all the contestants, we will see that all the letters except the initial one of the wanted contestant occur an even number of times. This claim leads us to the following idea:
Let denote the number of occurrences of the letter on position . Then it is sufficient that, after we have filled out the matrix, find which letter occurs an odd number of times for each index. Those letters, starting with index , make up the final solution. Filling out this matrix of dimensions can be done in which is fast enough for limitations from the task.
In the style of the main character of the sixth task – "Can it be pimped up? Of course it can!" Let us recall some properties of the binary operation . More specifically, the following properties are essential:
Combining these properties, it is easy to deduce that it is sufficient, for every index, output of all letters from the input that occur in that index. We leave the formal proof for the reader to practise.
Comments