Editorial for COCI '12 Contest 4 #2 Esej


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.

Let us describe the procedure of determining if a word is nice or not. We iterate along the word from left to right and keep the array of letters which we have not been able to pair yet. Assume we have encountered a letter A. We will check if the last unpaired letter is A: if it is, we can pair it with the new letter and remove it from the array of unpaired letters. If the last unpaired letter is B, we must add the new letter A to the array of unpaired letters (we cannot pair it with some previous A because if we try to connect the B afterwards, its arch will intersect with the arch from letter A).

Notice that the array of unpaired letters we keep is a stack: a structure which enables adding elements to the end, having access to the last element and removing the last element. If the stack is empty after we are done iterating, the word is nice.


Comments

There are no comments at the moment.