BNUOJ 7178 病毒侵袭持续中

病毒侵袭持续中

Time Limit: 1000ms
Memory Limit: 32768KB
This problem will be judged on HDU. Original ID: 3065
64-bit integer IO format: %I64d      Java class name: Main
 
小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

 
 
解题:AC自动机。。。。。
 
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cmath>
 5 #include <algorithm>
 6 #include <climits>
 7 #include <vector>
 8 #include <queue>
 9 #include <cstdlib>
10 #include <string>
11 #include <set>
12 #include <stack>
13 #define LL long long
14 #define INF 0x3f3f3f3f
15 using namespace std;
16 struct trie{
17     int wd[28],fail,index;
18     void init(){
19         memset(wd,0,sizeof(wd));
20         fail = index = 0;
21     }
22 };
23 trie dic[100000];
24 int tot,n,cnt[1010];
25 char word[1010][60];
26 char web[2001000];
27 void insertWd(int root,int id,char *s){
28     for(int i = 0; s[i]; i++){
29         int k = s[i]-'A';
30         if(!dic[root].wd[k])
31             dic[root].wd[k] = tot++;
32         root = dic[root].wd[k];
33     }
34     dic[root].index = id;
35 }
36 void build(int root){
37     queue<int>q;
38     q.push(root);
39     int u,v,i,j;
40     while(!q.empty()){
41         u = q.front();
42         q.pop();
43         for(i = 0; i < 28; i++){
44             if(!dic[u].wd[i]) continue;
45             if(!u) dic[dic[u].wd[i]].fail = 0;
46             else{
47                 v = dic[u].fail;
48                 while(v && !dic[v].wd[i]) v = dic[v].fail;
49                 if(dic[v].wd[i]) dic[dic[u].wd[i]].fail = dic[v].wd[i];
50                 else dic[dic[u].wd[i]].fail = 0;
51             }
52             q.push(dic[u].wd[i]);
53         }
54     }
55 }
56 void query(int root,char *s){
57     for(int i = 0; s[i]; i++){
58         int k = s[i]-'A';
59         if(k < 0 || k > 26) {root = 0;continue;}
60         while(root && !dic[root].wd[k]) root = dic[root].fail;
61         root = dic[root].wd[k];
62         if(root){
63             int v = root;
64             while(v && dic[v].index){
65                 cnt[dic[v].index]++;
66                 v = dic[v].fail;
67             }
68         }
69     }
70 }
71 int main() {
72     int i,root;
73     while(~scanf("%d",&n)){
74         tot = 1;
75         root = 0;
76         for(i = 0; i < 100000; i++)
77             dic[i].init();
78         memset(cnt,0,sizeof(cnt));
79         for(i = 1; i <= n; i++){
80             scanf("%s",word[i]);
81             insertWd(root,i,word[i]);
82         }
83         build(root);
84         scanf("%s",web);
85         query(root,web);
86         for(i = 1; i <= n; i++){
87             if(cnt[i]) printf("%s: %d
",word[i],cnt[i]);
88         }
89     }
90     return 0;
91 }
View Code
 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 const int maxn = 200010;
 4 int cnt[1010];
 5 char str[1010][55];
 6 struct Trie{
 7     int ch[maxn][26],fail[maxn],id[maxn],tot;
 8     int newnode(){
 9         memset(ch[tot],0,sizeof ch[tot]);
10         id[tot] = fail[tot] = 0;
11         return tot++;
12     }
13     void init(){
14         tot = 0;
15         newnode();
16     }
17     void insert(char *str,int index,int root = 0){
18         for(int i = 0; str[i]; ++i){
19             if(!ch[root][str[i]-'A']) ch[root][str[i]-'A'] = newnode();
20             root = ch[root][str[i]-'A'];
21         }
22         id[root] = index;
23     }
24     void build(int root = 0){
25         queue<int>q;
26         for(int i = 0; i < 26; ++i)
27             if(ch[root][i]) q.push(ch[root][i]);
28         while(!q.empty()){
29             root = q.front();
30             q.pop();
31             for(int i = 0; i < 26; ++i){
32                 if(ch[root][i]){
33                     fail[ch[root][i]] = ch[fail[root]][i];
34                     q.push(ch[root][i]);
35                 }else ch[root][i] = ch[fail[root]][i];
36             }
37         }
38     }
39     void query(char *str,int root = 0){
40         for(int i = 0; str[i]; ++i){
41             int k = str[i] - 'A';
42             if(k >= 26 || k < 0) {
43                 root = 0;
44                 continue;
45             }
46             int x = root = ch[root][k];
47             while(x){
48                 cnt[id[x]]++;
49                 x = fail[x];
50             }
51         }
52     }
53 }ac;
54 char ss[2000010];
55 int main(){
56     int n;
57     while(~scanf("%d",&n)){
58         ac.init();
59         for(int i = 1; i <= n; ++i){
60             scanf("%s",str[i]);
61             ac.insert(str[i],i);
62         }
63         ac.build();
64         memset(cnt,0,sizeof cnt);
65         scanf("%s",ss);
66         ac.query(ss);
67         for(int i = 1; i <= n; ++i)
68             if(cnt[i]) printf("%s: %d
",str[i],cnt[i]);
69     }
70     return 0;
71 }
View Code
原文地址:https://www.cnblogs.com/crackpotisback/p/3901120.html