记一下数组+node实现+封装的新板子
http://acm.hdu.edu.cn/showproblem.php?pid=2222
#include <bits/stdc++.h> #define IO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0) #define rep(ii,a,b) for(int ii=a;ii<=b;++ii) using namespace std; const int maxn=1e6+10,maxm=2e6+10; int casn,n,m; const int csize=26,minc='a'; class autom{public: #define nd node[now] struct acnode{int cnt,son[csize],fail;}node[maxn]; int sz=0; void clear(int n=maxn-5){ memset(node,0,sizeof(acnode)*n); sz=0; } void insert(char *s,int len){ int now=0; rep(i,0,len-1){ int ch=s[i]-minc; if(!nd.son[ch]) nd.son[ch]=++sz; now=nd.son[ch]; } node[now].cnt++; } void init(){ queue<int> que; int now=0; rep(i,0,csize-1) if(nd.son[i]) que.push(nd.son[i]); while(!que.empty()){ now=que.front();que.pop(); rep(i,0,csize-1){ if(nd.son[i]) { node[nd.son[i]].fail=node[nd.fail].son[i]; que.push(nd.son[i]); }else nd.son[i]=node[nd.fail].son[i]; } } } int query(char *t,int len){ int now=0,res=0; rep(i,0,len-1) { now=nd.son[t[i]-minc]; for(int j=now;j&&node[j].cnt!=-1;j=node[j].fail) res+=node[j].cnt,node[j].cnt=-1; } return res; } }acam; char s[maxm]; int main(){IO; cin>>casn; while(casn--){ cin>>n; acam.clear(); rep(i,1,n){ cin>>s; acam.insert(s,strlen(s)); } acam.init(); cin>>s; cout<<acam.query(s,strlen(s))<<endl; } }