hdu 2222(AC自动机)

Posted jaydenouyang

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了hdu 2222(AC自动机)相关的知识,希望对你有一定的参考价值。

  1 #include<bits/stdc++.h>
  2 using namespace std;
  3 const int maxn = 1e7 + 5;
  4 const int MAX = 10000000;
  5 int cnt;
  6 struct node{
  7     node *next[26];
  8     node *fail;
  9     int sum;
 10 };
 11 node *root;
 12 char key[70];
 13 node *q[MAX];
 14 int head,tail;
 15 node *newnode;
 16 char pattern[maxn];
 17 int N;
 18 void Insert(char *s)
 19 {
 20     node *p = root;
 21     for(int i = 0; s[i]; i++)
 22     {
 23         int x = s[i] - a;
 24         if(p->next[x] == NULL)
 25         {
 26             newnode=(struct node *)malloc(sizeof(struct node));
 27             for(int j=0;j<26;j++) newnode->next[j] = 0;
 28             newnode->sum = 0;newnode->fail = 0;
 29             p->next[x]=newnode;
 30         }
 31         p = p->next[x];
 32     }
 33     p->sum++;
 34 }
 35 void build_fail_pointer()
 36 {
 37     head = 0;
 38     tail = 1;
 39     q[head] = root;
 40     node *p;
 41     node *temp;
 42     while(head < tail)
 43     {
 44         temp = q[head++];
 45         for(int i = 0; i <= 25; i++)
 46         {
 47             if(temp->next[i])
 48             {
 49                 if(temp == root)
 50                 {
 51                     temp->next[i]->fail = root;
 52                 }
 53                 else
 54                 {
 55                     p = temp->fail;
 56                     while(p)
 57                     {
 58                         if(p->next[i])
 59                         {
 60                             temp->next[i]->fail = p->next[i];
 61                             break;
 62                         }
 63                         p = p->fail;
 64                     }
 65                     if(p == NULL) temp->next[i]->fail = root;
 66                 }
 67                 q[tail++] = temp->next[i];
 68             }
 69         }
 70     }
 71 }
 72 void ac_automation(char *ch)
 73 {
 74     node *p = root;
 75     int len = strlen(ch);
 76     for(int i = 0; i < len; i++)
 77     {
 78         int x = ch[i] - a;
 79         while(!p->next[x] && p != root) p = p->fail;
 80         p = p->next[x];
 81         if(!p) p = root;
 82         node *temp = p;
 83         while(temp != root)
 84         {
 85            if(temp->sum >= 0)
 86            {
 87                cnt += temp->sum;
 88                temp->sum = -1;
 89            }
 90            else break;
 91            temp = temp->fail;
 92         }
 93     }
 94 }
 95 int main()
 96 {
 97     int T;
 98     scanf("%d",&T);
 99     while(T--)
100     {
101         root=(struct node *)malloc(sizeof(struct node));
102         for(int j=0;j<26;j++) root->next[j] = 0;
103         root->fail = 0;
104         root->sum = 0;
105         scanf("%d",&N);
106         for(int i = 1; i <= N; i++)
107         {
108             scanf("%s",key);
109             Insert(key);
110         }
111         scanf("%s",pattern);
112         cnt = 0;
113         build_fail_pointer();
114         ac_automation(pattern);
115         printf("%d\n",cnt);
116     }
117     return 0;
118 }

大佬的博客:https://blog.csdn.net/creatorx/article/details/71100840

以上是关于hdu 2222(AC自动机)的主要内容,如果未能解决你的问题,请参考以下文章

HDU-2222-Keywords Search(AC自动机模板)

HDU 2222 Keywords Search(AC自动机入门)

hdu2222(ac自动机模板)

ac自动机基础模板(hdu2222)

hdu 2222 Keywords Search(ac自动机)

hdu2222 (AC自动机模板)