AC自动机 专题
- // 求目标串中出现了几个模式串
- //====================
- #include <stdio.h>
- #include <algorithm>
- #include <iostream>
- #include <string.h>
- #include <queue>
- using namespace std;
- struct Trie
- {
- int next[][],fail[],end[];
- int root,L;
- int newnode()
- {
- for(int i = ;i < ;i++)
- next[L][i] = -;
- end[L++] = ;
- return L-;
- }
- void init()
- {
- L = ;
- root = newnode();
- }
- void insert(char buf[])
- {
- int len = strlen(buf);
- int now = root;
- for(int i = ;i < len;i++)
- {
- if(next[now][buf[i]-'a'] == -)
- next[now][buf[i]-'a'] = newnode();
- now = next[now][buf[i]-'a'];
- }
- end[now]++;
- }
- void build()
- {
- queue<int>Q;
- fail[root] = root;
- for(int i = ;i < ;i++)
- if(next[root][i] == -)
- next[root][i] = root;
- else
- {
- fail[next[root][i]] = root;
- Q.push(next[root][i]);
- }
- while( !Q.empty() )
- {
- int now = Q.front();
- Q.pop();
- for(int i = ;i < ;i++)
- if(next[now][i] == -)
- next[now][i] = next[fail[now]][i];
- else
- {
- fail[next[now][i]]=next[fail[now]][i];
- Q.push(next[now][i]);
- }
- }
- }
- int query(char buf[])
- {
- int len = strlen(buf);
- int now = root;
- int res = ;
- for(int i = ;i < len;i++)
- {
- now = next[now][buf[i]-'a'];
- int temp = now;
- while( temp != root )
- {
- res += end[temp];
- end[temp] = ;
- temp = fail[temp];
- }
- }
- return res;
- }
- void debug()
- {
- for(int i = ;i < L;i++)
- {
- printf("id = %3d,fail = %3d,end = %3d,chi = [",i,fail[i],end[i]);
- for(int j = ;j < ;j++)
- printf("%2d",next[i][j]);
- printf("]\n");
- }
- }
- };
- char buf[];
- Trie ac;
- int main()
- {
- int T;
- int n;
- scanf("%d",&T);
- while( T-- )
- {
- scanf("%d",&n);
- ac.init();
- for(int i = ;i < n;i++)
- {
- scanf("%s",buf);
- ac.insert(buf);
- }
- ac.build();
- scanf("%s",buf);
- printf("%d\n",ac.query(buf));
- }
- return ;
- }
- //输出每个模式串出现的次数
- #include <iostream>
- #include <stdio.h>
- #include <string.h>
- #include <algorithm>
- #include <queue>
- using namespace std;
- char str[][];
- struct Trie
- {
- int next[*][],fail[*],end[*];
- int root,L;
- int newnode()
- {
- for(int i = ;i < ;i++)
- next[L][i] = -;
- end[L++] = -;
- return L-;
- }
- void init()
- {
- L = ;
- root = newnode();
- }
- void insert(char s[],int id)
- {
- int len = strlen(s);
- int now = root;
- for(int i = ;i < len;i++)
- {
- if(next[now][s[i]] == -)
- next[now][s[i]] = newnode();
- now = next[now][s[i]];
- }
- end[now] = id;
- }
- void build()
- {
- queue<int>Q;
- fail[root] = root;
- for(int i = ;i < ;i++)
- if(next[root][i] == -)
- next[root][i] = root;
- else
- {
- fail[next[root][i]] = root;
- Q.push(next[root][i]);
- }
- while(!Q.empty())
- {
- int now = Q.front();
- Q.pop();
- for(int i = ;i < ;i++)
- if(next[now][i] == -)
- next[now][i]=next[fail[now]][i];
- else
- {
- fail[next[now][i]]=next[fail[now]][i];
- Q.push(next[now][i]);
- }
- }
- }
- int num[];
- void query(char buf[],int n)
- {
- for(int i = ;i < n;i++)
- num[i] = ;
- int len=strlen(buf);
- int now=root;
- for(int i=;i<len;i++)
- {
- now=next[now][buf[i]];
- int temp = now;
- while( temp != root )
- {
- if(end[temp] != -)
- num[end[temp]]++;
- temp = fail[temp];
- }
- }
- for(int i = ;i < n;i++)
- if(num[i] > )
- printf("%s: %d\n",str[i],num[i]);
- }
- };
- char buf[];
- Trie ac;
- void debug()
- {
- for (int i = ; i < ac.L; i++)
- {
- printf("id = %3d ,fail = %3d ,end = %3d, chi = [",i,ac.fail[i],ac.end[i]);
- for (int j = ; j < ; j++)
- printf("%2d ",ac.next[i][j]);
- printf("]\n");
- }
- }
- int main()
- {
- // freopen("in.txt","r",stdin);
- // freopen("out.txt","w",stdout);
- int n;
- while(scanf("%d",&n) == )
- {
- ac.init();
- for(int i = ;i < n;i++)
- {
- scanf("%s",str[i]);
- ac.insert(str[i],i);
- }
- ac.build();
- scanf("%s",buf);
- ac.query(buf,n);
- }
- return ;
- }
转自bin神 orz
AC自动机 专题的更多相关文章
- AC自动机专题
AC自动机简介:KMP是用于解决单模式串匹配问题, AC自动机用于解决多模式串匹配问题. 精华:设这个节点上的字母为C,沿着他父亲的失败指针走,直到走到一个节点,他的儿子中也有字母为C的节点.然后把当 ...
- AC自动机专题总结
最近学习了AC自动机,做了notonlysuccess大牛里面的题,也该来个总结了. AC自动机(Aho-Corasick Automaton)在1975年产生于贝尔实验室,是著名的多模匹配算法之一. ...
- 【原创】AC自动机小结
有了KMP和Trie的基础,就可以学习神奇的AC自动机了.AC自动机其实就是在Trie树上实现KMP,可以完成多模式串的匹配. AC自动机 其实 就是创建了一个状态的转移图,思想很 ...
- AC自动机(AC automation)
字典树+KMP 参考自: http://www.cppblog.com/mythit/archive/2009/04/21/80633.html ; //字典大小 //定义结点 struct node ...
- 转自kuangbin的AC自动机(赛前最后一博)
有了KMP和Trie的基础,就可以学习神奇的AC自动机了.AC自动机其实就是在Trie树上实现KMP,可以完成多模式串的匹配. AC自动机 其实 就是创建了一个状态的转移图,思想很 ...
- HDU - 2222,HDU - 2896,HDU - 3065,ZOJ - 3430 AC自动机求文本串和模式串信息(模板题)
最近正在学AC自动机,按照惯例需要刷一套kuangbin的AC自动机专题巩固 在网上看过很多模板,感觉kuangbin大神的模板最为简洁,于是就选择了用kuangbin大神的模板. AC自动机其实就是 ...
- 「kuangbin带你飞」专题十七 AC自动机
layout: post title: 「kuangbin带你飞」专题十七 AC自动机 author: "luowentaoaa" catalog: true tags: - ku ...
- [专题总结]AC自动机
其实前面的模板也不是1A,我在题库里提前做过,也不必在意罚时,刚开始我在做别的专题 裸模板我就不说了,各个博客讲解的很明白 void insert(string s){ ,len=s.size(); ...
- [专题汇总]AC自动机
1.The 2011 ACM-ICPC Asia Dalian Regional Contest ZOJ 3545 Rescue the Rabbit 简单的AC自动机+状压DP, 状态DP[nod ...
随机推荐
- uva 10491
https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- [Js]JavaScript闭包和范围的快速测试
1. if (!("a" in window)) { var a = 1; } alert(a); [分析]代码含义:如果window不包含属性a,就声明一个变量a并赋值为1 ①J ...
- 戴文的Linux内核专题:03驱动程序
转自Linux中国 驱动程序是使内核能够沟通和操作硬件或协议(规则和标准)的小程序.没有驱动程序,内核不知道如何与硬件沟通或者处理协议(内核实际上先发送指令给BIOS,然后BIOS传给硬件). Lin ...
- get( )与getline( )区别
get与getline区别不是很大,但一个明显的区别是get遇到 '\n '字符后便返回,这是 '\n '还在缓冲区中,所以下次读出来的将是 '\n ',而getline遇到 '\n '也返回,但它会 ...
- android 回调函数
http://blog.csdn.net/xiaanming/article/details/8703708 此为回调的java 实例 http://www.cnblogs.com/qingchen1 ...
- Cannot change network to bridged: There are no un-bridged host network adapters解决方法
首先,在你安装上了虚拟机后要确保你也安装了桥接的协议,这可以通过点击右键“网上邻居”,在其中可以看到有两个虚拟出来的网络一个VMnet1,另一个是VMnet8, 如下图所示. 如果没有安装,可以通过下 ...
- 2799元的HTC One时尚版要卖疯了
俗话说“好人有好报”,这句话同样可以应用到手机上.本月初,HTC正式公布了HTC One时尚版的售价,裸机2799元,礼包价2999元(配智能立显保护套).该价格一出,立刻引来一片哗然.因为大家都不相 ...
- PHPSESSID的cookie
如果PHP脚本中有: 1 session_start(); 则说明使用了SESSION. SESSION是一种机制,可以在服务器端跨文件暂时保存数据或传递数据,常用于购物车等方面. SESSION只在 ...
- C# 使用命令行编译单个CS文件
编译单个CS文件. 1.编译 File.cs 以产生 File.exe: csc File.cs 2.编译 File.cs 以产生 File.dll: ...
- python03函数、递归
本节内容 1. 函数基本语法及特性 2. 参数与局部变量 3. 返回值 4.递归 5.匿名函数 6.函数式编程介绍 7.高阶函数 8.内置函数 1.函数基本语法及特性 函数是什么? 函数一词来源于数学 ...