Computer Virus on Planet PandoraTime
limit:6000/2000 MS (java/others) Memory limit:256000/128000 K (java/others)
Total submission (s): 2480 Accepted Submission (s): 688
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 Input
32abdcbdacb3abccdeghiabccdefihg4abbacdeebbbfeeea[2b]cd[4e]f
Sample Output
032Hintin the "second case" in the sample input, the reverse of the program was ' GHIFEDCCBA ', and ' GHI ' is a Substri Ng of the reverse, so the program was infected by virus ' GHI '.
Source2010 Asia Fuzhou Regional Contest
problem-solving ideas: AC automata naked problem, to find the positive and negative two strings of the total number of words appear
#include <iostream> #include <cstdio> #include <cstring> #include <queue> #define N 5555555using Namespace std;struct node{node *fail; Node *next[26]; int cnt; Node () {fail=0; cnt=0; memset (Next,0,sizeof (next)); }};node *root=null;void Insert (char *str) {int i=0,index; Node *p=root; while (str[i]!= ') {index=str[i]-' A '; if (p->next[index]==null) p->next[index]=new node; p=p->next[index]; i++; } p->cnt++;} void build () {int i; root->fail=null; Queue<node*> Q; Q.push (root); while (!q.empty ()) {node *tmp=q.front (); Q.pop (); Node *p=null; for (i=0;i<26;i++) {if (Tmp->next[i]) {if (tmp==root) tmp->next[i]->fail=root; else {p=tmp->fail; while (p) { if (P->next[i]) {tmp->next[i]->fail=p-& Gt;next[i]; Break } p=p->fail; } if (P==null) tmp->next[i]->fail=root; } q.push (Tmp->next[i]); }}}}int query (char *str) {int i=0,cnt=0,index; Node *p=root; while (str[i]!= ') {//cout<<i<<endl; index=str[i]-' A '; while (P->next[index]==null&&p!=root) p=p->fail; p=p->next[index]; p= (p==null) root:p; Node *tmp=p; while (tmp!=root&&tmp->cnt!=-1) {cnt+=tmp->cnt; tmp->cnt=-1; tmp=tmp->fail; } i++; } return CNT;} Char s1[n],s2[n];void input () {int p=0; char c; GetChar (); while (C=getchar (),C!= ' \ n ') {if (c!= ' [') s1[p++]=c; else {int x; scanf ("%d", &x); C=getchar (); while (x--) s1[p++]=c; GetChar (); }} for (int q=0;p>0;q++) s2[q]=s1[--p]; cout<<s1<< "" <<s2<<endl;} void del (node *root) {for (int i=0;i<26;i++) if (Root->next[i]) del (root->next[i]); Delete root;} int main () {//Freopen ("In.txt", "R", stdin);//Freopen ("OUT.txt", "w", stdout); int t,n; scanf ("%d", &t); Char str[1005]; while (t--) {root=new node; memset (S1, ' n ', sizeof (S1)); memset (S2, ' n ', sizeof (S2)); scanf ("%d", &n); while (n--) scanf ("%s", str), insert (str); Build (); Input (); printf ("%d\n", Query (S1) +query (S2)); del (root); } return 0;}