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自动机的模板题. ...
随机推荐
- [Jenkins] 全局变量
http://www.360doc.com/content/14/1110/10/7811581_423993429.shtml https://wiki.jenkins.io/display/JEN ...
- mybatis思维导图(一)
写在前面 与hibernate相比,我无疑更喜欢mybatis,就因为我觉得它真的好用,哈哈.它简单上手和掌握:sql语句和代码分开,方便统一管理和优化:当然缺点也有:sql工作量很大,尤其是字段多. ...
- 为啥final类型的map或者arraylist可以修改数据 而final类型的String变量不可以修改数据呢
比如 final Map map =new HashMap(); 可以往map里put数据final List list =new ArrayList(); 可以往list里 ...
- springboot+swagger集成
一.swagger介绍 Swagger 是一款RESTFUL接口的文档在线自动生成+功能测试功能软件.本文简单介绍了在项目中集成swagger的方法和一些常见问题.如果想深入分析项目源码,了解更多内容 ...
- 32 取一个整数a从右端开始的4-7位
题目:取一个整数a从右端开始的4-7位 public class _032FetchDigit { public static void main(String[] args) { fetchDigi ...
- Word文档发布到CSDN博客
目前大部分的博客作者在写博客这件事情上都会遇到以下3个痛点:1.所有博客平台关闭了文档发布接口,用户无法使用Word,Windows Live Writer等工具来发布博客.2.发布到博客或公众号平台 ...
- Hadoop-2.4.0分布式安装手册
目录 目录 1 1. 前言 2 2. 部署 2 2.1. 机器列表 2 2.2. 主机名 2 2.2.1. 临时修改主机名 3 2.2.2. 永久修改主机名 3 2.3. 免密码登录范围 4 3. 约 ...
- 直接通过Response输出流写文件,浏览器表现为下载文件
response.setContentType("application/x-download"); response.addHeader("Content-Dispos ...
- MIDA Converter Basic patched for RAD Studio 10.1.2 Berlin (VCL转换到FMX)
Mida is the only way to try to convert your project from VCL to FireMonkey. Version after version, M ...
- Oracle数据表转换为Shapefile(二)
在上一篇博文<Oracle数据表转换为Shapefile(一)>中详细描述了一种基于Oracle数据表生产Shapefile的技术方法,本文同样以详细图解的方式描述一种更便捷的方法来完成同 ...