题意:
给你n个病毒串 问在匹配串中这个n个病毒串分别出现过几次
分析:
ac自动机模板题//把病毒串的长度和病毒串的个搞错 蜜汁Outout Limit Exceeded 真是醉
ACcode:
#include <bits/stdc++.h>
#define maxn 2000009
#define tot 50005
using namespace std;
struct Aho{
struct state{
int next[128];
int fail,id;
}stateTable[tot];
queue<int>q;
int ans[1010];
int size;
inline void init(){
while(q.size())q.pop();
for(int i=0;i<tot;++i){
memset(stateTable[i].next,0,sizeof(stateTable[i].next));
stateTable[i].fail=stateTable[i].id=0;
}
memset(ans,0,sizeof(ans));
size=1;
}
inline void insert(char *s,int id){
int n=strlen(s);
int now=0;
for(int i=0;i<n;++i){
char c=s[i];
if(!stateTable[now].next[c])
stateTable[now].next[c]=size++;
now=stateTable[now].next[c];
}
stateTable[now].id=id;
}
inline void build(){
stateTable[0].fail=-1;
q.push(0);
while(q.size()){
int u=q.front();
q.pop();
for(int i=0;i<128;++i)
if(stateTable[u].next[i]){
if(u==0)stateTable[stateTable[u].next[i]].fail=0;
else{
int v=stateTable[u].fail;
while(v!=-1){
if(stateTable[v].next[i]){
stateTable[stateTable[u].next[i]].fail=stateTable[v].next[i];
break;
}
v=stateTable[v].fail;
}
if(v==-1)stateTable[stateTable[u].next[i]].fail=0;
}
q.push(stateTable[u].next[i]);
}
}
}
inline int match(char *s){
int n=strlen(s);
int now=0,res=0,tmp;
memset(ans,0,sizeof(ans));
for(int i=0;i<n;++i){
char c=s[i];
while(now!=0&&stateTable[now].next[c]==0)now=stateTable[now].fail;
now=stateTable[now].next[c];
if(now==-1)now=0;
tmp=now;
while(tmp!=0){
if(stateTable[tmp].id){
ans[stateTable[tmp].id]++;
res++;
}
tmp=stateTable[tmp].fail;
}
}
return res;
}
}aho;
char s[maxn];
struct N{
char s[55];
}my[1005];
int main(){
int n;
while(scanf("%d",&n)!=EOF){
aho.init();
for(int i=1;i<=n;++i){
scanf("%s",my[i].s);
aho.insert(my[i].s,i);
}
aho.build();
scanf("%s",s);
if(aho.match(s)){
for(int i=1;i<=n;++i)
if(aho.ans[i])printf("%s: %d\n",my[i].s,aho.ans[i]);
}
}
return 0;
}
/*
3
AA
BB
CC
ooxxCC%dAAAoen....END
4
A
AA
BB
CC
ooxxCC%dAAAoen....END
1
AAAAA
AAA
*/