Editorial for COI '15 #2 Palinilap
Submitting an official solution before solving the problem yourself is a bannable offence.
For the sake of simplicity, let's assume that we are only dealing with palindromes of odd length, and that the described solution is easily expanded to the general case. The center of a palindrome is the character in its middle, or the position of that character in the original sample. The first step of the solution is, for each position , to determine – the half length of the longest palindrome with its center at (the longest palindrome with its center in is ). Notice that the weight of the original sample is equal to the sum of all the numbers .
There are multiple efficient ways to calculate all numbers . One option is, for each position , to determine the value with binary search. In order for this approach to be efficient enough, we need to have a method that can quickly examine whether two arbitrary substrings of sample , for example and , are equal. This can be done using the so-called rolling hash function. (Curator's note: This website is in Croatian. Thanks, COCI.)
When we have calculated all the values , we know the current weight of the sample. In the second step of the solution, we examine how the weight changes if the character at position is converted to . After conversion, some palindromes disappear, and new ones appear. First, we calculate how many existing palindromes disappear.
Let's now consider only palindromes with its center at . Some of them disappear only if the converted character's position is from the interval , and if, for example, the position of conversion is from the interval , then exactly palindromes with centers in disappear. With the help of this fact, we can implement a sweep line algorithm that calculates in a single pass, for each position, the total number of palindromes that disappear if the character is converted at that position.
We still need to find the number of newly created palindromes for each possible conversion. Let's assume that, after the conversion at position into character , a new palindrome appears with its center in , where . Let . Therefore, the sample was not a palindrome before conversion, and now is. Since is now a palindrome, so is , but this sample hasn't been changed, so it was a palindrome before conversion. Hence, was the longest palindrome with its center in before conversion, so is equal to . We conclude that each new palindrome with its center in can appear so that either the character at position is converted to the character at position or vice versa. Therefore, similarly to how we calculated (binary search with hash function to compare strings), we can calculate the number of new palindromes that appear in each of the two cases. Now we have calculated everything we need in order to know how many palindromes appear for each position and each new character , and how many disappear if the character at position is converted to , so the task is solved.
Comments