AcWing 1282. 通过新建st数组,保证O(n)复杂度,防止之前模板退化成O(n^2)
原题链接
中等
作者:
Astephen
,
2021-07-19 21:01:10
,
所有人可见
,
阅读 207
#include <bits/stdc++.h>
using namespace std;
const int N = 10010, M = 1000010, S = 55;
int tr[N * S][26], cnt[N * S], idx;
char str[M];
int q[N * S], ne[N * S];
bool st[N * S];
int hh, tt;
void insert() {
int node = 0;
for (int i = 0; str[i]; i++) {
int j = str[i] - 'a';
if (!tr[node][j]) tr[node][j] = ++idx;
node = tr[node][j];
}
cnt[node]++;
}
void build() {
hh = 0, tt = -1;
for (int i = 0; i < 26; i++)
if (tr[0][i]) q[++tt] = tr[0][i];
while(hh <= tt) {
int t = q[hh ++];
for (int i = 0; i < 26; i++) {
int c = tr[t][i];
if (!c) tr[t][i] = tr[ne[t]][i];
else {
ne[c] = tr[ne[t]][i];
q[++tt] = c;
}
}
}
}
int main() {
int T;
scanf("%d", &T);
while(T--) {
memset(ne, 0, sizeof ne);
memset(tr, 0, sizeof tr);
memset(cnt, 0, sizeof cnt);
memset(st, 0, sizeof st);
idx = 0;
int n;
scanf("%d", &n);
while(n--) {
scanf("%s", str);
insert();
}
build();
scanf("%s", str);
for (int i = 0, j = 0; str[i]; i++) {
int t = str[i] - 'a';
j = tr[j][t];
st[j] = 1;
}
for (int i = tt; i >= 0; i--)
if (st[q[i]])
st[ne[q[i]]] = 1;
int res = 0;
for (int i = tt; i>= 0; i--) {
if (st[q[i]]) res += cnt[q[i]];
}
printf("%d\n", res);
}
}