ac自动机膜板

Posted mzh2017

tags:

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

技术分享图片
  1 #include<bits/stdc++.h>
  2 using namespace std;
  3 typedef long long LL;
  4 typedef unsigned long long ULL;
  5 #define MAXN 26
  6 #define L 31
  7 #define INF 1000000009
  8 #define eps 0.00000001
  9 struct node
 10 {
 11     node()
 12     {
 13         cnt = 0;
 14         fail = NULL;
 15         for (int i = 0; i < MAXN; i++)
 16             next[i] = NULL;
 17     }
 18     int cnt;
 19     struct node* next[MAXN];
 20     struct node* fail;
 21 };
 22 typedef struct node* Tree;
 23 void insert(Tree T, const char* str)
 24 {
 25     int p = 0;
 26     Tree tmp = T;
 27     while (str[p] != )
 28     {
 29         int k = str[p] - a;
 30         if (tmp->next[k] == NULL)
 31         {
 32             tmp->next[k] = new node();
 33         }
 34         tmp = tmp->next[k];
 35         p++;
 36     }
 37     tmp->cnt++;
 38 }
 39 void build_ac(Tree root)
 40 {
 41     root->fail = NULL;
 42     queue<Tree> q;
 43     q.push(root);
 44     while (!q.empty())
 45     {
 46         Tree tmp = q.front();
 47         q.pop();
 48         Tree p = NULL;
 49         for (int i = 0; i < MAXN; i++)
 50         {
 51             if (tmp->next[i] != NULL)
 52             {
 53                 if (tmp == root)
 54                     tmp->next[i]->fail = root;
 55                 else
 56                 {
 57                     p = tmp->fail;
 58                     while (p != NULL)
 59                     {
 60                         if (p->next[i] != NULL)
 61                         {
 62                             tmp->next[i]->fail = p->next[i];
 63                             break;
 64                         }
 65                         p = p->fail;
 66                     }
 67                     if (p == NULL) tmp->next[i]->fail = root;
 68                 }
 69                 q.push(tmp->next[i]);
 70             }
 71         }
 72     }
 73 }
 74 int query(Tree root,const char* str)
 75 {
 76     int i = 0;
 77     int count = 0;
 78     int l = strlen(str);
 79     Tree tmp = root;
 80     int index;
 81     while (str[i])
 82     {
 83         index = str[i] - a;
 84         while (tmp->next[index] == NULL && tmp != root)
 85             tmp = tmp->fail;
 86         tmp = tmp->next[index];
 87         if (tmp == NULL) tmp = root;
 88         Tree p = tmp;
 89         while (p != root)
 90         {
 91             count += p->cnt;
 92             p->cnt = 0;
 93             p = p->fail;
 94         }
 95         i++;
 96     }
 97     return count;
 98 }
 99  
100 void del(Tree root)
101 {
102     for (int i = 0; i < MAXN; i++)
103         if (root->next[i] != NULL)
104             del(root->next[i]);
105     delete root;
106 }
107 char key[60], s[1000010];
108 int main()
109 {
110     int T=1,n;
111     while (T--)
112     {
113         scanf("%d", &n);
114         Tree root = new node();
115         root->cnt = 0;
116         root->fail = NULL;
117         for (int i = 0; i < MAXN; i++)
118             root->next[i] = NULL;
119         for (int i = 0; i < n; i++)
120         {
121             scanf("%s", key);
122             insert(root, key);
123         }
124         build_ac(root);
125         scanf("%s", s);
126         printf("%d
", query(root, s));
127         del(root);
128     }
129 }
ac

 

以上是关于ac自动机膜板的主要内容,如果未能解决你的问题,请参考以下文章

HDU3247 Resource Archiver(AC自动机+BFS+DP)

POJ3691DNA repair(AC自动机,DP)

HDU4057 Rescue the Rabbit(AC自动机+状压DP)

Codeforces 86C Genetic engineering(AC自动机+DP)

POJ1699 Best Sequence(AC自动机+状压DP)

POJ - 2778 ~ HDU - 2243 AC自动机+矩阵快速幂