#P16693. Tokitsukaze and Palindrome Border

    ID: 19027 远端评测题 3000ms 512MiB 尝试: 0 已通过: 0 显示难度省选/NOI− 上传者: 标签>线段树树链剖分字典树 Trie回文自动机 PAMManacher 算法

Tokitsukaze and Palindrome Border

Background

However, for some reasons, this problem did not appear in Codeforces Round 789.

Problem Description

Given a string ss. Define:

  • pre⁡(s,len)\operatorname{pre}(s, len) as the prefix substring of ss with length lenlen.
  • suf⁡(s,len)\operatorname{suf}(s, len) as the suffix substring of ss with length lenlen.
  • ∣s∣|s| as the length of the string ss.

For any two strings ss and tt, define the function f(s,t)f(s, t) as:

$$f(s, t) = \sum_{len=1}^{\min(|s|, |t|)} \operatorname{val}(len)$$

Here, val⁡(len)\operatorname{val}(len) 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 nn strings s1,s2,…,sns_1, s_2, \dots, s_n. At the same time, she asks qq queries.
Each query gives a set BB containing kk positive integers, representing the indices of disabled strings. For each query, compute:

∑i∉B∑j∉Bf(si,sj)\sum_{i \notin B} \sum_{j \notin B} f(s_i, s_j)

Input Format

The first line contains an integer nn (1≤n≤3⋅1051 \leq n \leq 3 \cdot 10^5), denoting the number of strings.

The next nn lines each contain a string ss consisting of lowercase letters (1≤∣s∣≤3⋅1051 \leq |s| \leq 3 \cdot 10^5).

The next line contains a positive integer qq (1≤q≤3⋅1051 \leq q \leq 3 \cdot 10^5), denoting the number of queries.

The next qq lines each describe a query with k+1k + 1 integers. The first integer is kk (0≤ki≤n0 \leq k_i \leq n), followed by kk pairwise distinct positive integers BjB_j (1≤Bj≤n1 \leq B_j \leq n), representing the set of disabled indices.

It is guaranteed that ∑∣s∣\sum |s| and ∑k\sum k do not exceed 3⋅1053 \cdot 10^5.

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:

  • f(s1,s1)=1f(s_1, s_1) = 1
  • f(s1,s2)=1f(s_1, s_2) = 1
  • f(s2,s1)=1f(s_2, s_1) = 1
  • f(s2,s2)=1+2+3=6f(s_2, s_2) = 1 + 2 + 3 = 6

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 f(s2,s2)=6f(s_2, s_2) = 6.

The answer to the third query is f(s1,s1)=1f(s_1, s_1) = 1.

For the fourth query, since all indices are disabled, the answer is 00.

Translated by ChatGPT 5