HDU 3065 病毒侵袭持续中(AC自己主动机)

题目链接:http://acm.hdu.edu.cn/showproblem.php?

pid=3065


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

代码例如以下:

#include <stdio.h>
#include <algorithm>
#include <iostream>
#include <string.h>
#include <queue>
#include <string>
using namespace std;
char ss[1017][57];
int ans[1017];
struct Trie
{
    int next[500010][128],fail[500010],end[500010];
    int root,L;
    int newnode()
    {
        for(int i = 0; i < 128; i++)
            next[L][i] = -1;
        end[L++] = -1;
        return L-1;
    }
    void init()
    {
        L = 0;
        root = newnode();
    }
    void insert(char buf[], int id)
    {
        int len = strlen(buf);
        int now = root;
        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()
    {
        queue<int>Q;
        fail[root] = root;
        for(int i = 0; i < 128; i++)
            if(next[root][i] == -1)
                next[root][i] = root;
            else
            {
                fail[next[root][i]] = root;
                Q.push(next[root][i]);
            }
        while(!Q.empty())
        {
            int now = Q.front();
            Q.pop();
            for(int i = 0; i < 128; i++)
                if(next[now][i] == -1)
                    next[now][i] = next[fail[now]][i];
                else
                {
                    fail[next[now][i]]=next[fail[now]][i];
                    Q.push(next[now][i]);
                }
        }
    }
    void query(char buf[], int n)
    {
        memset(ans,0,sizeof(ans));
        int len = strlen(buf);
        int now = root;
        for(int i = 0; i < len; i++)
        {
            now = next[now][buf[i]];
            int temp = now;
            while( temp != root )
            {
                //res += end[temp];
                //printf("temp:%d end[temp]:%d
",temp,end[temp]);
                if(end[temp] != -1)
                {
                    ans[end[temp]]++;
                }
                //end[temp] = 0;
                temp = fail[temp];
            }
        }
    }
};
char buf[2000010];
Trie ac;
int main()
{
    int t, n;
    while(~scanf("%d",&n))
    {
        ac.init();
        for(int i = 0; i < n; i++)
        {
            scanf("%s",ss[i]);
            ac.insert(ss[i],i);
        }
        ac.build();//fail
        scanf("%s",buf);
        ac.query(buf, n);
        for(int i = 0; i < n; i++)
        {
            if(ans[i])
            {
                printf("%s: %d
",ss[i],ans[i]);
            }
        }
    }
    return 0;
}


原文地址:https://www.cnblogs.com/slgkaifa/p/6882784.html