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

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/*
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\n",buf[i],ans[i]);
            }
        }
    }
    return 0;
}

  

posted @   Przz  阅读(908)  评论(0编辑  收藏  举报
努力加载评论中...
点击右上角即可分享
微信分享提示