hdu-2896 病毒侵袭 --------AC自动机

题意:给出m个模式和一个文本,求各种模式的出现次数

比较裸的AC自动机,成功匹配就计数就是了..

View Code
  1 //Accepted    2896    156MS    23516K    2412 B    C++
  2 
  3 #include <iostream>
  4 #include <cstring>
  5 #include <cstdio>
  6 #include <cmath>
  7 #include <algorithm>
  8 using namespace std;
  9 const int inf=1<<28;
 10 const int nMax=501;
 11 const int mMax=100010;
 12 bool vis[nMax];
 13 class node{
 14 public:
 15     int id;
 16     node *next[100],*fail;
 17     node(){
 18         id=0;
 19         fail=NULL;
 20         memset(next,0,sizeof(next));
 21     }
 22 }*root,*que[mMax];
 23 
 24 void insert(char *s,int id){  
 25     int i;
 26     node *r=root;
 27     int l=strlen(s);
 28     for(i=0;i<l;i++){
 29         int loc=s[i]-30;
 30         if(r->next[loc]==NULL){
 31             r->next[loc]=new node();
 32         }
 33         r=r->next[loc];
 34     }
 35     r->id = id;
 36 }
 37 
 38 void acAuto(){      //用bfs为每个节点设定fail指针
 39     int i,head=0,tail=0;
 40     node *p,*tmp;
 41     root->fail=NULL;
 42     que[tail++]=root;
 43     while(head<tail)
 44     {
 45         tmp=que[head++];
 46         for(i=0;i<100;i++){
 47             if(tmp->next[i]==NULL)continue;
 48             if(tmp==root){
 49                 tmp->next[i]->fail=root;
 50             }
 51             else {
 52                 for(p=tmp->fail;p!=NULL;p=p->fail){
 53                     if(p->next[i]!=NULL){
 54                         tmp->next[i]->fail=p->next[i];
 55                         break;
 56                     }
 57                 }
 58                 if(p==NULL){
 59                     tmp->next[i]->fail=root;
 60                 }
 61             }
 62             que[tail++]=tmp->next[i];
 63         }
 64     }
 65 }
 66 
 67 void search(char msg[])
 68 {
 69     int i,idx,ans=0;
 70     node *p=root,*tmp;
 71     for(i=0;msg[i];i++)
 72     {
 73         idx=msg[i]-30;
 74         while(p->next[idx]==NULL&&p!=root)
 75         {
 76             p=p->fail;
 77         }
 78         if(p->next[idx])
 79             p=p->next[idx];
 80        
 81         for(tmp=p;tmp!=root;tmp=tmp->fail)
 82         {    
 83             if(tmp->id)
 84                 vis[tmp->id] = true;
 85         }
 86     }
 87 }
 88 
 89 int main(){
 90     int i,j,cas,n;
 91     int total = 0;
 92     int num;
 93     char str[205],text[10010];    
 94     scanf("%d",&n);
 95     root=new node();
 96     for(i=1;i<=n;i++)
 97     {
 98         scanf("%s",str);
 99         insert(str,i);
100     }
101     acAuto();
102     scanf("%d",&cas);
103     for(i=1;i<=cas;i++)
104     {
105         scanf("%s",text);
106         num = 0;
107         memset(vis,0,sizeof(vis));
108         search(text);
109         for(j=1;j<=n;j++)
110             num += vis[j];
111         if(num)
112         {
113             total++;
114             printf("web %d:",i);
115             for(int j=1;j<=n;j++)
116                 if(vis[j])
117                     printf(" %d",j);
118             printf("\n");
119         }
120     }
121     printf("total: %d\n",total);
122     return 0;
123 }

 

 

posted @ 2012-08-22 20:17  Wheat″  阅读(247)  评论(0)    收藏  举报