hdu2896 病毒肆虐【AC自动机】

病毒侵袭

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


Problem Description
当太阳的光辉逐渐被月亮遮蔽,世界失去了光明,大地迎来最黑暗的时刻。。。。在这样的时刻,人们却异常兴奋——我们能在有生之年看到500年一遇的世界奇观,那是多么幸福的事儿啊~~
但网路上总有那么些网站,开始借着民众的好奇心,打着介绍日食的旗号,大肆传播病毒。小t不幸成为受害者之一。小t如此生气,他决定要把世界上所有带病毒的网站都找出来。当然,谁都知道这是不可能的。小t却执意要完成这不能的任务,他说:“子子孙孙无穷匮也!”(愚公后继有人了)。
万事开头难,小t收集了好多病毒的特征码,又收集了一批诡异网站的源码,他想知道这些网站中哪些是有病毒的,又是带了怎样的病毒呢?顺便还想知道他到底收集了多少带病毒的网站。这时候他却不知道何从下手了。所以想请大家帮帮忙。小t又是个急性子哦,所以解决问题越快越好哦~~
 
Input
第一行,一个整数N(1<=N<=500),表示病毒特征码的个数。
接下来N行,每行表示一个病毒特征码,特征码字符串长度在20—200之间。
每个病毒都有一个编号,依此为1—N。
不同编号的病毒特征码不会相同。
在这之后一行,有一个整数M(1<=M<=1000),表示网站数。
接下来M行,每行表示一个网站源码,源码字符串长度在7000—10000之间。
每个网站都有一个编号,依此为1—M。
以上字符串中字符都是ASCII码可见字符(不包括回车)。
 
Output
依次按如下格式输出按网站编号从小到大输出,带病毒的网站编号和包含病毒编号,每行一个含毒网站信息。
web 网站编号: 病毒编号 病毒编号 …
冒号后有一个空格,病毒编号按从小到大排列,两个病毒编号之间用一个空格隔开,如果一个网站包含病毒,病毒数不会超过3个。
最后一行输出统计信息,如下格式
total: 带病毒网站数
冒号后有一个空格。
 
Sample Input
3 aaa bbb ccc 2 aaabbbccc bbaacc
 
Sample Output
web 1: 1 2 3 total: 1
 
Source
 
Recommend
gaojie   |   We have carefully selected several similar problems for you:  3065 2243 2825 3341 3247 

题意:

给定n个模式串,m个文本串。问每个文本串中有多少个模式串出现过,输出他们的编号。最后输出所有含有模式串的文本串个数。

思路:

暴力跑AC自动机。有一些坑点。

第一是没看到说字符是ASC码中的字符,刚开始就开了26.看题解说开256会MLE,开130左右就够了。

第二是对每个文本串进行统计时,用了一个vis数组。每一次查询一个文本串,vis应该要重新初始化。

第三是输入的字符串中会有空格,要用gets

AC自动机的题目一定要注意对每个节点进行初始化,细节方面要考虑好。

好像今天有点浮躁,写题目很草率啊。

  1 #include <iostream>
  2 #include <set>
  3 #include <cmath>
  4 #include <stdio.h>
  5 #include <cstring>
  6 #include <algorithm>
  7 #include <vector>
  8 #include <queue>
  9 #include <map>
 10 //#include <bits/stdc++.h>
 11 using namespace std;
 12 typedef long long LL;
 13 #define inf 0x7f7f7f7f
 14 
 15 int m, n;
 16 const int maxn = 555;
 17 const int maxlen = 500 * 200 + 50;
 18 
 19 struct tree{
 20     int fail;
 21     int son[130];
 22     int ed;
 23     bool vis;
 24 }AC[maxlen];
 25 int tot = 0;
 26 char s[maxlen];
 27 
 28 void build(char s[], int id)
 29 {
 30     int len = strlen(s);
 31     int now = 0;
 32     for(int i = 0; i < len; i++){
 33         if(AC[now].son[s[i]] == 0){
 34             AC[now].son[s[i]] = ++tot;
 35         }
 36         now = AC[now].son[s[i]];
 37     }
 38     AC[now].ed = id;
 39 }
 40 
 41 void get_fail()
 42 {
 43     queue<int>que;
 44     for(int i = 0; i < 130; i++){
 45         if(AC[0].son[i] != 0){
 46             AC[AC[0].son[i]].fail = 0;
 47             que.push(AC[0].son[i]);
 48         }
 49     }
 50     while(!que.empty()){
 51         int u = que.front();
 52         que.pop();
 53         for(int i = 0; i < 130; i++){
 54             if(AC[u].son[i] != 0){
 55                 AC[AC[u].son[i]].fail = AC[AC[u].fail].son[i];
 56                 que.push(AC[u].son[i]);
 57             }
 58             else{
 59                 AC[u].son[i] = AC[AC[u].fail].son[i];
 60             }
 61         }
 62     }
 63 }
 64 
 65 int ans[5], top = 0;
 66 int AC_query(char s[])
 67 {
 68     for(int i = 0; i <= tot; i++){
 69         AC[i].vis = false;
 70     }
 71     int len = strlen(s);
 72     int now = 0, cnt = 0;
 73     for(int i = 0; i < len; i++){
 74         now = AC[now].son[s[i]];
 75         for(int t = now; t; t = AC[t].fail){
 76             if(!AC[t].vis && AC[t].ed != 0){
 77                 ans[top] = AC[t].ed;
 78                 //cout<<ans[top]<<endl;
 79                 top++;
 80                 cnt++;
 81                 AC[t].vis = true;
 82                 if(cnt >= 3)return cnt;
 83             }
 84         }
 85     }
 86     return cnt;
 87 }
 88 
 89 int main()
 90 {
 91     while(scanf("%d", &n) != EOF){
 92         for(int i = 0; i <= tot; i++){
 93             AC[i].ed = 0;
 94             AC[i].fail = 0;
 95             AC[i].vis = false;
 96             for(int j = 0; j < 130; j++){
 97                 AC[i].son[j] = 0;
 98             }
 99         }
100         tot = 0;
101         for(int i = 1; i <= n; i++){
102             getchar();
103             gets(s);
104             build(s, i);
105         }
106         AC[0].fail = 0;
107         get_fail();
108         scanf("%d", &m);
109         int num = 0;
110         for(int i = 1; i <= m; i++){
111             getchar();
112             gets(s);
113             top = 0;
114             if(AC_query(s)){
115                 printf("web %d:", i);
116                 sort(ans, ans + top);
117                 for(int j = 0; j < top; j++){
118                     printf(" %d", ans[j]);
119                 }
120                 printf("
");
121                 num++;
122             }
123         }
124 
125         printf("total: %d
", num);
126     }
127 
128     return 0;
129 }
原文地址:https://www.cnblogs.com/wyboooo/p/9897419.html