Editorial for COCI '12 Contest 2 #2 Krizaljka


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.

We can use a triple-nested for loop to choose crossword rows in all possible ways. Then we arrange them into a 3 by 3 matrix. The last remaining step is checking whether the crossword columns match the remaining three words. The simplest method for this is finding the three strings corresponding to the matrix columns, sorting them lexicographically and comparing with the remaining three strings from the input (which are already sorted lexicographically).

There is another reason that makes the sorted order of the given words convenient: as soon as we find a matching crossword, we can immediately stop the search knowing that we have found the solution – provided that the outermost for loop chooses the first word index in order from 1 to 6, the middle one chooses the second word in the same order, and the innermost one chooses the third word, again in the same order, it is easy to see that the first matching crossword that we find will also be the first one lexicographically.


Comments

There are no comments at the moment.