Hdu 2896 Virus attack & hdu 3695 Computer Virus on Planet Pandora AC automatic machine

Source: Internet
Author: User
 /*
2896 very bare AC automatic machines, pay attention to the heavy lifting. */# include <cstdio> # include <cstring> # include <iostream> # include <algorithm> # include <queue> # include <set> using namespace std; const int kind = 128; set <int> S; set <int>: iterator it; struct node {node * fail; // failure pointer node * next [kind]; // The 26 subnodes (up to 26 letters) of each Tire node int count, id; // whether it is the last node of the word node () {// constructor initialization fail = NULL; count = 0; memset (next, NULL, sizeof (next) ;}; // queue for bfs Construction Creation failure pointer char keyword [201]; // input word char str [10009]; // mode string int head, tail; // void insert (char * str, node * root, int id) {node * p = root; int I = 0, index; while (str [I]) {index = str [I]; if (p-> next [index] = NULL) p-> next [index] = new node (); p = p-> next [index]; I ++;} p-> count ++; p-> id = id;} void build_ac_automation (node * root) {int I; root-> fail = NULL; queue <node *> q; q. push (root); while (! Q. empty () {node * temp = q. front (); q. pop (); node * p = NULL; for (I = 0; I <128; I ++) {if (temp-> next [I]! = NULL) {if (temp = root) temp-> next [I]-> fail = root; else {p = temp-> fail; while (p! = NULL) {if (p-> next [I]! = NULL) {temp-> next [I]-> fail = p-> next [I]; break;} p = p-> fail ;} if (p = NULL) temp-> next [I]-> fail = root;} q. push (temp-> next [I]) ;}}} int n, m; int query (node * root) {int I = 0, cnt = 0, index, len = strlen (str); node * p = root; while (str [I]) {index = str [I]; while (p-> next [index] = NULL & p! = Root) p = p-> fail; p = p-> next [index]; p = (p = NULL )? Root: p; node * temp = p; while (temp! = Root) {cnt + = temp-> count; if (temp-> id> 0 & temp-> id <= n) S. insert (temp-> id); temp = temp-> fail;} I ++;} return cnt;} int main () {char c; while (scanf ("% d", & n )! = EOF) {node * root = new node (); for (int I = 1; I <= n; I ++) {scanf ("% s", keyword ); insert (keyword, root, I) ;}build_ac_automation (root); scanf ("% d", & m); int tot = 0; for (int I = 1; I <= m; I ++) {S. clear (); scanf ("% s", str); query (root); if (S. size ()> 0) {printf ("web % d:", I); for (it = S. begin (); it! = S. end (); it ++) printf ("% d", * it); puts (""); tot ++ ;}} printf ("total: % d \ n ", tot);} return 0;}/* calculates the number of matched strings, it itself is a child string of the parent string or it is a child string of the parent string after being flipped; at the beginning, when creating the dictionary tree, insert the matching string and its Child string, the mle of the cup is later inserted only to itself. It is enlightening to make the mother string match each time in order and in reverse order... */# Include <cstdio> # include <cstring> # include <iostream> # include <algorithm> # include <queue> using namespace std; const int kind = 26; struct node {node * fail; // failure pointer node * next [kind]; // The 26 subnodes (up to 26 letters) of each Tire node int count; // whether it is the last node of the word node () {// constructor initialization fail = NULL; count = 0; memset (next, NULL, sizeof (next) ;}}; // queue for bfs Construction Failure pointer char keyword [1001]; // input word char str [5100001]; // mode string int head, tail; // The header and tail pointer of the queue void insert (char * str, node * root) {node * p = root; int I = 0, index; while (str [I]) {index = str [I]-'A'; if (p-> next [index] = NULL) p-> next [index] = new node (); p = p-> next [index]; I ++;} p-> count ++;} void build_ac_automation (node * root) {int I; root-> fail = NULL; // q [head ++] = root; queue <node *> q; q. push (root); while (! Q. empty () {node * temp = q. front (); q. pop (); node * p = NULL; for (I = 0; I <26; I ++) {if (temp-> next [I]! = NULL) {if (temp = root) temp-> next [I]-> fail = root; else {p = temp-> fail; while (p! = NULL) {if (p-> next [I]! = NULL) {temp-> next [I]-> fail = p-> next [I]; break;} p = p-> fail ;} if (p = NULL) temp-> next [I]-> fail = root;} // q [head ++] = q. push (temp-> next [I]) ;}}} int query (node * root) {int I = 0, cnt = 0, index, len = strlen (str); node * p = root; while (str [I]) {index = str [I]-'A '; while (p-> next [index] = NULL & p! = Root) p = p-> fail; p = p-> next [index]; p = (p = NULL )? Root: p; node * temp = p; while (temp! = Root & temp-> count! =-1) {cnt + = temp-> count; temp-> count =-1; temp = temp-> fail;} I ++;} I = len-1; while (str [I]) {index = str [I]-'A'; while (p-> next [index] = NULL & p! = Root) p = p-> fail; p = p-> next [index]; p = (p = NULL )? Root: p; node * temp = p; while (temp! = Root & temp-> count! =-1) {cnt + = temp-> count; temp-> count =-1; temp = temp-> fail;} I --;} return cnt ;} int main () {int n, t; char c; scanf ("% d", & t); while (t --) {head = tail = 0; node * root = new node (); scanf ("% d", & n); while (n --) {scanf ("% s", keyword); insert (keyword, root); // reverse (keyword, keyword + strlen (keyword); // insert (keyword, root);} build_ac_automation (root); int j = 0; getchar (); while (c = getchar (), c! = '\ N') {if (c =' [') {scanf ("% d", & n); c = getchar (); for (int I = 0; I <n; I ++) str [j ++] = c; getchar () ;}else str [j ++] = c ;} str [j] = '\ 0'; printf ("% d \ n", query (root);} return 0 ;}

Related Article

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.