~{→看不见LaTex格式的Dalao们请刷新本页Thanks♪(・ω・)ノ←}~
2019寒假集训题
#.String
题面
Bob has a dictionary with $N$ words in it.
Now there is a list of words in which the middle part of the word has continuous letters disappeared. The middle part does not include the first and last character.
We only know the prefix and suffix of each word, and the number of characters missing is uncertain, it could be $0$. But the prefix and suffix of each word can not overlap.
For each word in the list, Bob wants to determine which word is in the dictionary by prefix and suffix.
There are probably many answers. You just have to figure out how many words may be the answer.
简要翻译
给定$n$个串,有$m$次询问,每次询问给定串$a,b$,问$n$个串中有多少个能表示为$axb$,其中$x$为任意字符串。
Input
第一行给出一个整数$T$,代表数据的组数。
每一组数据包含$2$个整数$n$和$q$, 分别代表串的个数和询问个数。
以下$n$行每行包含一个字符串$W_i$。
以下$q$行每行包含两个串$a_i$串$b_i$。
保证所有的串都是小写字母。
Output
对于每组数据,输出包含$q$行,每行包含一个整数代表答案。
Sample Input
1
4 4
aba
cde
acdefa
cdef
a a
cd ef
ac a
ce f
Sample Output
2
1
1
0
Hint
$T\le 5$
$0< n,q\le 100,000$
$\sum S_i+P_i\le 500,000$
$\sum W_i\le 500,000$
条件等价于前缀为$a$,后缀为$b$,且长度$>=|a|+|b|$的个数。
在不考虑长度的情况下,将正串和反串分别按字典序编号,一个询问对应了编号的一个区间,用$Trie$树或排序+二分+$hash$求出区间,然后用主席树来求答案。
然后考虑减掉长度不足的,直接枚举长度就可以确定字符串。
时间复杂度为$O(slogs)$。
Code
1 |
|