Editorial for COCI '19 Contest 4 #5 Nivelle


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 implement a solution of time complexity \mathcal O(N^2) which for every substring calculates the number of different letters in it. If we use the so-called sliding window technique, we need to keep track of how many times each letter appears and note each time when a new letter starts or stops appearing.

Note that the numerator of the expression we want to minimize, i.e. the number of different characters in a substring, can either be 1, 2, \dots, 26. Therefore, it is enough to fix its value in every possible way and determine the largest possible substring which has exactly that many different characters. Now we have 26 values to compare and pick the smallest one.

A simple implementation calculates for each starting character the longest substring which contains exactly K different characters. If we calculate for each character and each position the first next appearance of that character, for each starting character we can quickly check \le 26 strings which go to the next new character.


Comments

There are no comments at the moment.