3.3
字符串
3.3.2
Kmp
模板
void getnxt(char *p){
int m = strlen(p);
int i=0, j=-1;
nxt[0]=-1;
while(i<m){
if(j==-1||p[i]==p[j])nxt[++i]=++j;
else j=nxt[j];
}
}
bool kmp(char *p, char *s){
int n=strlen(s),m=strlen(p);
int i=0,j=0;
while(i<n){
if(j==-1||s[i]==p[j])i++,j++;
else j=nxt[j];
if(j==m)return true;
}
return false;
}
模板题
next
应用循环节。
ac自动机
模板题
#include <iostream>
#include <bits/stdc++.h>
#define For(i,x,y) for(int i=(x); i<=(y); i++)
#define fori(i,x,y) for(int i=(x); i<(y); i++)
#define rep(i,y,x) for(int i=(y); i>=(x); i--)
#define mst(x,a) memset(x,a,sizeof(x))
#define pb push_back
#define sz(a) (int)a.size()
#define ALL(x) x.begin(),x.end()
#define mp make_pair
#define fi first
#define se second
#define db double
#define endl '\n'
#define debug(a) cout << #a << ": " << a << endl
using namespace std;
typedef long long LL;
typedef long long ll;
typedef unsigned long long ULL;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int inf = 0x3f3f3f3f;
const int mod = 1e9+7;
typedef pair<int,int>pa;
typedef pair<ll,ll>pai;
typedef pair<db,db> pdd;
const db eps = 1e-8;
const db pi = acos(-1.0);
int read() {
int x = 0, f = 0; char ch = getchar();
while (!isdigit(ch)) f |= ch == '-', ch = getchar();
while (isdigit(ch)) x = 10 * x + ch - '0', ch = getchar();
return f ? -x : x;
}
template<typename T> void print(T x) {
if (x < 0) putchar('-'), x = -x;
if (x >= 10) print(x / 10);
putchar(x % 10 + '0');
}
template<typename T> void print(T x, char let) {
print(x), putchar(let);
}
const int maxn = 1e4+10;
const int maxm = 1e6+10;
int tr[maxn*55][26], cnt[maxn*55], nex[maxn*55], q[maxn*55];
int n, idx;
char str[maxm];
void insert(char* str){
int p = 0;
for(int i = 0;str[i]; i ++ ){
int t = str[i] - 'a';
if(!tr[p][t]) tr[p][t] = ++ idx;
p = tr[p][t];
}
cnt[p]++;
}
void build(){
int hh = 0, tt = -1;
fori(i,0,26)
if(tr[0][i]) q[++tt] = tr[0][i];
while(hh <= tt){
int t = q[hh ++ ];
fori(i, 0, 26){
int p = tr[t][i];
if(!p) tr[t][i] = tr[nex[t]][i];
else {
nex[p] = tr[nex[t]][i];
q[ ++ tt] = p;
}
}
}
}
void init(){
mst(tr,0);
mst(cnt,0);
mst(nex,0);
idx = 0;
n = read();
fori(i,0,n){
scanf("%s", str);
insert(str);
}
build();
}
void sol(){
init();
int res = 0;
scanf("%s", str);
for(int i = 0, j = 0; str[i]; i ++ ){
int t = str[i] - 'a';
j = tr[j][t];
int p = j;
while(p){
res += cnt[p];
cnt[p] = 0;
p = nex[p];
}
}
printf("%d\n", res);
}
int main()
{
#ifdef LOCAL
freopen("in.txt", "r", stdin);
#endif
int tt;
tt = read();
while(tt--)sol();
return 0;
}