AC自动机2
给N个模式串,求文本串中出现次数最多的模式串出现次数。
#include<bits/stdc++.h>
using namespace std;
#define maxn 1000005
#define maxm 28
struct AC
{
int trieN;
int ch[maxn][maxm];
int val[maxn];
int fail[maxn];
int s[maxn];
int tim[maxn];
void init()
{
trieN=-;
newnod();
}
int newnod()
{
memset(ch[++trieN],,sizeof ch[]);
val[trieN]=fail[trieN]=;
tim[trieN]=s[trieN]=;
return trieN;
}
void insert(const string & str,int k)
{
int cur=;
for(int i=; i<str.size(); i++)
{
int d=str[i]-'a';
if(!ch[cur][d])
{
ch[cur][d]=newnod();
}
cur=ch[cur][d];
}
val[cur]++;
s[cur]=k;
}
void build()
{
queue<int> q;
for(int i=; i<maxm; i++)
{
if(ch[][i])
{
q.push(ch[][i]);
}
}
while(!q.empty())
{
int cur=q.front();
q.pop();
for(int i=; i<maxm; i++)
{
if(ch[cur][i])
{
fail[ch[cur][i]]=ch[fail[cur]][i];
q.push(ch[cur][i]);
}
else
{
ch[cur][i]=ch[fail[cur]][i];
}
}
}
}
vector<int>ans;
int man=;
void query(const string & str)
{
ans.clear();
man=;
int res=,cur=;
for(int i=; str[i]; i++)
{
int d=str[i]-'a';
cur=ch[cur][d];
int tmp=cur;
// int si=0;
//cout<<tmp<<'\n';
while(tmp&&val[tmp]>=)
{
res=val[tmp];
if(val[tmp])
{
tim[tmp]++;
if(tim[tmp]>man)
{
man=tim[tmp];
ans.clear();
for(int _i=; _i<res; _i++)
ans.push_back(s[tmp]);
}
else if(man==tim[tmp])
{
for(int _i=; _i<res; _i++)
ans.push_back(s[tmp]);
}
}
//val[tmp]=-1;
tmp=fail[tmp];
} }
}
} ac;
string A[maxn];
int main()
{
string s;
int n;
while(scanf("%d",&n))
{
ac.init();
if(n==)break;
for(int i=; i<n; i++)
{
cin>>A[i];
ac.insert(A[i],i);
}
ac.build();
cin>>s;
ac.query(s);
cout<<ac.man<<'\n';
sort(ac.ans.begin(),ac.ans.end());
for(int i=; i<ac.ans.size(); i++)
{
cout<<A[ac.ans[i]]<<'\n';
}
}
}
AC自动机2的更多相关文章
- 基于trie树做一个ac自动机
基于trie树做一个ac自动机 #!/usr/bin/python # -*- coding: utf-8 -*- class Node: def __init__(self): self.value ...
- AC自动机-算法详解
What's Aho-Corasick automaton? 一种多模式串匹配算法,该算法在1975年产生于贝尔实验室,是著名的多模式匹配算法之一. 简单的说,KMP用来在一篇文章中匹配一个模式串:但 ...
- python爬虫学习(11) —— 也写个AC自动机
0. 写在前面 本文记录了一个AC自动机的诞生! 之前看过有人用C++写过AC自动机,也有用C#写的,还有一个用nodejs写的.. C# 逆袭--自制日刷千题的AC自动机攻克HDU OJ HDU 自 ...
- BZOJ 2434: [Noi2011]阿狸的打字机 [AC自动机 Fail树 树状数组 DFS序]
2434: [Noi2011]阿狸的打字机 Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 2545 Solved: 1419[Submit][Sta ...
- BZOJ 3172: [Tjoi2013]单词 [AC自动机 Fail树]
3172: [Tjoi2013]单词 Time Limit: 10 Sec Memory Limit: 512 MBSubmit: 3198 Solved: 1532[Submit][Status ...
- BZOJ 1212: [HNOI2004]L语言 [AC自动机 DP]
1212: [HNOI2004]L语言 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 1367 Solved: 598[Submit][Status ...
- [AC自动机]【学习笔记】
Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)To ...
- AC自动机 HDU 3065
大概就是裸的AC自动机了 #include<stdio.h> #include<algorithm> #include<string.h> #include< ...
- AC自动机 HDU 2896
n个字串 m个母串 字串在母串中出现几次 #include<stdio.h> #include<algorithm> #include<string.h> #inc ...
- 【BZOJ-3881】Divljak AC自动机fail树 + 树链剖分+ 树状数组 + DFS序
3881: [Coci2015]Divljak Time Limit: 20 Sec Memory Limit: 768 MBSubmit: 508 Solved: 158[Submit][Sta ...
随机推荐
- for循环、列表的切片、元组
一.遍历整个列表 使用for语句循环将列表每取出一个变量,然后存储在中间变量中,打印中间变量:循环取出: 1.简单for循环 示例: carssa = ['richan','fengtian','be ...
- django中使用原生的orm
只有用的时候才能执行
- Python-自定义函数-参数
一.自定义函数参数 1.种类 (1)位置参数 "x"就是位置参数 #!/usr/bin/env python # -*- coding: utf-8 -*- #author: di ...
- C语言Ⅰ博客作业01
1.你对计算机科学与技术专业了解是怎样? 本专业培养具有良好的科学素养,系统地.较好地掌握计算机科学与技术包括计算机硬件.软件与应用的基本理论.基本知识和基本技能与方法,能在科研部门.教育单位.企业. ...
- Linux运维的第三周总结
01. 下列文件中, 包含了主机名到IP地址的映射关系的文件是() A. /etc/HOSTNAME B. /etc/hosts C. /etc/resolv.conf ...
- [转帖]16nm国内最先进 兆芯展示x86 KX-6000八核处理器
16nm国内最先进 兆芯展示x86 KX-6000八核处理器 https://www.cnbeta.com/articles/tech/847125.htm 在近日的2019北京国际互联网科技博览会暨 ...
- 使用Object.create()实现继承 用 Object.create实现类式继承
使用Object.create()实现继承:https://www.cnblogs.com/cuew1987/p/4075027.html 用 Object.create实现类式继承:https:// ...
- adb,aapt等命令使用
adb install/uninstall:安装/卸载手机中的应用. devices:查看当前连接到电脑中的设备. adb shell 首先运行adb ...
- 《剑指offer》面试题23 从上往下打印二叉树 Java版
注意层序遍历的时候对每一层的处理方式可能不同,这里把每一层的元素保存进一个List中了,那么就需要记录每一层的数量. public List<List<Integer>> se ...
- c++多线程并发学习笔记(2)
等待一个时间或其他条件 在一个线程等待完成任务时,会有很多选择: 1. 它可以持续的检查共享数据标志(用于做保护工作的互斥量),直到另一个线程完成工作时对这个标志进行重设.缺点:资源浪费,开销大 2. ...