LCP Queries
Time limit2.5sMemory limit1024 MB
Given strings s_i and t_j, each query concatenates the listed t_j and asks for the sum of its LCP with every s_i.
- Level
Medium7 of 10
- Topics
- Trie, String matching, Hash map
- Solved
- No attempts yet
Statement
A string is called a prefix of a string if can be obtained by repeatedly removing the last letter of zero or more times. For example, abac, aba, ab, a, and the empty string are the prefixes of abac.
For two strings and , let be the length of their longest common prefix. For example, the longest common prefix of abacab and abacbba is abac, so of the two is . Every pair of strings has the empty string as a common prefix, so is always defined.
You are given strings and strings , all made of lowercase English letters. Then you are given queries. Each query is an integer sequence . Let be the concatenation of . For each query, compute .
Input
The input is a single test case in the following format.
The first line has an integer . Each of the next lines has a non-empty string . The next line has an integer . Each of the next lines has a non-empty string . The next line has an integer . Then queries follow, one per line, in this format.
is a positive integer, the length of the integer sequence. Each is an integer between and , inclusive.
The constraints are , , and . The sum of lengths of the is at most . The sum of lengths of the is at most . The sum of over all queries is at most .
Output
Output lines. The -th line contains the answer to the -th query.