zoukankan      html  css  js  c++  java
  • hdu 3065 AC自动机(各子串出现的次数)

    病毒侵袭持续中

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 10324    Accepted Submission(s): 3633


    Problem Description
    小t非常感谢大家帮忙解决了他的上一个问题。然而病毒侵袭持续中。在小t的不懈努力下,他发现了网路中的“万恶之源”。这是一个庞大的病毒网站,他有着好多好多的病毒,但是这个网站包含的病毒很奇怪,这些病毒的特征码很短,而且只包含“英文大写字符”。当然小t好想好想为民除害,但是小t从来不打没有准备的战争。知己知彼,百战不殆,小t首先要做的是知道这个病毒网站特征:包含多少不同的病毒,每种病毒出现了多少次。大家能再帮帮他吗?
     
    Input
    第一行,一个整数N(1<=N<=1000),表示病毒特征码的个数。
    接下来N行,每行表示一个病毒特征码,特征码字符串长度在1—50之间,并且只包含“英文大写字符”。任意两个病毒特征码,不会完全相同。
    在这之后一行,表示“万恶之源”网站源码,源码字符串长度在2000000之内。字符串中字符都是ASCII码可见字符(不包括回车)。
     
    Output
    按以下格式每行一个,输出每个病毒出现次数。未出现的病毒不需要输出。
    病毒特征码: 出现次数
    冒号后有一个空格,按病毒特征码的输入顺序进行输出。
     
    Sample Input
    3
    AA
    BB
    CC
    ooxxCC%dAAAoen....END
    /*
    hdu 3065 AC自动机(各子串出现的次数)
    
    给你m个子串,然后从一个字符串中查找这些子串哪些出现过,出现了多少次
    在用ed对子串进行标记。 查询过程中遇到ed则对在相应的子串上 +1
    
    hhh-2016-04-23 21:08:09
    */
    #include <iostream>
    #include <vector>
    #include <cstring>
    #include <string>
    #include <cstdio>
    #include <queue>
    #include <algorithm>
    #include <functional>
    #include <map>
    using namespace std;
    #define lson  (i<<1)
    #define rson  ((i<<1)|1)
    typedef long long ll;
    typedef unsigned int ul;
    const int  maxn = 40010;
    const int mod = 10007;
    int ans[1005];
    int tot;
    struct Tire
    {
        int nex[1000*55][130],fail[1000*55],ed[1000*55];
        int root,L;
        int newnode()
        {
            for(int i = 0; i < 130; i++)
                nex[L][i] = -1;
            ed[L++] = 0;
            return L-1;
        }
    
        void ini()
        {
            L = 0,root = newnode();
        }
    
        void inser(char buf[],int id)
        {
            int len = strlen(buf);
            int now = root;
            for(int i = 0; i < len; i++)
            {
                int ta = buf[i];
                if(nex[now][ta] == -1)
                    nex[now][ta] = newnode();
                now = nex[now][ta];
            }
            ed[now] = id;
        }
    
        void build()
        {
            queue<int >q;
            fail[root] = root;
            for(int i = 0; i < 130; i++)
                if(nex[root][i] == -1)
                    nex[root][i] = root;
                else
                {
                    fail[nex[root][i]] = root;
                    q.push(nex[root][i]);
                }
            while(!q.empty())
            {
                int now = q.front();
                q.pop();
                for(int i = 0; i < 130; i++)
                {
                    if(nex[now][i] == -1)
                        nex[now][i] = nex[fail[now]][i];
                    else
                    {
                        fail[nex[now][i]] = nex[fail[now]][i];
                        q.push(nex[now][i]);
                    }
                }
            }
        }
    
        void query(char buf[])
        {
            tot = 0;
            int cur = root;
            int len = strlen(buf);
            for(int i = 0; i < len; i++)
            {
                cur = nex[cur][(int)buf[i]];
                int tp = cur;
    
                while(tp != root)
                {
                    if(ed[tp])
                        ans[ed[tp]]++;
                    tp = fail[tp];
                }
            }
        }
    };
    
    Tire ac;
    char buf[1005][55];
    char to[2000100];
    int main()
    {
        int n;
        while(scanf("%d",&n) != EOF)
        {
            ac.ini();
            for(int i = 1; i <= n; i++)
            {
                scanf("%s",buf[i]);
                ac.inser(buf[i],i);
            }
            ac.build();
            memset(ans,0,sizeof(ans));
            scanf("%s",to);
            ac.query(to);
    
            for(int i = 1; i <= n; i++)
            {
                if(ans[i] > 0)
                {
                    printf("%s: %d
    ",buf[i],ans[i]);
                }
            }
        }
        return 0;
    }
    

      

  • 相关阅读:
    JS中利用正则表达式提取一个字符串中的子字符串的方法
    Xcode的环境变量列表
    在未安装Visual Studio 2012的服务器上使用MSBuild以文件系统方式发布ASP.NET MVC系统
    使Web API支持二级实体操作,兼对RESTFul风格API设计的疑惑。
    忽略大小写的字符串包含测试
    Entity Framework里不用查询直接更新的办法
    iOS里生成灰化(黑白)图像
    微信小程序开发调试工具
    微信小程序产品定位及功能介绍
    微信小程序DEMO初体验
  • 原文地址:https://www.cnblogs.com/Przz/p/5449297.html
Copyright © 2011-2022 走看看