#P8406. [COCI 2021/2022 #6] Palindromi
[COCI 2021/2022 #6] Palindromi
Problem Description
You are given a sequence of length , indexed by . Initially, each character represents a string of length . In one concatenation operation, you need to choose two strings and , delete them, and replace them with the string , i.e., write all characters of first and then write all characters of .
These initial strings need to be merged into one string using concatenation operations. The pair of strings chosen in the -th operation is described by an index pair , meaning that the two strings to be concatenated are the one containing the character with index and the one containing the character with index . It is guaranteed that the characters with indices and are not in the same string.
The palindrome value of some string is defined as the number of distinct palindromic substrings in . We define a palindrome as a string that reads the same from left to right and from right to left. A substring of a string is defined as a string that can be obtained by deleting one or more characters from the beginning and/or the end of the string.
For each concatenation operation, output the palindrome value of the resulting string.
Input Format
The first line contains an integer , representing the number of characters.
The second line contains a string of length , representing the initial strings.
The next lines each contain two integers , , representing the -th concatenation operation.
Output Format
Output lines, where the -th line is the palindrome value of the string obtained after the -th concatenation operation.
3
010
1 2
2 3
2
3
5
00111
4 1
1 5
2 1
3 1
2
3
4
5
8
10010000
7 5
4 2
3 6
1 3
6 8
5 3
1 2
2
2
2
3
4
6
8
Hint
Sample Explanation 3:
After each concatenation, the newly created strings are: and .
Their palindrome values are given in the sample output. For example, the palindrome value of is , because the string contains palindromic substrings: and .
Constraints:
For of the testdata: .
For of the testdata: .
For of the testdata: .
For of the testdata: , .
The scoring for this problem is the same as COCI 2021-2022#6, with a full score of points.
Translated by ChatGPT 5