WA automatic chicken, WA
WA chicken
Don't believe it. You have to submit it.
Trie tree + next array.
WA template.
#include<cstdio>#include<cstring>#include<cstdlib>#include<iostream>#include<queue>#include<algorithm>using namespace std;int size,sumsize;struct AC{int son[1000001][30];int last[1000001];int fail[1000001];int back[1000001];void clear(){memset(son[0],0,sizeof(son[0]));size=1;}void clear_x(int x){memset(son[x],0,sizeof(son[x]));back[x]=0;}void insert(char *a){int len=strlen(a);int now=0;for(int i=0;i<len;i++){int c=a[i]-'a';if (!son[now][c]) {clear_x(size);son[now][c]=size++;}now=son[now][c];}back[now]++;}void gf(){queue<int> q;int u=0;fail[0]=0;for(int i=0;i<sumsize;i++){ u=son[0][i];if (u){q.push(u);fail[u]=0;last[u]=0;}}while(!q.empty()){int f=q.front();q.pop();for(int i=0;i<sumsize;i++){ u=son[f][i];if (!u){son[f][i]=son[fail[f]][i]; continue;}q.push(u);int v=fail[f];while(v&&!son[v][i]) v=fail[v];fail[u]=son[v][i];last[u]=back[fail[u]]?fail[u]:last[fail[u]];}}}int find(char *a){int u=0,cnt=0;int len=strlen(a);for(int i=0;i<len;i++){int c=a[i]-'a';u=son[u][c];int tmp=0;if (back[u])tmp=u;else if (last[u]) tmp=last[u];while(tmp){cnt+=back[tmp];back[tmp]=0;tmp=last[tmp];}}return cnt;}}WA;char s[1000001];int n;int main(){int t;sumsize=27;scanf("%d",&t);while(t--){WA.clear();scanf("%d",&n); for(int i=1;i<=n;i++){scanf("%s",s); WA.insert(s);}WA.gf();scanf("%s",s);printf("%d\n",WA.find(s));return 0;}}