AC自动机专题

AC自动机

 1 /*
 2   AC自动机模板
 3 */
 4 struct AC   {
 5     int ch[MAXNODE][SIZE], fail[MAXNODE], val[MAXNODE], sz;
 6     void init(void) {
 7         memset (ch[0], 0, sizeof (ch[0]));
 8         sz = 1; val[0] = 0;
 9     }
10     int idx(char c) {
11         return c - ‘a‘;
12     }
13     void insert(char *P)    {
14         int u = 0;
15         for (int i=0; P[i]; ++i)   {
16             int c = idx (P[i]);
17             if (!ch[u][c])  {
18                 memset (ch[sz], 0, sizeof (ch[sz]));
19                 ch[u][c] = sz;  val[sz++] = 0;
20             }
21             u = ch[u][c];
22         }
23         val[u]++;
24     }
25     void get_fail(void) {
26         queue<int> Q;   fail[0] = 0;
27         for (int i=0; i<SIZE; ++i)  {
28             int u = ch[0][i];
29             if (u)  {
30                 fail[u] = 0;    Q.push (u);
31             }
32         }
33         while (!Q.empty ()) {
34             int u = Q.front (); Q.pop ();
35             for (int i=0; i<SIZE; ++i)  {
36                 int &v = ch[u][i];
37                 if (!v) {
38                     v = ch[fail[u]][i];  continue;
39                 }
40                 Q.push (v);
41                 fail[v] = ch[fail[u]][i];   //val[v] += val[fail[u]];
42             }
43         }
44     }
45     int query(char *T)  {
46         int ret = 0;
47         for (int u=0, i=0; T[i]; ++i)   {
48             int c = idx (T[i]);
49             u = ch[u][c];
50             ret += val[u];
51             int tmp = u;
52             while (tmp) {
53                 ret += val[tmp];    val[tmp] = 0;
54                 tmp = fail[tmp];
55             }
56         }
57         return ret;
58     }
59 }ac;
时间: 2024-10-11 11:00:29

AC自动机专题的相关文章

AC自动机 专题

1 // 求目标串中出现了几个模式串 2 //==================== 3 #include <stdio.h> 4 #include <algorithm> 5 #include <iostream> 6 #include <string.h> 7 #include <queue> 8 using namespace std; 9 10 struct Trie 11 { 12 int next[500010][26],fail[

转自kuangbin的AC自动机(赛前最后一博)

有了KMP和Trie的基础,就可以学习神奇的AC自动机了.AC自动机其实就是在Trie树上实现KMP,可以完成多模式串的匹配.           AC自动机 其实 就是创建了一个状态的转移图,思想很重要.           推荐的学习链接: http://acm.uestc.edu.cn/bbs/read.php?tid=4294 http://blog.csdn.net/niushuai666/article/details/7002823 http://hi.baidu.com/nial

AC自动机(AC automation)

字典树+KMP 参考自: http://www.cppblog.com/mythit/archive/2009/04/21/80633.html 1 const int MAXN = 26; //字典大小 2 3 //定义结点 4 struct node{ 5 node* fail; 6 node* child[MAXN]; 7 int count; 8 node(){ 9 fail = NULL; 10 count = 0; 11 memset(child, NULL, sizeof(chil

[dp专题] AC自动机与状态压缩dp的结合

最近做到好几道关于AC自动机与状态压缩dp的结合的题,这里总结一下. 题目一般会给出m个字符串,m不超过10,然后求长度为len并且包含特定给出的字符串集合的字符串个数. 以HDU 4758为例: 把题意抽象为:给出两个字符串,且只包含两种字符 'R'.'D',现在求满足下列条件的字符串个数:字符串长度为(m+n),其中包含n个'D',m个'R'. 如果不用AC自动机来做,这道题还真没法做了,因为不管怎样都找不到正确的dp状态转移方程. 而如果引入AC自动机,把在AC自动机上的结点当做dp的一个

专题训练之AC自动机

推荐博客:http://www.cnblogs.com/kuangbin/p/3164106.html AC自动机小结 https://blog.csdn.net/creatorx/article/details/71100840 AC自动机最详细的解释 1.(HDOJ2222)http://acm.hdu.edu.cn/showproblem.php?pid=2222 题意:求目标串中出现了几个模式串. 分析:AC自动机模板题 1 #include<cstdio> 2 #include<

每日总结-05-19(AC自动机结束)

今天下午讨论了一下校赛的题,终于最终拍板,把校赛的题目定下来了. 然后今天A掉了4个AC自动机的题目.终于完成了AC自动机专辑里面的15个题.至此AC自动机完全结束. 明天开启线段树专题..... ------------------------------------------------------------------------------------------------------------------------------------ 1,,hdu-2457-DNA re

AC自动机+dp(CodeForces - 86C )

"Multidimensional spaces are completely out of style these days, unlike genetics problems" — thought physicist Woll and changed his subject of study to bioinformatics. Analysing results of sequencing he faced the following problem concerning DNA

暑假集训day9补充(AC自动机)

推荐网站http://blog.csdn.net/niushuai666/article/details/7002823 AC自动机嘛,此AC(aho-corasick)非彼AC(Accepted). 我也不是很会解释 有一题是必须打的hdu2222. #include<iostream> #include<cstdio> #include<cstring> #include<queue> using namespace std; const int mn=

ac自动机基础模板(hdu2222)

In the modern time, Search engine came into the life of everybody like Google, Baidu, etc. Wiskey also wants to bring this feature to his image retrieval system. Every image have a long description, when users type some keywords to find the image, th