Just record the code
Tutorial AC automaton algorithms of the venue here
There are here
Pointer looking to rip off force can also look here
#include<iostream> #include<cstdio> #include<queue> #include<cstring> #define maxn 10010 #define maxl 1000100 using namespace std; int n; char s[maxl]; struct ac_automation { int tot; struct node { Node * Son [ 26 is ]; // record son int size; // Number son Node * Fail; // mismatch pointer node () { memset(this,0,sizeof(node)); } }; node *root; inline void init() { root=new node(); } inline void insert() { int l=strlen(s+1),i=1; node *now=root; while(i<=l) { if(!now->son[s[i]-'a'])now->son[s[i]-'a']=new node(); now=now->son[s[i]-'a']; i++; } now->size++; } inline void build() { queue <node*> q; for(int i=0;i<26;i++) { if(root->son[i]) { q.push(root->son[i]); root->son[i]->fail=root; } else root->son[i]=root; } while(!q.empty()) { node *x=q.front(); q.pop(); for(int i=0;i<26;i++) { if(x->son[i]) { x->son[i]->fail=x->fail->son[i]; q.push(x->son[i]); } else x->son[i]=x->fail->son[i]; } } } inline int query() { node *now=root; int i=1,l=strlen(s+1),ans=0; while(i<=l) { now=now->son[s[i]-'a']; for(node *j=now;j!=root&&j->size!=-1;j=j->fail) { ans+=j->size; j->size=-1; } i++; } return years; } }ac; int main () { int t; scanf("%d",&t); while(t--) { ac.init (); // do not forget Scanf ( " % D " , & n-); for ( int I = . 1 ; I <= n-; I ++ ) { scanf("%s",s+1); ac.insert(); } ac.build(); scanf("%s",s+1); printf("%d\n",ac.query()); } return 0; }