#P15725. [JAG 2023 Summer Camp #3] LCP Queries

[JAG 2023 Summer Camp #3] LCP Queries

题目描述

A string xx is called a prefix of a string yy if xx can be obtained by repeating the removal of the last letter of yy zero or more times. For example, “abac”, “aba”, “ab”, “a”, and an empty string are the prefixes of “abac”.

For two strings xx and yy, let LCP(x,y)\text{LCP}(x, y) be the length of the longest common prefix of xx and yy. For example, LCP(“abacab”,“abacbba”)=4\text{LCP}(\text{“abacab”}, \text{“abacbba”}) = 4 because the longest common prefix of these two strings is “abac”. Note that LCP(x,y)\text{LCP}(x, y) is always defined for any strings xx and yy because at least an empty string is one of their common prefixes.

You are given nn strings s1,,sns_1, \ldots, s_n and mm strings t1,,tmt_1, \ldots, t_m of lowercase English letters. Then, you are given qq queries. In each query you are given an integer sequence a1,,aka_1, \ldots, a_k. Let uu be the concatenation of ta1,,takt_{a_1}, \ldots, t_{a_k}. Your task is to calculate i=1nLCP(u,si)\sum_{i=1}^{n} \text{LCP}(u, s_i).

输入格式

The input consists of a single test case of the following format.

$$\begin{aligned} &n \\ &s_1 \\ &\vdots \\ &s_n \\ &m \\ &t_1 \\ &\vdots \\ &t_m \\ &q \\ &\text{Query}_1 \\ &\vdots \\ &\text{Query}_q \end{aligned} $$

The first line consists of an integer nn. Each of the next nn lines consists of a non-empty string sis_i of lowercase English letters. The next line consists of an integer mm. Each of the next mm lines consists of a non-empty string tjt_j of lowercase English letters.

The next line consists of an integer qq. Then qq queries are given in order. Each of the queries is given in a single line in the following format.

k a1  akk \ a_1 \ \ldots \ a_k

kk is a positive integer which represents the length of the integer sequence of this query. Each aia_i is an integer between 11 and mm, inclusive.

You can assume that 1n200,0001 \leq n \leq 200,000, 1m200,0001 \leq m \leq 200,000 and 1q200,0001 \leq q \leq 200,000. The sum of lengths of sis_i does not exceed 200,000200,000. Similarly, the sum of lengths of tit_i does not exceed 200,000200,000. The sum of kk over all queries does not exceed 200,000200,000.

输出格式

Output qq lines. The ii-th line should be the answer to the ii-th query.

5
abcde
aaa
a
ab
bcd
5
a
bc
de
aaaa
b
5
1 1
3 1 2 3
2 2 3
5 5 4 3 2 1
3 3 3 3
4
9
3
1
0