#P16693. Tokitsukaze and Palindrome Border
Tokitsukaze and Palindrome Border
Background

However, for some reasons, this problem did not appear in Codeforces Round 789.
Problem Description
Given a string . Define:
- as the prefix substring of with length .
- as the suffix substring of with length .
- as the length of the string .
For any two strings and , define the function as:
$$f(s, t) = \sum_{len=1}^{\min(|s|, |t|)} \operatorname{val}(len)$$Here, is computed as follows:
$$\operatorname{val}(len) = \begin{cases} len, & \text{if } \operatorname{pre}(s, len) = \operatorname{suf}(t, len) \text{ and } \operatorname{pre}(s, len) \text{ is a palindrome} \\ 0, & \text{otherwise} \end{cases}$$Note: A palindrome is a string that reads the same forward and backward, such as a, aa, or aba.
Now Tokitsukaze has strings . At the same time, she asks queries.
Each query gives a set containing positive integers, representing the indices of disabled strings. For each query, compute:
Input Format
The first line contains an integer (), denoting the number of strings.
The next lines each contain a string consisting of lowercase letters ().
The next line contains a positive integer (), denoting the number of queries.
The next lines each describe a query with integers. The first integer is (), followed by pairwise distinct positive integers (), representing the set of disabled indices.
It is guaranteed that and do not exceed .
Output Format
For each query, output one line containing one integer representing the answer.
2
a
aaa
4
0
1 1
1 2
2 1 2
9
6
1
0
3
a
aa
aaba
3
0
2 1 3
1 1
13
3
8
Hint
Explanation for Sample 1:
The answer to the first query is $f(s_1, s_1) + f(s_1, s_2) + f(s_2, s_1) + f(s_2, s_2) = 9$.
The answer to the second query is .
The answer to the third query is .
For the fourth query, since all indices are disabled, the answer is .
Translated by ChatGPT 5