zoukankan      html  css  js  c++  java
  • Hdu3065 病毒侵袭持续中

    病毒侵袭持续中

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


    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
     
    Sample Output
    AA: 2 CC: 1
    Hint
    Hit: 题目描述中没有被提及的所有情况都应该进行考虑。比如两个病毒特征码可能有相互包含或者有重叠的特征码段。 计数策略也可一定程度上从Sample中推测。
     
    Source
     
    Recommend
    lcy   |   We have carefully selected several similar problems for you:  2222 2896 2243 2825 3247 
    分析:比较简单的AC自动机题.每次将跳到的点的答案+1即可.比较坑的一点是非大写字母的处理,遇到非大写字母都要跳回根节点.不能再预处理的时候删掉非大写字母,这样可能会将本来分开的两个字符串拼在一起.这题还是多组数据......
    #include <cstdio>
    #include <queue>
    #include <cstring>
    #include <iostream>
    #include <algorithm>
    
    using namespace std;
    
    int n, tot = 1, ans[53010], vis[53010], len;
    char s[50010][60], s2[2000010],s3[2000010];
    
    struct node
    {
        int tr[30], id, fail;
        void clear()
        {
            memset(tr, 0, sizeof(tr));
            id = fail = 0;
        }
    }e[200000];
    
    void insert(char *ss,int x)
    {
        int len = strlen(ss);
        int u = 1;
        for (int i = 0; i < len; i++)
        {
            int ch = ss[i] - 'A';
            if (!e[u].tr[ch])
            {
                e[u].tr[ch] = ++tot;
                e[tot].clear();
            }
            u = e[u].tr[ch];
        }
        e[u].id = x;
    }
    
    void build()
    {
        for (int i = 0; i < 26; i++) 
            e[0].tr[i] = 1;
        queue <int> q;
        q.push(1);
        while (!q.empty())
        {
            int u = q.front();
            q.pop();
            int fail = e[u].fail;
            for (int i = 0; i < 26; i++)
            {
                int y = e[u].tr[i];
                if (y)
                {
                    e[y].fail = e[fail].tr[i];
                    q.push(y);
                }
                else
                    e[u].tr[i] = e[fail].tr[i];
            }
        }
    }
    
    void solve()
    {
        int u = 1;
        for (int i = 1; i <= len; i++)
        {
            if (s2[i] >= 'A' && s2[i] <= 'Z')
            {
                int ch = s2[i] - 'A';
                while (u && !e[u].tr[ch])
                    u = e[u].fail;
                u = e[u].tr[ch];
                int temp = u;
                while (temp != 1)
                {
                    ans[e[temp].id]++;
                    temp = e[temp].fail;
                }
            }
            else
                u = 1;
        }
    }
    
    int main()
    {
        while (scanf("%d", &n) != EOF)
        {
            memset(ans, 0, sizeof(ans));
            e[tot = 1].clear();
            len = 0;
            for (int i = 1; i <= n; i++)
            {
                scanf("%s", s[i]);
                insert(s[i], i);
            }
            build();
            getchar();
            scanf("%s", s2 + 1);
            len = strlen(s2 + 1);
            solve();
            for (int i = 1; i <= n; i++)
                if (ans[i])
                    printf("%s: %d
    ", s[i], ans[i]);
        }
    
        return 0;
    }
  • 相关阅读:
    VS2019调试 asp.net core 2.2 出现《ANCM In-Process Handler Load Failure 发布后启动错误处理》处理
    网页上显示数学公式的三种方案
    FileStream实现多线程断点续传(已封装)
    绝对定位不脱离文档流的方法
    百度地图InfoWindow弹窗圆角
    并发:线程池异步执行与创建单独的线程执行
    互斥锁和自旋锁的区别
    事务的特性和隔离级别
    线程不安全与线程安全示例
    多线程过去与现在
  • 原文地址:https://www.cnblogs.com/zbtrs/p/8057959.html
Copyright © 2011-2022 走看看