zoukankan      html  css  js  c++  java
  • HDU 3695 Computer Virus on Planet Pandora(AC自动机模版题)

    Computer Virus on Planet Pandora

    Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 256000/128000 K (Java/Others)
    Total Submission(s): 4090    Accepted Submission(s): 1072

    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’.

    题目链接:HDU 3695

    比较裸的一道题目,拿来熟练默写一下自动机模版还行,主串转换完记得清空数组或者在结尾加上'',不然若小于上一次留下来的主串长度则会跟上一次的连在一起造成WA,坑了好久…………

    代码:

    #include <stdio.h>
    #include <bits/stdc++.h>
    using namespace std;
    #define INF 0x3f3f3f3f
    #define CLR(arr,val) memset(arr,val,sizeof(arr))
    #define LC(x) (x<<1)
    #define RC(x) ((x<<1)+1)
    #define MID(x,y) ((x+y)>>1)
    typedef pair<int,int> pii;
    typedef long long LL;
    const double PI=acos(-1.0);
    const int N=1010;
    const int M=5100010;
    struct Aho
    {
        struct Trie
        {
            int nxt[26];
            int cnt,fail;
            inline void reset()
            {
                CLR(nxt,0);
                cnt=fail=0;
            }
        };
        Trie L[260*N];
        int tot;
    
        void init()
        {
            L[0].reset();
            tot=1;
        }
    
        void insert(char s[])
        {
            int len=strlen(s);
            int now=0;
            for (int i=0; i<len; ++i)
            {
                int indx=s[i]-'A';
                if(!L[now].nxt[indx])
                {
                    L[tot].reset();
                    L[now].nxt[indx]=tot++;
                }
                now=L[now].nxt[indx];
            }
            ++L[now].cnt;
        }
    
        void build()
        {
            L[0].fail=-1;
            queue<int>Q;
            Q.push(0);
            while (!Q.empty())
            {
                int now=Q.front();
                Q.pop();
                for (int i=0; i<26; ++i)
                {
                    if(!L[now].nxt[i])
                        continue;
                    if(!now)
                        L[L[now].nxt[i]].fail=0;
                    else
                    {
                        int v=L[now].fail;
                        while (~v)
                        {
                            if(L[v].nxt[i])
                            {
                                L[L[now].nxt[i]].fail=L[v].nxt[i];
                                break;
                            }
                            v=L[v].fail;
                        }
                        if(v==-1)
                            L[L[now].nxt[i]].fail=0;
                    }
                    Q.push(L[now].nxt[i]);
                }
            }
        }
    
        int cal(int now)
        {
            int r=0;
            while (now)
            {
                r+=L[now].cnt;
                L[now].cnt=0;
                now=L[now].fail;
            }
            return r;
        }
        int solve(char s[])
        {
            int len=strlen(s);
            int now=0;
            int ret=0;
            for (int i=0; i<len; ++i)
            {
                int indx=s[i]-'A';
                if(L[now].nxt[indx])
                    now=L[now].nxt[indx];
                else
                {
                    int p=L[now].fail;
                    while (~p&&!L[p].nxt[indx])
                        p=L[p].fail;
                    if(p==-1)
                        now=0;
                    else
                        now=L[p].nxt[indx];
                }
                if(L[now].cnt)
                    ret+=cal(now);
            }
            return ret;
        }
    };
    Aho aho;
    char s[N],temp[M],tar[M];
    
    int main(void)
    {
        int tcase,n,i;
        scanf("%d",&tcase);
        while (tcase--)
        {
            CLR(tar,0);//就是这里要清空一下
            aho.init();
            scanf("%d",&n);
            for (i=0; i<n; ++i)
            {
                scanf("%s",s);
                aho.insert(s);
            }
            aho.build();
            scanf("%s",temp);
            int len=strlen(temp);
            int cnt=0;
            for (i=0; i<len; ++i)
            {
                if(temp[i]>='A'&&temp[i]<='Z')
                    tar[cnt++]=temp[i];
                else
                {
                    ++i;
                    int C=0;
                    while (temp[i]<'A'||temp[i]>'Z')
                    {
                        C=C*10+(temp[i]-'0');
                        ++i;
                    }
                    char c=temp[i];
                    while (C--)
                        tar[cnt++]=c;
                    ++i;
                }
            }
            int ans=0;
            ans+=aho.solve(tar);
            reverse(tar,tar+cnt);
            ans+=aho.solve(tar);
            printf("%d
    ",ans);
        }
        return 0;
    }
  • 相关阅读:
    Web API中的消息处理程序(Message Handler)
    Web API中的模型验证
    autoconf配置的项目,编译debug版本
    gcc编译工具生成动态库和静态库之一----介绍
    VMware中四种网络连接模式的区别
    vmware虚拟机三种网络模式的区别
    code block自动生成makefile
    centos7 安装 codeblock(rpm)
    利用cbmakegen导出Code::blocks的Makefile
    C/C++程序CPU问题分析
  • 原文地址:https://www.cnblogs.com/Blackops/p/6099789.html
Copyright © 2011-2022 走看看