Editorial for COCI '19 Contest 4 #5 Nivelle
Submitting an official solution before solving the problem yourself is a bannable offence.
We can implement a solution of time complexity 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 . 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 values to compare and pick the smallest one.
A simple implementation calculates for each starting character the longest substring which contains exactly 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 strings which go to the next new character.
Comments