Hdoj topic 3695 Computer Virus on Planet Pandora (AC automaton)

Source: Internet
Author: User

Computer Virus on Planet PandoraTime limit:6000/2000 MS (java/others) Memory limit:256000/128000 K (java/others)
Total submission (s): 3169 Accepted Submission (s): 867


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 3698 3697

Bare topic instantaneous seconds
AC Code

#include <stdio.h> #include <string.h>char str[5100000],key[1010],ss[5100000];int head,tail;struct node{    Node *fail;    Node *next[26];    int cnt;        Node () {fail=null;        cnt=0;    for (int i=0;i<26;i++) next[i]=null;    }}*q[252000];node *root;void Insert (char *s) {int temp,len,i;    Node *p=root;    Len=strlen (s);        for (i=0;i<len;i++) {temp=s[i]-' A ';        if (P->next[temp]==null) p->next[temp]=new node ();    p=p->next[temp]; } p->cnt++;}    void Build_ac () {root->fail=null;    Q[tail++]=root;        while (Head!=tail) {node *p=q[head++];        Node *temp=null;                for (int i=0;i<26;i++) {if (p->next[i]!=null) {if (p==root)                {p->next[i]->fail=root;                    } else {temp=p->fail;                 while (Temp!=null)   {if (temp->next[i]!=null) {p->next[i]-&                            gt;fail=temp->next[i];                        Break                    } temp=temp->fail;                    } if (temp==null) {p->next[i]->fail=root;            }} q[tail++]=p->next[i];    }}}}int query () {int ans=0;    int Len=strlen (str);    Node *p=root,*temp;        for (int i=0;i<len;i++) {int x=str[i]-' A ';        while (p->next[x]==null&&p!=root) {p=p->fail;        } p=p->next[x];        if (p==null) {p=root;        } temp=p;            while (temp!=root&&temp->cnt!=-1) {ans+=temp->cnt;            temp->cnt=-1;        temp=temp->fail; }} return ans; int main () {inT T;    scanf ("%d", &t);        while (t--) {int n;        scanf ("%d", &n);        int i;        head=tail=0;        Root=new node ();            for (i=0;i<n;i++) {scanf ("%s", key);        Insert (key);        } build_ac ();        scanf ("%s", SS);        int Len=strlen (ss), J=0,K1;        for (i=0;i<len;)                {if (ss[i]== ' [') {i++;                int num=0;                    while (ss[i]>= ' 0 ' &&ss[i]<= ' 9 ') {num=num*10+ (ss[i]-' 0 ');                i++;                } while (num--) {str[j++]=ss[i];            } i+=2;        } else str[j++]=ss[i++];    }str[j]= ' + '; printf ("%s\n", str); int ans=query (); Len=strlen (str); for (i=0;i<= (len-1)/2;i++) {char ch=str[len-1-i];str[ Len-1-i]=str[i];str[i]=ch;}        Ans+=query ();    printf ("%d\n", ans); }}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Hdoj topic 3695 Computer Virus on Planet Pandora (AC automaton)

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.