QOJ.ac

QOJ

Límite de tiempo: 3.0 s Límite de memoria: 1024 MB Puntuación total: 100

#10799. 寻找公共前缀

Estadísticas

如果字符串 $x$ 可以通过删除字符串 $y$ 末尾的零个或多个字符得到,则称 $x$ 是 $y$ 的前缀。例如,字符串 “abac” 有以下前缀:“abac”、“aba”、“ab”、“a” 和空字符串。

对于两个字符串 $x$ 和 $y$,令 $\text{LCP}(x, y)$ 为 $x$ 和 $y$ 的最长公共前缀的长度。例如,$\text{LCP}(\text{abacab}, \text{abacbba}) = 4$,因为这两个字符串的最长公共前缀是 “abac”。注意,对于任意字符串 $x$ 和 $y$,$\text{LCP}(x, y)$ 总是有定义的,因为至少空字符串是它们的公共前缀之一。

给定 $n$ 个由小写英文字母组成的字符串 $s_1, \dots, s_n$ 和 $m$ 个由小写英文字母组成的字符串 $t_1, \dots, t_m$。随后有 $q$ 次询问。每次询问给定一个整数序列 $a_1, \dots, a_k$。令 $u$ 为 $t_{a_1}, \dots, t_{a_k}$ 的拼接结果。你的任务是计算 $\sum_{i=1}^n \text{LCP}(u, s_i)$。

输入格式

第一行包含一个整数 $n$ ($1 \le n \le 2 \cdot 10^5$)。接下来 $n$ 行,每行包含一个由小写英文字母组成的非空字符串 $s_i$。

下一行包含一个整数 $m$ ($1 \le m \le 2 \cdot 10^5$)。接下来 $m$ 行,每行包含一个由小写英文字母组成的非空字符串 $t_j$。

下一行包含一个整数 $q$,表示询问次数 ($1 \le q \le 2 \cdot 10^5$)。随后是 $q$ 次询问。每个询问占一行,格式为 “$k \ a_1 \ \dots \ a_k$” ($1 \le k \le 2 \cdot 10^5$; $1 \le a_i \le m$)。

所有 $s_i$ 的长度之和不超过 $2 \cdot 10^5$。同理,所有 $t_i$ 的长度之和不超过 $2 \cdot 10^5$。此外,所有询问中 $k$ 的总和不超过 $2 \cdot 10^5$。

输出格式

输出 $q$ 行。第 $i$ 行应包含一个整数:第 $i$ 次询问的答案。

样例

样例输入 1

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

样例输出 1

4
9
3
1
0

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.