AC自动机模板题
AC自动机理解要点:
1)fail指针指向的是每个节点,在字典树上和这个节点后缀相同的最长单词,每次都这样匹配,必定不会漏过答案。
2)字典树建立后,会在bfs求fail阶段把字典树变成一个字典树图(不知道理解的对不对),就是把字典树的末尾节点再往下添加一层,并且连接到fail指针指向的相同位子的儿子。
两道模板题和AC代码。
#include<bits/stdc++.h>
#define clr(a,b) memset(a,b,sizeof(a))
using namespace std;
typedef long long ll;
const int maxn=;
char s[maxn],p[maxn];
int trie[maxn][],cntword[maxn],fail[maxn],cnt=;
int n;
void insert(char *s){
int root=;
int si=strlen(s);
for(int i=;i<si;i++)
{
int Next=s[i]-'a';
if(!trie[root][Next])trie[root][Next]=++cnt;
root=trie[root][Next];
}
cntword[root]++;
}
void getfail(){
queue<int >q;
for(int i=;i<;i++)
{
if(trie[][i]){
q.push(trie[][i]);
}
}
while(!q.empty())
{
int now=q.front();
q.pop();
for(int i=;i<;i++)
{
if(trie[now][i]){
fail[trie[now][i]]=trie[fail[now]][i];
q.push(trie[now][i]);
}else{
trie[now][i]=trie[fail[now]][i];
}
}
}
}
int query(char *s){
int now=,ans=;
int si=strlen(s);
for(int i=;i<si;i++)
{
now=trie[now][s[i]-'a'];
for(int j=now;j&&cntword[j]!=-;j=fail[j])
{
ans+=cntword[j];
cntword[j]=-;
}
}
return ans;
}
int main(){
cin>>n;
while(n--)
{
scanf("%s",p);
insert(p);
}
fail[]=;
getfail();
scanf("%s",s);
cout<<query(s)<<endl;
}
#include<bits/stdc++.h>
#define clr(a,b) memset(a,b,sizeof(a))
using namespace std;
typedef long long ll;
const int maxn=;
char s[maxn],p[maxn];
int trie[maxn][],cntword[maxn],fail[maxn],cnt=;
int n;
int num[maxn];
map<int,string>dio;
void insert(char *s){
int root=;
int si=strlen(s);
for(int i=;i<si;i++)
{
int Next=s[i]-'a';
if(!trie[root][Next])trie[root][Next]=++cnt;
root=trie[root][Next];
}
dio[root]=s;
cntword[root]++;
}
void getfail(){
queue<int >q;
for(int i=;i<;i++)
{
if(trie[][i]){
q.push(trie[][i]);
}
}
while(!q.empty())
{
int now=q.front();
q.pop();
for(int i=;i<;i++)
{
if(trie[now][i]){
fail[trie[now][i]]=trie[fail[now]][i];
q.push(trie[now][i]);
}else{
trie[now][i]=trie[fail[now]][i];
}
}
}
}
void query(char *s){
int now=,ans=;
int si=strlen(s);
for(int i=;i<si;i++)
{
now=trie[now][s[i]-'a'];
for(int j=now;j&&cntword[j]!=-;j=fail[j])
{
//ans+=cntword[j];
if(cntword[j]>){
num[j]+=cntword[j];
}
//cntword[j]=-1;
}
}
}
void init(){
clr(trie,);
clr(num,),clr(cntword,);
dio.clear();
}
int main(){
while(scanf("%d",&n),n){
init();
for(int i=;i<=n;i++)
{
scanf("%s",p);
insert(p);
}
int p,maxx=;
vector<int >ans;
fail[]=;
getfail();
scanf("%s",s);
query(s);
for(int i=;i<=cnt;i++)
{
if(num[i]>maxx){
maxx=num[i];
ans.clear();
ans.push_back(i);
}else if(num[i]==maxx)
{
ans.push_back(i);
}
}
cout<<maxx<<endl;
for(int i=;i<ans.size();i++)
{
cout<<dio[ans[i]]<<endl;
}
}
}
AC自动机模板题的更多相关文章
- HDU 2222 AC自动机模板题
题目: http://acm.hdu.edu.cn/showproblem.php?pid=2222 AC自动机模板题 我现在对AC自动机的理解还一般,就贴一下我参考学习的两篇博客的链接: http: ...
- HDU 3065 (AC自动机模板题)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3065 题目大意:多个模式串,范围是大写字母.匹配串的字符范围是(0~127).问匹配串中含有哪几种模 ...
- HDU 2896 (AC自动机模板题)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2896 题目大意:多个模式串.多个匹配串.其中串的字符范围是(0~127).问匹配串中含有哪几个模式串 ...
- HDU 2222(AC自动机模板题)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2222 题目大意:多个模式串.问匹配串中含有多少个模式串.注意模式串有重复,所以要累计重复结果. 解题 ...
- HDU3695(AC自动机模板题)
题意:给你n个字符串,再给你一个大的字符串A,问你着n个字符串在正的A和反的A里出现多少个? 其实就是AC自动机模板题啊( ╯□╰ ) 正着query一次再反着query一次就好了 /* gyt Li ...
- HDu-2896 病毒侵袭,AC自动机模板题!
病毒侵袭 模板题,不多说了.. 题意:n个不同的字符串分别代表病毒特征,给出m次查询,每次一个字符串(网址),求这个字符串中有几个病毒特征,分别从大到小输出编号,最后输出所有的带病毒网址个数.格式请看 ...
- [Bzoj3940] [AC自动机,USACO 2015 February Gold] Censor [AC自动机模板题]
AC自动机模板题(膜jcvb代码) #include <iostream> #include <algorithm> #include <cstdio> #incl ...
- HDU 2222 (AC自动机模板题)
题意: 给一个文本串和多个模式串,求文本串中一共出现多少次模式串 分析: ac自动机模板,关键是失配函数 #include <map> #include <set> #incl ...
- HDU-2222 Keywords Search(AC自动机--模板题)
题目大意:统计一共出现了多少次模板串. 题目分析:AC自动机的模板题.不过这题有坑,相同的模板串不能只算一次. 代码如下: # include<iostream> # include< ...
- HDU 2222 Keywords Search(AC自动机模板题)
http://acm.hdu.edu.cn/showproblem.php?pid=2222 题意:给出多个单词,最后再给出一个模式串,求在该模式串中包含了多少个单词. 思路: AC自动机的模板题. ...
随机推荐
- 洛谷 P3627 [APIO2009](抢掠计划 缩点+spfa)
题目描述 Siruseri 城中的道路都是单向的.不同的道路由路口连接.按照法律的规定, 在每个路口都设立了一个 Siruseri 银行的 ATM 取款机.令人奇怪的是,Siruseri 的酒吧也都设 ...
- btrfs的精简总结版
Btrfs 也有一个重要的缺点,当 BTree 中某个节点出现错误时,文件系统将失去该节点之下的所有的文件信息.而 ext2/3 却避免了这种被称为”错误扩散”的问题. 扩展性:extent.B-Tr ...
- axure & Markman学习总结
最近学了几款有意思的软件,一款是axure,另一款是Markman.接下来聊聊自己的学习心得吧. 关于axure,百度上的解释是:是一个专业的快速原型设计工具.在我看来,它就是能快速把效果网页做给客户 ...
- css实现水平伸缩菜单
ul li a{transition:width 500ms ease;} a:hover{width:*;} 高度向上延伸用height:*;margin-top:-*px;//负值实现向上
- UVa 1220 Party at Hali-Bula (树形DP,最大独立集)
题意:公司有 n 个人形成一个树形结构,除了老板都有唯一的一个直系上司,要求选尽量多的人,但不能同时选一人上和他的直系上司,问最多能选多少人,并且是不是唯一的方案. 析:这个题几乎就是树的最大的独立集 ...
- servlet及xml文件处理流程
启动项目----会找到web.xml文件---跳转到默认jsp----页面重定向----转到xml.文件下 通过<servlet-mapping>映射找到<servlet>标签 ...
- [LintCode笔记了解一下]64.合并排序数组
Given two sorted integer arrays A and B, merge B into A as one sorted array. 思路: 因为A的后面的部分都是空的留出来给我们 ...
- django 'set' object does not support indexing
在定义Model之后使用syncdb 同步的时候报出这个错误,检查之后发现是用错了.在model的Meta里面,排序这些用的应该是 ordering = ['last','first','middle ...
- 一些参考网站 - Reference Documentation - Website Address
Reference Documentation - Website Address MSDN Visual Studio 2015官方文档 https://msdn.microsoft.com/zh- ...
- scvmm2008 错误 2941 0x80072efe
该错误是由于vmm和主机之间的证书不匹配所导致的. 1. 运行mmc,文件=>添加管理单元=>添加证书=>计算机账户=>本地计算机. 2. 在下图各个目录证书下删除和vmm关联 ...