Editorial for COCI '12 Contest 3 #3 Malcolm


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 solution that, for each string, iterates over the previous K strings and counts the strings with the same number of letters, is too slow.

A faster solution reads a string, counts its letters – let us denote the number of letters with L – and then immediately, without another for loop, answers the question: how many strings, out of the previous K, have exactly L letters?

In order to find that number quickly, we need to keep an auxiliary array with the corresponding count for each L. This array must, of course, be maintained: upon reading a new string with length L, we increment the L^\text{th} element of the auxiliary array by one, and decrement the (L')^\text{th} element by one, where L' is the length of the string "falling out" of the last K strings interval, i.e. not included in the set of friends of upcoming strings anymore.

This solution is actually based on the sweep-line principle: the imaginary scanner is scanning through the rankings list and processing events such as new name added and name removed from friends set.


Comments

There are no comments at the moment.