题目链接:点击打开链接
题解:可以重复增加子串的AC自动机经典入门
#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
typedef long long ll;
const int mx = 1e5+10;
int n,m,root,tot;
int nxt[mx][26],fail[mx],key[mx],ans[1010];
int init(){
memset(nxt[tot],0,sizeof(nxt[tot]));
return tot++;
}
char str[mx*20],st[1010][110];
void insert(int x){
int len = strlen(st[x]);
int now = root;
for(int i = 0;i < len;i++){
int id = st[x][i]-'A';
if(!nxt[now][id]) nxt[now][id] = init();
now = nxt[now][id];
}
key[now] = x;
}
void Getfail(){
queue<int> skt;
skt.push(root);
while(!skt.empty()){
int temp = skt.front();
skt.pop();
for(int i = 0;i < 26;i++){
int son = nxt[temp][i];
if(son){
if(root == temp) fail[son] = root;
else{
int p = fail[temp];
while(p != -1){
if(nxt[p][i]){
fail[son] = nxt[p][i];
break;
}
p = fail[p];
}
if(p == -1) fail[son] = root;
}
skt.push(son);
}
}
}
}
void query(){
int p = root,temp;
int len = strlen(str);
for(int i = 0;i < len;i++){
int id = str[i]-'A';
if(id<0||id>25){ p = root; continue; }
while(!nxt[p][id]&&p!=root) p = fail[p];
p = nxt[p][id];
if(!p) p = root;
temp = p;
while(temp != root){
ans[key[temp]]++;
temp = fail[temp];
}
}
}
int main(){
while(~scanf("%d",&n)){
fail[0] = -1,tot = 0,root = init();
memset(key,0,sizeof(key));
memset(ans,0,sizeof(ans));
for(int i = 1;i <= n;i++){
scanf("%s",st[i]);
insert(i);
}
Getfail();
scanf("%s",str);
query();
for(int i = 1;i<=n;i++)
if(ans[i]) printf("%s: %d\n",st[i],ans[i]);
}
return 0;
}