题目
传送门:QWQ
分析
$ AC $自动机模板,黈力的码风真的棒极了,这是我抄他的。
还有 题号不错
代码
#include <cstdio> #include <cstring> #define N 500007 using namespace std; int n,rt,newp,i,m,l,r,son[N][26],fail[N],cur,j,end[N],q[N],vis[N],ans[N],end2[N],end3[N]; char s[1000000]; int find(int cur,int i){ if(!cur)return rt; if(son[cur][i])return son[cur][i]; return fail[son[cur][i]]=find(fail[cur],i); } int main(){ int T; scanf("%d",&T); while(T--) { memset(son,0,sizeof(son));memset(fail,0,sizeof(fail));memset(vis,0,sizeof(vis)); memset(q,0,sizeof(q));memset(end,0,sizeof(end));memset(ans,0,sizeof(ans)); memset(end2,0,sizeof(end2)); memset(end3,0,sizeof(end3)); scanf("%d",&n); rt=newp=1; for(i=1;i<=n;i++){ scanf("%s",s+1); m=strlen(s+1); for(cur=rt,j=1;j<=m;j++){ if(!son[cur][s[j]-'a']) son[cur][s[j]-'a']=++newp; cur=son[cur][s[j]-'a']; } end[cur]=i; end2[cur]++; end3[i]=cur; } for(q[l=r=1]=rt;l<=r;l++){ for(i=0;i<26;i++) if(son[q[l]][i]){ fail[son[q[l]][i]]=find(fail[q[l]],i); q[++r]=son[q[l]][i]; } } scanf("%s",s+1); m=strlen(s+1);int ansx=0; for(i=1,cur=rt;i<=m;i++) cur=find(cur,s[i]-'a'),vis[cur]++; for(i=r;i>=1;i--){ vis[fail[q[i]]]+=vis[q[i]]; if(end[q[i]]) { if(vis[q[i]]!=0) ansx+=end2[q[i]]; } } printf("%d ",ansx); } return 0; } /* 1 10 ab aba bba a aa a a aba b b ababbaba */