HDU - 2896 病毒侵袭 【AC自动机模板*sp1】

当太阳的光辉逐渐被月亮遮蔽,世界失去了光明,大地迎来最黑暗的时刻。。。。在这样的时刻,人们却异常兴奋——我们能在有生之年看到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

做法:
AC自动机模板稍微变一下型
将字典树的节点编号设置为对应字符串的编号
然后匹配时标记编号即可

坑点:
用指针建树各种MLE啊,所以指针建树的代码必须交C++
而且有一个优化就是 0 - 31 不是可视化字符 所以并不需要建next指针数组为128大小的节点

代码:
  1 #include<iostream>
  2 using namespace std;
  3 #include<cstdio>
  4 #include<cstring>
  5 #include<queue>
  6 const int maxn = 127-32;
  7 struct Node{
  8     int mark;
  9     Node *next[maxn];
 10     Node *fail;
 11     Node(){
 12         mark = 0;
 13         for(int i=0;i<maxn;i++)
 14             next[i] = NULL;
 15     }
 16     ~Node(){
 17         for(int i=0;i<maxn;i++)
 18             if(next[i]!=NULL)
 19                 delete next[i];
 20     }
 21 };
 22 int v[600];
 23 struct Ac_Machine{
 24     Node *head;
 25     char hash(char &s){//压缩 
 26         return s-32;
 27     }
 28     Ac_Machine(){
 29         head = NULL;
 30     }
 31     void init(){
 32         if(head!=NULL)
 33             delete head;
 34         head = new Node();
 35     }
 36     void insert(char *s,int len,int num){
 37         Node *pt = head;
 38         for(int i=0;i<len;i++){
 39             int k = hash(s[i]);
 40             if(pt->next[k]==NULL){
 41                 pt->next[k] = new Node();
 42                 pt->next[k]->fail = head;
 43             }
 44             pt = pt->next[k];
 45         }
 46         pt->mark=num;
 47     }
 48     void creat_fail(){
 49         queue<Node*> *team = new queue<Node*>();
 50         head->fail = NULL;
 51         for(int i=0;i<maxn;i++)
 52             if(head->next[i]!=NULL)
 53                 team->push(head->next[i]);
 54         while(!team->empty()){
 55             Node *pt = team->front();
 56             team->pop();
 57             for(int i=0;i<maxn;i++){
 58                 if(pt->next[i]==NULL)
 59                     continue;
 60                 team->push(pt->next[i]);
 61                 Node *pd = pt->fail;
 62                 while(pd!=NULL&&pd->next[i]==NULL)
 63                     pd = pd->fail;
 64                 if(pd!=NULL)
 65                     pt->next[i]->fail = pd->next[i];
 66             }    
 67         } 
 68         delete team;
 69     }
 70     int Matching(char *s,int len){
 71       //  creat_fail();
 72         Node *pt = head;
 73         int ans = 0;
 74         for(int i=0;i<len;i++){
 75             int k = hash(s[i]);
 76             while(pt!=head&&pt->next[k]==NULL)
 77                 pt = pt->fail;
 78             if(pt->next[k]!=NULL){
 79                 pt = pt->next[k];
 80                 Node *pd = pt;
 81                 while(pd!=head&&v[pd->mark]!=1){
 82                     if(pd->mark!=0&&v[pd->mark]==0){
 83                         v[pd->mark] = 1;
 84                        }    
 85                        pd = pd->fail;
 86                 }
 87             }
 88         }
 89         return 0;
 90     }
 91 };
 92 Ac_Machine machine;
 93 char virus[510][210];
 94 char web_code[11000];
 95 queue<int> ans;
 96 int deal(){
 97     int n;
 98     if(scanf("%d",&n)==EOF)
 99         return 0;
100     machine.init();
101     for(int i=1;i<=n;i++){
102         scanf("%s",virus[i]);
103         machine.insert(virus[i],strlen(virus[i]),i);
104     }
105     int m;
106     machine.creat_fail();
107     scanf("%d",&m);
108     int num = 0;
109     for(int i=1;i<=m;i++){
110         scanf("%s",web_code);
111         memset(v,0,sizeof(v));
112         machine.Matching(web_code,strlen(web_code));
113         while(!ans.empty())
114             ans.pop();
115         for(int j=1;j<=n;j++)
116             if(v[j])
117                 ans.push(j);
118         if(ans.empty())
119             continue;
120         printf("web %d:",i);
121         while(!ans.empty()){
122             printf(" %d",ans.front());
123             ans.pop();
124         }
125         num+=1; 
126         putchar('\n');
127     }
128     printf("total: %d\n",num);
129     return 1;
130 }
131 int main(){
132     while(deal());
133     return 0;
134 }

 

 
posted @ 2018-03-25 09:38  晓风微微  阅读(195)  评论(0编辑  收藏  举报