HDU-3695 computer Virus on Planet Pandora
Test instructions: computer virus, now n-clock virus command, then there is a computer instruction, look at this computer instructions in a few viruses, if the computer planted a virus, then there is a substring is a virus instruction, or the substring of the crossdress is a virus directive, now ask the computer command total infected with how many viruses.
The following: AC automata, then forward matching again, reverse match again.
Code:
1#include <bits/stdc++.h>2 using namespacestd;3 #defineLL Long Long4 #defineULL unsigned LL5 #defineFi first6 #defineSe Second7 #defineLson l,m,rt<<18 #defineRson m+1,r,rt<<1|19 #defineMax3 (A,B,C) max (A,max (b,c))Ten #defineMin3 (a,b,c) min (a,min (b,c)) One Const intINF =0x3f3f3f3f; A ConstLL mod = 1e9+7; -typedef pair<int,int>PLL; - Const intN = 1e6+Ten; the inttot =2; - Charstr[n* -], tmp[n* -]; - inttrie[n*4][ -], cnt[n* -], fair[n* -]; - voidinit () { + for(inti =1; i < tot; i++){ - for(intj =0; J < -; J + +) +TRIE[I][J] =0; A } attot =2; - } - voidInsert () { - intRT =1, Len =strlen (str); - for(inti =0; i < Len; i++){ - intid = str[i]-'A'; in if(!Trie[rt][id]) { -Cnt[tot] =0; toFair[tot] =0; +Trie[rt][id] = tot++; - } theRT =Trie[rt][id]; * } $cnt[rt]++;Panax Notoginseng } - voidBuild_tree () { thequeue<int>Q; +Q.push (1); A intp; the while(!Q.empty ()) { + intTMP =Q.front (); - Q.pop (); $ for(intj =0; J < -; J + +){ $ if(Trie[tmp][j]) { - if(TMP = =1) -FAIR[TRIE[TMP][J]] =1; the Else { -p =fair[tmp];Wuyi while(p) { the if(Trie[p][j]) { -FAIR[TRIE[TMP][J]] =Trie[p][j]; Wu Break; - } About Elsep =Fair[p]; $ } - if(!P) fair[trie[tmp][j]] =1; - } - Q.push (Trie[tmp][j]); A } + } the } - } $ intFind (intLen) { the intRET =0, RT =1; the for(inti =0; i < Len; i++){ the intid = str[i]-'A'; the while(RT! =1&&!trie[rt][id]) RT =fair[rt];; - if(Trie[rt][id]) RT =Trie[rt][id]; in intp =RT; the while(P! =1){ the if(Cnt[p] >=0){ AboutRET + =Cnt[p]; theCNT[P] =-1; the } the Else Break; +p =Fair[p]; - } the }BayiRT =1; the for(inti = len-1; I >=0; i--){ the intid = str[i]-'A'; - while(RT! =1&&!trie[rt][id]) RT =Fair[rt]; - if(Trie[rt][id]) RT =Trie[rt][id]; the intp =RT; the while(P! =1){ the if(Cnt[p] >=0){ theRET + =Cnt[p]; -CNT[P] =-1; the } the Else Break; thep =Fair[p];94 } the } the returnret; the }98 intMain () { About intT; -scanf"%d", &t);101 while(t--){102 intN;103 init ();104scanf"%d", &n); the while(n--){106scanf"%s", str);107 Insert ();108 }109 Build_tree (); thescanf"%s", TMP);111 intLen =strlen (TMP); the intCCC =0;113 for(inti =0; i < Len; i++){ the if(Tmp[i]! ='['){ theStr[ccc++] =Tmp[i]; the }117 Else {118 int_c =0;119i++; - while(IsDigit (Tmp[i]))121_c = _c *Ten+ (int) (tmp[i]-'0'), i++;122 while(_c--)123Str[ccc++] =Tmp[i];124i++; the }126 }127printf"%d\n", Find (CCC)); - }129 return 0; the}
View Code
HDU-3695 computer Virus on Planet Pandora