Computer Virus on Planet Pandora
Time limit:6000/2000 MS (java/others) Memory limit:256000/128000 K (java/others)
Total Submission (s): 2879 Accepted Submission (s): 808
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 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
032 Hint in the second case in the sample input, the reverse of the program was ' GHIFEDCCBA ', and ' GHI ' is a substring of the R Everse, so the program was infected by virus ' GHI '.
SourceAsia Fuzhou Regional Contest
Title Link: http://acm.hdu.edu.cn/showproblem.php?pid=3695
To ask different words or their flashbacks in the article appear in the number of times, the article may be compressed form, [QX] is a continuous ' x ' Q
Title analysis: Because to consider flashbacks, can think of two scenarios, one is to use words and their flashbacks to build a dictionary tree, because the data is too large, so that will be mle, can only take the second method, the article will be matched once again flashback match once, here I used a while (I < (int) Strlen (Get)) {...} Because this t took an afternoon, change to int l =strlen (get);
while (I < L) {...} I don't understand why.
#include <cstdio> #include <cstring> #include <queue> #include <algorithm>using namespace std; int const MAX = 5100005;char s[1005], Get[max], text[max];struct node{node *next[26]; Node *fail; BOOL End; Node () {memset (Next, NULL, sizeof (next)); Fail = NULL; end = false; }};void Insert (node *p, char *s) {for (int i = 0; s[i]! = ' + '; i++) {int idx = s[i]-' A '; if (P-next[idx] = = NULL) P--NEXT[IDX] = new node (); p = P-NEXT[IDX]; } P-end = true;} void Ac_automation (node *root) {queue <node*> q; Q.push (root); while (!q.empty ()) {Node *p = Q.front (); Q.pop (); for (int i = 0; i < i++) {if (P, next[i]) {if (p = = root) P--next[i]--fail = root; else P-next[i], fail = p, fail, next[i]; Q.push (P-next[i]); } else {if (p = = root) P--next[i] = root; else P-next[i] = p, fail-and next[i]; }}}}int Query (node *root, char *s) {int ans = 0; Node *p = root; for (int i = 0; s[i]! = ' + '; i++) {int idx = s[i]-' A '; while (!p-next[idx] && p! = root) p = p, fail; p = P-NEXT[IDX]; if (!p) {p = root; Continue } node *tmp = p; while (tmp! = root) {if (TMP-and-end) {ans + +; TMP, end = false; } else break; TMP = TMP, fail; }} return ans; void reverse (char *s) {char tmp; int len = strlen (s)-1; for (int i = 0; i < LEN/2 + 1; i++) swap (S[i], s[len-i]); S[len + 1] = ' + ';} int main () {int T; scanf ("%d", &t); while (t--) {int n, ans = 0; Node *root = new node (); memset (GET, 0, sizeof (get)); scanf ("%d", &n); while (n--) {scanf ("%s", s); Insert (root, s); } ac_automation (root); scanf ("%s", get); int num = 0, i = 0, L = strlen (get); while (I < L) {if (Get[i] <= ' Z ' && get[i] >= ' A ') text[num++] = get[i++] ; else if (get[i] = = ' [') {i++; int len = 0; while (Get[i] >= ' 0 ' && get[i] <= ' 9 ') len = ten * len + (get[i++]-' 0 '); for (int j = 0; J < Len; j + +) text[num++] = Get[i]; i + = 2; }} Text[num] = ' + '; Ans + = Query (root, text); reverse (text); Ans + = Query (root, text); printf ("%d\N ", ans); }}
HDU 3695 computer Virus on Planet Pandora (AC automaton)