HDU3695---computer Virus on Planet Pandora

Source: Internet
Author: User

Computer Virus on Planet Pandora Time limit:6000/2000 MS (java/others) Memory limit:256000/128000 K (java/others)
Total submission (s): 2847 Accepted Submission (s): 799


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
Recommendchenyongfu | We have carefully selected several similar problems for you:3699 3692 3691 3697 3696

Water problem of AC automatic machine


/************************************************************************* > File Name:hdu3695.cpp > Author: ALex > Mail: [email protected] > Created time:2015 February 04 Wednesday 21:17 18 seconds ******************************* /#include <map> #include <set> #include <queue> #include <stack> #include <vector> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <algorithm>using namespace std;const double pi = ACOs ( -1); const int inf = 0x3f3f3f3f;const double EPS = 1e-15;typedef long long ll;typedef pair <int, int> pll;const int N = 5110000 ; char Str[1010];char Buf[n];char input[n];struct node{node *next[26];//depending on the topic node *fail;int Count;node () {fail = NULL;fo R (int i = 0; i <; ++i) {next[i] = NULL;} Count = 0;}}; Node *qu[500010];class ac_automation{private:node *root;int head;int tail;public:ac_automation () {head = 0;taIl = 0;root = new node ();} void Build_trie (char str[]) {int len = strlen (str); node *p = root;for (int i = 0; i < len; ++i) {int ind = Str[i]-' A '; if (P-next[ind] = = NULL) {p--next[ind] = new node ();} p = P-next[ind];} ++p, Count;}  void Build_ac () {root-fail = null;qu[tail++] = Root;while (Head < tail) {node *now = Qu[head++];node *p = null;for (int i = 0; i <; ++i) {if (now-next[i]! = NULL) {if (now-=-root) {now-next[i]-fail = root;} Else{p = Now-Fail;while (P! = NULL) {if (P-next[i]! = NULL) {now-next[i]-fail = P-Next[i];brea K;} Else{p = p, fail;}} if (p = = NULL) {now-next[i]-fail = root;} qu[tail++] = now-next[i];}}}  int Match (char buf[]) {int ans = 0;int len = strlen (BUF); node *p = root;for (int i = 0; i < len; ++i) {int ind = Buf[i] -' A '; while (P-next[ind] = = NULL && p! = root) {p = p = fail;} p = P-next[ind];if (P = = NULL) {p = root;} Node *tmp = P;while (tmp! = Root && tmp, Count! =-1) {ans + = tmp, count;tmp, count = -1;TMP = tmp, Fail;}} return ans;} void Dfs (node *p) {for (int i = 0; i <; ++i) {if (P-next[i]! = NULL) {DFS (P-next[i]);}} Delete p;} ~ac_automation () {DFS (root);}}; int main () {int t;scanf ("%d", &t), while (t--) {int n;scanf ("%d", &n); Ac_automation ac;for (int i = 1; I <= n; ++i) {scanf ("%s", str); AC. Build_trie (str);}  scanf ("%s", input), int len = strlen (input), int cnt = 0;for (int i = 0; i < len; ++i) {if (Input[i] >= ' A ' && Input[i] <= ' Z ') {buf[cnt++] = input[i];}  else if (input[i] = = ' [') {int x = 0;++i;while (input[i] >= ' 0 ' && input[i] <= ' 9 ' && i < len) {x = X * + input[i++]-' 0 ';} for (int j = 0; j < x; ++j) {buf[cnt++] = input[i];} ++i;} else if (input[i] = = '] ') {continue;}} BUF[CNT] = ' + '; AC. Build_ac (); int ans = AC. Match (BUF); for (int i = 0;i < CNT/2; ++i) {swap (buf[i], buf[cnt-i-1]);} Ans + = AC. Match (BUF);p rintf ("%d\n", ans);} return 0;}


HDU3695---computer Virus on Planet Pandora

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.