现在的位置: 首页 > 综合 > 正文

hdu3065—病毒侵袭持续中(AC自动机)

2019年02月13日 ⁄ 综合 ⁄ 共 2783字 ⁄ 字号 评论关闭

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
2009 Multi-University Training Contest 16 - Host by NIT

Recommend
lcy | We have carefully selected several similar problems for you: 2243 2825 3247 3341 2296

模版题

/*************************************************************************
    > File Name: hdu3065.cpp
    > Author: ALex
    > Mail: zchao1995@gmail.com 
    > Created Time: 2015年03月02日 星期一 14时28分04秒
 ************************************************************************/

#include <map>
#include <set>
#include <queue>
#include <stack>
#include <vector>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;

const double pi = acos(-1);
const int inf = 0x3f3f3f3f;
const double eps = 1e-15;
typedef long long LL;
typedef pair <int, int> PLL;

const int MAX_NODE = 50010;
const int CHILD_NUM = 128;

char str[1010][52];
char buf[2000010];
int num[1010];

struct AC_Automation
{
    int next[MAX_NODE][CHILD_NUM];
    int fail[MAX_NODE];
    int end[MAX_NODE];
    int root, L;

    int newnode ()
    {
        for (int i = 0; i < CHILD_NUM; ++i)
        {
            next[L][i] = -1;
        }
        end[L++] = 0;
        return L - 1;
    }

    void init ()
    {
        L = 0;
        root = newnode();
    }

    void Build_Trie (char buf[], int id)
    {
        int now = root;
        int len = strlen (buf);
        for (int i = 0; i < len; ++i)
        {
            if (next[now][buf[i]] == -1)
            {
                next[now][buf[i]] = newnode();
            }
            now = next[now][buf[i]];
        }
        end[now] = id;
    }

    void Build_AC ()
    {
        queue <int> qu;
        fail[root] = root;
        for (int i = 0; i < CHILD_NUM; ++i)
        {
            if (next[root][i] == -1)
            {
                next[root][i] = root;
            }
            else
            {
                fail[next[root][i]] = root;
                qu.push (next[root][i]);
            }
        }
        while (!qu.empty())
        {
            int now = qu.front();
            qu.pop();
            for (int i = 0; i < CHILD_NUM; ++i)
            {
                if (next[now][i] == -1)
                {
                    next[now][i] = next[fail[now]][i];
                }
                else
                {
                    fail[next[now][i]] = next[fail[now]][i];
                    qu.push(next[now][i]);
                }
            }
        }
    }

    void MATCH (char buf[])
    {
        int now = root;
        int len = strlen (buf);
        for (int i = 0; i < len; ++i)
        {
            now = next[now][buf[i]];
            int tmp = now;
            while (tmp != root)
            {
                if (end[tmp])
                {
                    ++num[end[tmp]];
                }
                tmp = fail[tmp];
            }
        }
    }
}AC;

int main ()
{
    int n;
    while (~scanf("%d", &n))
    {
        AC.init();
        for (int i = 1; i <= n; ++i)
        {
            scanf("%s", str[i]);
            AC.Build_Trie(str[i], i);
        }
        AC.Build_AC();
        getchar();
        memset (num, 0, sizeof(num));
        gets(buf);
        AC.MATCH(buf);
        for (int i = 1; i <= n; ++i)
        {
            if (num[i])
            {
                printf("%s: %d\n", str[i], num[i]);
            }
        }
    }
    return 0;
}

抱歉!评论已关闭.