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

【AC自动机】HDU中模板题汇总(待更新)

时间:2016-02-07 13:36:30      阅读:260      评论:0      收藏:0      [点我收藏+]

标签:

【HDU2222】

最纯粹的裸题,错误点详见注释。

  1 #include<iostream>
  2 #include<cstdio>
  3 #include<cstring>
  4 #include<algorithm>
  5 #include<queue>
  6 using namespace std;
  7 struct ACauto
  8 {
  9     int sum;
 10     ACauto* next[26];
 11     ACauto* fail;
 12     ACauto()
 13     {
 14         sum=0;
 15         for (int i=0;i<26;i++) next[i]=NULL;
 16         fail=NULL;
 17     }
 18 };
 19 
 20 void insert(ACauto* root,char* str)
 21 {
 22     int len=strlen(str);
 23     ACauto* now=root;
 24     for (int i=0;i<len;i++)
 25     {
 26         int index=str[i]-a;
 27         if (now->next[index]==NULL)
 28         {
 29             ACauto* tmp=new ACauto;
 30             now->next[index]=tmp;
 31         }
 32         now=now->next[index]; 
 33     }
 34     now->sum++;
 35 }
 36 
 37 void build(ACauto* root)
 38 {
 39     queue<ACauto*> que;
 40     que.push(root);
 41     while (!que.empty())
 42     {
 43         ACauto* tmp=que.front();que.pop();
 44         for (int i=0;i<26;i++)
 45         {
 46             if (tmp->next[i]==NULL) continue;
 47             if (tmp==root)
 48                 tmp->next[i]->fail=root;
 49             else
 50             {
 51                 ACauto* p=tmp->fail;
 52                 while (p!=NULL)
 53                 {
 54                     if (p->next[i]!=NULL)
 55                     {
 56                         tmp->next[i]->fail=p->next[i];
 57                         break;
 58                     }
 59                     p=p->fail;
 60                 }
 61                 if (p==NULL) tmp->next[i]->fail=root; 
 62             }
 63             que.push(tmp->next[i]);
 64         }
 65     }
 66 }
 67 
 68 int query(ACauto* root,char* str)
 69 { 
 70     int ans=0,len=strlen(str);
 71     ACauto* p=root;
 72     for (int i=0;i<len;i++)
 73     {
 74         int index=str[i]-a;
 75         while (p->next[index]==NULL && p!=root) p=p->fail;
 76         /*错误点:上述语句是while语句不是if语句*/ 
 77         p=p->next[index];
 78         p=(p==NULL)?root:p;
 79         ACauto* tmp=p;
 80         while (tmp!=root)
 81         /*-1表示这个单词之前已经被统计过了,不再重复计算*/ 
 82         {
 83             if (tmp->sum>=0)
 84             {
 85                 ans+=tmp->sum;
 86                 tmp->sum=-1;
 87             }
 88             else
 89             break;
 90             tmp=tmp->fail;
 91         }
 92     }
 93     return ans;
 94 }
 95 
 96 void submain()
 97 {
 98     ACauto* root=new ACauto;
 99     int n;
100     char str[1000000+50];
101     scanf("%d",&n);
102     for (int i=0;i<n;i++)
103     {
104         scanf("%s",str);
105         insert(root,str);
106     } 
107     build(root);
108     scanf("%s",str);
109     cout<<query(root,str)<<endl;
110 }
111 
112 int main() 
113 {
114     int T;
115     scanf("%d",&T);
116     for (int kase=0;kase<T;kase++) submain();
117     return 0;
118 } 

 

【AC自动机】HDU中模板题汇总(待更新)

标签:

原文地址:http://www.cnblogs.com/iiyiyi/p/5184589.html

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