zoukankan      html  css  js  c++  java
  • HDU 3695 Computer Virus on Planet Pandora

    Computer Virus on Planet Pandora

    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’.
     
    #include<cstdio>
    #include<cstdlib>
    #include<queue>
    #include<cstring>
    using namespace std;
    const int sigma_size=26;
    char p[1005];
    bool vis[255];
    char s[5100005];
    struct Trie
    {
        Trie* next[sigma_size];
        Trie* fail;
        int v;
        Trie()
        {
            memset(next,0,sizeof(next));
            fail=0;
            v=0;
        }
    };
    struct AhoCorasickAutomata
    {
        Trie *root;
        inline int idx(char c){return c-'A';}
        AhoCorasickAutomata()
        {
            root=new Trie();
        }
        void Insert(char *s,int v)
        {
            int len=strlen(s);
            Trie* p=root;
            for(int i=0;i<len;i++)
            {
                int c=idx(s[i]);
                if(p->next[c]==0)
                {
    
                    Trie* q=new Trie();
                    p->next[c]=q;
                }
                p=p->next[c];
            }
            p->v=v;
        }
        void Getfail()
        {
            queue<Trie*>Q;
            root->fail=0;
            Q.push(root);
            while(!Q.empty())
            {
                Trie* p=Q.front();Q.pop();
                for(int i=0;i<sigma_size;i++)
                    if(p->next[i])
                    {
                        Trie* q=p->fail;
                        while(q&&!q->next[i])q=q->fail;
                        p->next[i]->fail=q?q->next[i]:root;
                        Q.push(p->next[i]);
                    }
            }
        }
        void Find(char* s)
        {
            Trie* p=root;
            for(int i=0,len=strlen(s);i<len;i++)
            {
                int c=idx(s[i]);
                while(p&&!p->next[c])p=p->fail;
                p=p?p->next[c]:root->next[c];
                for(Trie* q=p;q&&!vis[q->v];q=q->fail)vis[q->v]=1;
            }
        }
    };
    void read(char *s)
    {
        int len=0;
        s[len]=0;
        while(1)
        {
            char c=getchar();
            if(c=='
    '){s[len]=0;break;}
            if(c=='[')
            {
                int num=0;
                while(1)
                {
                    char cc=getchar();
                    if(!(cc>='0'&&cc<='9'))
                    {
                        while(num--)s[len++]=cc;
                        getchar();
                        break;
                    }
                    num=num*10+cc-'0';
                }
            }
            else s[len++]=c;
        }
    }
    void rev(char *s)
    {
        for(int i=0,len=strlen(s);i<len/2;i++)
            swap(s[i],s[len-i-1]);
    }
    void dfs(Trie *u)
    {
        for(int i=0;i<sigma_size;i++)
            if(u->next[i])dfs(u->next[i]);
        delete u;
    }
    int main()
    {
        int T;
        scanf("%d",&T);
        while(T--)
        {
            AhoCorasickAutomata ac;
            memset(vis,0,sizeof(vis));
            int n;
            scanf("%d",&n);
            for(int i=1;i<=n;i++)
                scanf("%s",p),ac.Insert(p,i);
            ac.Getfail();
            getchar();
            read(s);
            ac.Find(s);
            rev(s);
            ac.Find(s);
            int ans=0;
            for(int i=1;i<=n;i++)if(vis[i])ans++;
            printf("%d
    ",ans);
            dfs(ac.root);
        }
        return 0;
    }
  • 相关阅读:
    微软发布Visual Studio 2005快捷键文档
    获得了2007年7月份微软MVP(ASP.NET方向)
    ASP.NET代码重构插件——Refactor!™ for ASP.NET
    sonarqube C# Opencover来生成单元测试覆盖报告文档
    .Net 6 EnableDynamicLoading利用反射动态加载DLL类库的方法(解决类库不包含Nuget依赖包的问题)
    C# lib open source
    博客排名到第六,积分到100万分 留言
    vs2005视频教程 之 实例剖析 [视频]
    vs2005入门 .Net2.0视频教程 之 控件那章的小节考试[视频]
    寻找创业团队成员
  • 原文地址:https://www.cnblogs.com/homura/p/5767027.html
Copyright © 2011-2022 走看看