Test instructions: Give T, N,t case, n string
The following gives N+1 a string, n a different small string, and the last is a pattern string
The pattern string will appear in [QX] form, q is a number, X is a letter
Ask n a small string in the pattern string appear in the number, is appearing, the opposite appears to calculate.
Pretty Nude ac automaton, originally thought in the query inside to find the end of the zero, the pattern string unfold is looking for two times, that even if found plus 0 does not affect. But it's time-out ...
So the found end is marked as -1,-1 no longer find, you can go to the code
#include <cstdio>#include<cstdlib>#include<cstring>#include<climits>#include<cctype>#include<cmath>#include<string>#include<sstream>#include<iostream>#include<algorithm>#include<iomanip>using namespacestd; #include<queue>#include<stack>#include<vector>#include<deque>#include<Set>#include<map>typedefLong LongLl;typedefLong DoubleLD;Const Doubleeps=1e-8;#definePi ACOs (-1.0)#defineLson L, M, rt<<1#defineRson m+1, R, rt<<1|1typedef pair<int,int>Pi;typedef pair<int, pi>PP; #ifdef _win32#defineLLD "%i64d"#else#defineLLD "%lld"#endif//#pragma COMMENT (linker, "/stack:1024000000,1024000000")//ll Quick (ll A, ll b) {ll ans=1;while (b) {if (b & 1) ans*=a;a=a*a;b>>=1;} return ans;}//inline int Read () {char ch= "; int ans=0;while (ch< ' 0 ' | | ch> ' 9 ') Ch=getchar (); while (ch<= ' 9 ' && Ch >= ' 0 ') {ans=ans*10+ch-' 0 '; Ch=getchar ();} return ans;}//inline void print (LL x) {printf (LLD, x);p UTS ("");//inline void Read (double &x) {char c = getchar (), while (C < ' 0 ') c = GetChar (), x = C-' 0 '; c = GetChar (); while (c >= ' 0 ') {x = x * + (C-' 0 '); c = GetChar ();}}//inline void SC (LL &x) {scanf (LLD, &x);}structtrie{intnext[5000100][ -], fail[5000100], end[5000100]; introot, L; intNewNode () { for(intI=0;i< -; i++) Next[l][i]=-1; End[l++]=0; returnL-1; } voidinit () {L=0; Root=NewNode (); } voidInsertCharbuf[]) { intlen=strlen (BUF); intnow=Root; for(intI=0; i<len;i++) { if(next[now][buf[i]-'A']==-1) Next[now][buf[i]-'A']=NewNode (); now=next[now][buf[i]-'A']; } End[now]++; } voidbuild () {Queue<int>Q; Fail[root]=Root; for(intI=0;i< -; i++) if(next[root][i]==-1) Next[root][i]=Root; Else{Fail[next[root][i]]=Root; Q.push (Next[root][i]); } while(!Q.empty ()) { intnow=Q.front (); Q.pop (); for(intI=0;i< -; i++) if(next[now][i]==-1) Next[now][i]=Next[fail[now]][i]; Else{Fail[next[now][i]]=Next[fail[now]][i]; Q.push (Next[now][i]); } } } intQueryCharbuf[]) { intlen=strlen (BUF); intnow=Root; intres=0; for(intI=0; i<len;i++) { now=next[now][buf[i]-'A']; inttmp=Now ; while(Tmp!=root && end[tmp]!=-1) {res+=END[TMP];//printf ("%d%d\n", TMP, End[tmp]);end[tmp]=-1; TMP=Fail[tmp]; } } returnRes; }}ac;Charbuf[5200010], tmp[5200010];intMain () {#ifndef Online_judge freopen ("In.txt","R", stdin); Freopen ("OUT.txt","W", stdout);#endif intT; scanf ("%d", &T); while(t--) { intN; scanf ("%d", &N); Ac.init (); for(intI=0; i<n;i++) {scanf ("%s", BUF); Ac.insert (BUF);//int Len=strlen (BUF);//reverse (buf, buf+len);//Ac.insert (BUF);} ac.build (); scanf ("%s", TMP); intLen=strlen (TMP), d=0; for(intI=0; i<len;i++) if(tmp[i]=='[') { intcur=i+1, num=0; while(IsDigit (tmp[cur]) num=num*Ten+tmp[cur++]-'0'; Fill (buf+d, buf+d+num, tmp[cur]); I=cur+1, d+=num; } Elsebuf[d++]=Tmp[i]; BUF[D]=' /';//puts (BUF);//for (int i=0;i<ac. l;i++)//printf ("%d%d\n", I, Ac.end[i]); intans=ac.query (BUF); Reverse (buf, buf+d); printf ("%d\n", ans+ac.query (BUF)); } return 0;}
hdoj 3695
[AC automaton] HDOJ3695 computer Virus on Planet Pandora