码迷,mamicode.com
首页 > 其他好文 > 详细

HDU 2222 Keywords Search【AC自动机】

时间:2018-03-25 12:05:26      阅读:190      评论:0      收藏:0      [点我收藏+]

标签:mes   自动机   amp   nbsp   自动   hdu   cst   ++   style   

自写的板子,新鲜出炉,所以不保证没BUG,慎用

  1 #include<iostream>
  2 using namespace std;
  3 #include<cstdio>
  4 #include<cstring>
  5 #include<queue>
  6 const int maxn = 26;
  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 struct Ac_Machine{
 23     Node *head;
 24     char hash(char s){//压缩 
 25         return s - a;
 26     }
 27     Ac_Machine(){
 28         head = NULL;
 29     }
 30     void init(){
 31         if(head!=NULL)
 32             delete head;
 33         head = new Node();
 34     }
 35     void insert(char *s,int len){
 36         Node *pt = head;
 37         for(int i=0;i<len;i++){
 38             int k = hash(s[i]);
 39             if(pt->next[k]==NULL){
 40                 pt->next[k] = new Node();
 41                 pt->next[k]->fail = head;
 42             }
 43             pt = pt->next[k];
 44         }
 45         pt->mark+=1;
 46     }
 47     void creat_fail(){
 48         queue<Node*> *team = new queue<Node*>();
 49         head->fail = NULL;
 50         for(int i=0;i<maxn;i++)
 51             if(head->next[i]!=NULL)
 52                 team->push(head->next[i]);
 53         while(!team->empty()){
 54             Node *pt = team->front();
 55             team->pop();
 56             for(int i=0;i<maxn;i++){
 57                 if(pt->next[i]==NULL)
 58                     continue;
 59                 team->push(pt->next[i]);
 60                 Node *pd = pt->fail;
 61                 while(pd!=NULL&&pd->next[i]==NULL)
 62                     pd = pd->fail;
 63                 if(pd!=NULL)
 64                     pt->next[i]->fail = pd->next[i];
 65             }    
 66         } 
 67         delete team;
 68     }
 69     int Matching(char *s,int len){
 70         creat_fail();
 71         Node *pt = head;
 72         int ans = 0;
 73         for(int i=0;i<len;i++){
 74             int k = hash(s[i]);
 75             while(pt!=head&&pt->next[k]==NULL)
 76                 pt = pt->fail;
 77             //cout<<s[i]<<" "<<pt<<" "<<head<<" "<<pt->fail<<endl;;
 78             if(pt->next[k]!=NULL){
 79             //    cout<<pt<<endl;
 80                 pt = pt->next[k];
 81                 Node *pd = pt;
 82                 while(pd!=head&&pd->mark!=-1){
 83                 //    cout<<" "<<pd<<"\tchar:"<<pd->mark<<" "<<pd->fail<<endl;
 84                     ans += pd->mark;
 85                     pd->mark = -1;
 86                     pd = pd->fail;
 87                 }
 88             }
 89         }
 90         return ans;
 91     }
 92 };
 93 Ac_Machine machine;
 94 char s[2000000];
 95 void deal(){
 96     int n;
 97     machine.init();
 98     scanf("%d",&n);
 99     for(int i=0;i<n;i++){
100         scanf("%s",s);
101         machine.insert(s,strlen(s));
102     }
103     scanf("%s",s);
104     int ans = machine.Matching(s,strlen(s));
105     printf("%d\n",ans);
106 }
107 int main(){
108     int t;
109     scanf("%d",&t);
110     while(t--){
111         deal();
112     }
113     return 0;
114 }

 

HDU 2222 Keywords Search【AC自动机】

标签:mes   自动机   amp   nbsp   自动   hdu   cst   ++   style   

原文地址:https://www.cnblogs.com/xfww/p/8642928.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!