• HDU Computer Virus on Planet Pandora (AC自动机)


    Computer Virus on Planet Pandora

    Time Limit : 6000/2000ms (Java/Other)   Memory Limit : 256000/128000K (Java/Other)
    Total Submission(s) : 12   Accepted Submission(s) : 5

    Font: Times New Roman | Verdana | Georgia

    Font Size:  

    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 have anti-virus software. Of course they learned virus scanning algorithm from the Earth. Every virus has a pattern string which consists of only capital letters. If a virus’s pattern string is a substring of a program, or the pattern string is a substring of the reverse of that program, they can say the program is 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.

    Input

    There are multiple test cases. The first line in the input is an integer T ( T<= 10) indicating the number of test cases.

    For each test case:

    The first line is a integer n( 0 < n <= 250) 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 are all different so there 
    are n different viruses. The length of pattern string is no more than 1,000 and a pattern string at least consists of one letter.

    The last line of a test case is the program. The program may be 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 is a capital letter. It means q consecutive letter xs in the original uncompressed program. For example, [6K] means 
    ‘KKKKKK’ in the original program. So, if a compressed program is like:

    AB[2D]E[7K]G

    It actually is ABDDEKKKKKKKG after decompressed to original format.

    The length of the program is at least 1 and at most 5,100,000, no matter in the compressed format or after it is decompressed to original format.

    Output

    For each test case, print an integer K in a line meaning that the program is infected by K viruses.

    Sample Input

    3
    2
    AB
    DCB
    DACB
    3
    ABC
    CDE
    GHI
    ABCCDEFIHG
    4
    ABB
    ACDEE
    BBB
    FEEE
    A[2B]CD[4E]F
    

    Sample Output

    0
    3
    2

    Hint

    In the second case in the sample input, the reverse of the program is ‘GHIFEDCCBA’, and ‘GHI’ is a substring of the reverse, so the program is infected 
    by virus ‘GHI’.

    Source

    2010 Asia Fuzhou Regional Contest

    题目:一些字典,然后给出一个主串,问正向和反向的一起出现了多少个模式串

    http://acm.hdu.edu.cn/showproblem.php?pid=3695 

    建好AC自动机后,正向和反向扫描一遍就行了。

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    
    using namespace std;
    
    const int M=6000010;
    const int N=1000010;
    
    struct Trie{
        int count;
        Trie *next[26];
        Trie *fail;
        Trie(){
            count=0;
            fail=NULL;
            memset(next,NULL,sizeof(next));
        }
    }*q[M];
    
    Trie *NewNode(){
        Trie *tmp=new Trie;
        memset(tmp->next,NULL,sizeof(tmp->next));
        tmp->count=0;
        return tmp;
    }
    
    void Insert(char *str,Trie *root){
        Trie *loc=root;
        int i=0;
        while(str[i]!='\0'){
            int id=str[i]-'A';
            if(loc->next[id]==NULL)
                loc->next[id]=new Trie();
            loc=loc->next[id];
            i++;
        }
        loc->count++;
    }
    
    void AC_automation(Trie *root){
        int head=0,tail=0;
        Trie *cur,*tmp;
        root->fail=NULL;
        q[tail++]=root;
        while(head!=tail){
            cur=q[head++];
            tmp=NULL;
            for(int i=0;i<26;i++){
                if(cur->next[i]==NULL)
                    continue;
                if(cur==root)
                    cur->next[i]->fail=root;
                else{
                    tmp=cur->fail;
                    while(tmp!=NULL){
                        if(tmp->next[i]!=NULL){
                            cur->next[i]->fail=tmp->next[i];
                            break;
                        }
                        tmp=tmp->fail;
                    }
                    if(tmp==NULL)
                        cur->next[i]->fail=root;
                }
                q[tail++]=cur->next[i];
            }
        }
    }
    
    int query(char *str,Trie *root){
        int ans=0;
        Trie *loc=root,*tmp;
        int i=0;
        while(str[i]!='\0'){
            int id=str[i]-'A';
            while(loc->next[id]==NULL && loc!=root)
                loc=loc->fail;
            loc=loc->next[id];
            loc=(loc==NULL)?root:loc;
            tmp=loc;
            while(tmp!=root){
                if(tmp->count>0){
                    ans+=tmp->count;
                    tmp->count=0;
                }else
                    break;
                tmp=tmp->fail;
            }
            i++;
        }
        return ans;
    }
    
    char str1[M],str2[M],str[M];
    
    void Change(char *s){
        int len=strlen(s);
        int j=0,q;
        for(int i=0;i<len;i++){
            if(str[i]==']')
                continue;
            if(s[i]=='['){
                i++;
                q=0;
                while(s[i]>='0' && s[i]<='9'){
                    q=q*10+s[i]-'0';
                    i++;
                }
                while(q--)
                    str1[j++]=s[i];
            }else
                str1[j++]=s[i];
        }
        str1[j]='\0';
        for(int i=0;i<j;i++)
            str2[i]=str1[j-1-i];
        str2[j]='\0';
    }
    
    int main(){
    
        //freopen("input.txt","r",stdin);
    
        int t,n;
        scanf("%d",&t);
        char wrd[1010];
        while(t--){
            Trie *root=new Trie();
            scanf("%d",&n);
            for(int i=0;i<n;i++){
                scanf("%s",wrd);
                Insert(wrd,root);
            }
            AC_automation(root);
            scanf("%s",str);
            Change(str);
            printf("%d\n",query(str1,root)+query(str2,root));
        }
        return 0;
    }
  • 相关阅读:
    浅谈IO这件事
    tushare+pandas实现财经数据分析
    大话设计模式Python实现-解释器模式
    大话设计模式Python实现- 享元模式
    大话设计模式Python实现-中介者模式
    大话设计模式Python实现-职责链模式
    大话设计模式Python实现-命令模式
    大话设计模式Python实现-桥接模式
    java之AbstractStringBuilder类详解
    java之Class类详解
  • 原文地址:https://www.cnblogs.com/jackge/p/3058722.html
Copyright © 2020-2023  润新知