HDU 3695 computer virus on planet Pandora (AC automatic machine)

Source: Internet
Author: User

Link:

Http://acm.hdu.edu.cn/showproblem.php? PID = 1, 3695

Question:

Give n matching strings, and then give a long string s to find that there areQuantityMatching string. Note that the reverse sequence of the matching string in S is displayed.

S may compress some segments in the format of [QX]. Q is a number and X is a letter, which indicates that Q x is used. Assume that after B [3A] de is "decompressed ",

Baaade


Analysis and Summary:

In the first place, I did not understand the meaning of the question. I thought it was the total number of all viruses and the result was "tle .. In fact, this question requires the number of viruses in the source code. Once a virus is found, mark the word in trie as false.

In addition, the steps to decompress the source code of the "COMPRESSED" website must be careful and error-prone.

Code:

# Include <iostream> # include <cstdio> # include <cstring> # include <queue> using namespace STD; const int maxn = 5200005; const int max_node = 3000005; const int sigma_size = 26; char STR [maxn]; char Buf [maxn]; struct node {node * fail; node * Next [sigma_size]; bool isword; int ID; void Init () {id = 0; isword = false; memset (next, 0, sizeof (next) ;}}; class ac_automation {public: void Init (); void insert (char *, INT); voi D getfail (); int find (char *); Private: node * new_node (); int ID (char ch) {return ch-'A';} PRIVATE: node heap [max_node]; node * root; int size ;}; node * ac_automation: new_node () {heap [size]. init (); Return & heap [size ++];} void ac_automation: Init () {size = 0; root = new_node ();} void ac_automation :: insert (char * STR, int ID) {node * P = root; For (; * STR; ++ Str) {int CH = ID (* Str ); if (p-> next [CH] = NULL) P-> next [CH] = New_node (); P = p-> next [CH];} p-> isword = true; P-> id = ID;} void ac_automation: getfail () {queue <node *> q; q. push (Root); While (! Q. empty () {node * TMP = Q. front (); q. pop (); For (INT I = 0; I <sigma_size; ++ I) {node * & now = TMP-> next [I]; If (now! = NULL) {q. Push (now); If (TMP = root) now-> fail = root; else {node * P = TMP-> fail; while (P! = NULL) {If (p-> next [I]! = NULL) {now-> fail = p-> next [I]; break;} p = p-> fail;} If (P = NULL) now-> fail = root;} else {If (TMP = root) Now = root; else now = TMP-> fail-> next [I] ;}}} int ac_automation: Find (char * Str) {node * P = root; int CNT = 0; For (; * STR; ++ Str) {int CH = ID (* Str); P = p-> next [CH]; If (P = NULL) P = root; node * TMP = P; while (TMP! = Root & TMP-> isword) {++ CNT; TMP-> isword = false; TMP = TMP-> fail;} return CNT ;} // string transpose inline void Rev (char * STR, int Len) {char ch; For (INT I = 0, K = len-1; I <Len/2; ++ I, -- k) {CH = STR [I]; STR [I] = STR [k]; STR [k] = CH ;}} // string "decompress" inline void change (char * STR, char * Now) {memset (now, 0, sizeof (now); int Len = strlen (STR ); int I; for (I = 0; I <Len; ++ I) {If (STR [I] = '[') {++ I; int num = 0; while (STR [I]> = '0' & STR [I] <= '9') {num = num * 10 + STR [I]-'0 '; ++ I ;}while (Num --) {* Now = STR [I]; ++ now ;}++ I ;}else {* Now = STR [I]; + + now ;}}ac_automation AC; int main () {int N, ncase; char Pat [1005]; scanf ("% d", & ncase ); while (ncase --) {AC. init (); scanf ("% d % * C", & N); For (INT I = 1; I <= N; ++ I) {gets (PAT ); AC. insert (Pat, I);} AC. getfail (); memset (BUF, 0, sizeof (BUF); memset (STR, 0, sizeof (STR); gets (BUF); change (BUF, STR ); int ans = ac. find (STR); Rev (STR, strlen (STR); printf ("% d \ n", ANS + AC. find (STR);} return 0 ;}


 -- The significance of life is to give it a meaningful person.

Original Http://blog.csdn.net/shuangde800,
D_double (reprinted please mark)

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.