HDU 2222 Template Questions
1#include <iostream>2#include <cstdio>3#include <cstring>4#include <algorithm>5#include <queue>6 using namespacestd;7 Const intmaxn=1001000;8 intN,kase,sz;9 CharSTR[MAXN];Ten intson[maxn][ -],FAIL[MAXN],WORD[MAXN]; One BOOLVIS[MAXN]; Aqueue<int>Q; -InlinevoidInit () - { theMemset (son,0,sizeof(son)); -sz=1; for(intI=1; i<= -; i++) son[0][i]=1; -memset (Fail,0,sizeof(Fail)); -memset (Word,0,sizeof(Word)); +memset (Vis,false,sizeof(Vis)); - } +InlinevoidInsert (Char*S) A { at intLen=strlen (s+1), pos=1; - for(intI=1; i<=len;i++) - { - intt=s[i]-'a'+1; - if(Son[pos][t]) pos=son[pos][t];Elsepos=son[pos][t]=++sz; - } inword[pos]++; - } toInlinevoidMake () + { -Q.push (1); fail[1]=0; the while(!q.empty ()) * { $ intu=Q.front (); Q.pop ();Panax Notoginseng for(intI=1; i<= -; i++) - if(Son[u][i]) the { + intk=Fail[u]; A while(!son[k][i]) k=Fail[k]; thefail[son[u][i]]=Son[k][i]; + Q.push (Son[u][i]); - } $ } $ } -InlineintSolve () - { the intLen=strlen (str+1), pos=1, ans=0; - for(intI=1; i<=len;i++)Wuyi { thevis[pos]=true; - intt=str[i]-'a'+1; Wu while(!son[pos][t]) pos=Fail[pos]; -pos=Son[pos][t]; About if(!Vis[pos]) $ for(intj=pos;j;j=Fail[j]) - { -ans+=Word[j]; -word[j]=0; A } + } the returnAns; - } $ intMain () the { thescanf"%d",&Kase); the for(intKase=1; kase<=kase;kase++) the { -scanf"%d",&N); Init (); in for(intI=1; i<=n;i++) the { thescanf"%s", str+1); About Insert (str); the } the Make (); thescanf"%s", str+1); +printf"%d\n", Solve ()); - } the return 0;Bayi}
C + +
AC Automatic Machine