Editorial for COCI '14 Contest 2 #2 Utrka


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.

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 \mathcal O(N^2 \times \text{name_length}) and was sufficient for 50\% 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 cnt[index][letter] denote the number of occurrences of the letter letter on position index. 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 0, make up the final solution. Filling out this matrix of dimensions 27 \times 21 can be done in \mathcal O(N \times \text{name_length}) 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 xor. More specifically, the following properties are essential:

  1. a \mathbin{xor} b = b \mathbin{xor} a
  2. a \mathbin{xor} a = 0
  3. a \mathbin{xor} 0 = a

Combining these properties, it is easy to deduce that it is sufficient, for every index, output xor of all letters from the input that occur in that index. We leave the formal proof for the reader to practise.


Comments

There are no comments at the moment.