Computer Virus on Planet Pandora
Problem Description Aliens on Planet Pandora also write computer programs like us. Their programs only consist of capital letters (' A ' to ' Z ') which they learned from the Earth. On
Planet Pandora, hackers make computer virus, so they also has anti-virus software. Of course they learned virus scanning algorithm from the Earth. Every virus has a pattern string which consists of the capital letters. If A virus ' s pattern string is a substring of a program, or the pattern string was a substring of the reverse of that progr AM, they can say the program was infected by that virus. Give you a program and a list of virus pattern strings, please write a program to figure out how many viruses the program is infected by.
Inputthere is multiple test cases. The first line of the input was an integer T (t<=) indicating the number of test cases.
For each test case:
The first line is a integer n (0 < n <=) indicating the number of virus pattern strings.
Then n lines follows, each represents a virus pattern string. Every pattern string stands for a virus. It ' s guaranteed that those n pattern strings is all different so there
is n different viruses. The length of pattern string is no more than $ and a pattern string at least consists of one letter.
The last line of a test was the program. The program is described in a compressed format. A Compressed program consists of capital letters and
"Compressors". A "Compressor" is in the following format:
[QX]
Q is a number (0 < Q <= 5,000,000) and X are a capital letter. It means q consecutive letter Xs on the original uncompressed program. For example, [6K] means
' Kkkkkk ' in the original program. So, if a compressed program was like:
Ab[2d]e[7k]g
It actually is abddekkkkkkkg through decompressed to original format.
The length of the program was at least 1 and at the very 5,100,000, no matter in the compressed format or under it is decompres SED to original format.
Outputfor each of the test case, print an integer K in a line meaning that the program is infected by K viruses.
Sample input32abdcbdacb3abccdeghiabccdefihg4abbacdeebbbfeeea[2b]cd[4e]f
Sample Output032
HintIn the "second case" in the sample input, the reverse of the program was ' GHIFEDCCBA ', and ' GHI ' is a substring of the revers E, so the program was infected by virus ' GHI '.
#include <cstdio>#include<cstdlib>#include<queue>#include<cstring>using namespacestd;Const intSigma_size= -;Charp[1005];BOOLvis[255];Chars[5100005];structtrie{Trie*Next[sigma_size]; Trie*fail; intv; Trie () {memset (Next,0,sizeof(next)); Fail=0; V=0; }};structahocorasickautomata{Trie*Root; InlineintIdxCharc) {returnC-'A';} Ahocorasickautomata () {root=NewTrie (); } voidInsert (Char*s,intv) {intlen=strlen (s); Trie* p=Root; for(intI=0; i<len;i++) { intC=idx (s[i]); if(p->next[c]==0) {Trie* q=NewTrie (); P->next[c]=Q; } P=p->Next[c]; } P->v=v; } voidGetfail () {Queue<Trie*>p; Root->fail=0; Q.push (root); while(!Q.empty ()) {Trie* p=Q.front (); Q.pop (); for(intI=0; i<sigma_size;i++) if(p->Next[i]) {Trie* q=p->fail; while(Q&&!q->next[i]) q=q->fail; P->next[i]->fail=q?q->Next[i]:root; Q.push (P-Next[i]); } } } voidFind (Char*s) {Trie* p=Root; for(intI=0, Len=strlen (s); i<len;i++) { intC=idx (s[i]); while(P&&!p->next[c]) p=p->fail; P=p?p->next[c]:root->Next[c]; for(trie* q=p;q&&!vis[q->v];q=q->fail) vis[q->v]=1; } }};voidReadChar*s) { intlen=0; S[len]=0; while(1) { CharC=GetChar (); if(c=='\ n') {s[len]=0; Break;} if(c=='[') { intnum=0; while(1) { CharCc=GetChar (); if(! (cc>='0'&&cc<='9')) { while(num--) s[len++]=cc; GetChar (); Break; } num=num*Ten+cc-'0'; } } Elses[len++]=C; }}voidRev (Char*s) { for(intI=0, Len=strlen (s); i<len/2; i++) Swap (S[i],s[len-i-1]);}voidDFS (Trie *7) { for(intI=0; i<sigma_size;i++) if(U->next[i]) DFS (u->Next[i]); Deleteu;}intMain () {intT; scanf ("%d",&T); while(t--) {Ahocorasickautomata ac; memset (Vis,0,sizeof(VIS)); intN; scanf ("%d",&N); for(intI=1; i<=n;i++) scanf ("%s", p), AC. Insert (P,i); Ac. Getfail (); GetChar (); Read (s); Ac. Find (s); Rev (s); Ac. Find (s); intans=0; for(intI=1; i<=n;i++)if(Vis[i]) ans++; printf ("%d\n", ans); DFS (Ac.root); } return 0;}
HDU 3695 computer Virus on Planet Pandora