题目:P3808:https://www.luogu.org/problemnew/show/P3808

P3796:https://www.luogu.org/problemnew/show/P3796

从这里学了下AC自动机:http://www.cnblogs.com/cjyyb/p/7196308.html

我的理解大概就是构建一棵由模式串组成的 Trie 树,然后把文本串一节一节放在上面查找;

失配指针指向的是结尾字母和自己一样的、Trie 树上的其他分支,大约就是在找后缀这样的感觉;

所以文本串每加入一个字符,就在 Trie 树上查找以这个字符结尾的后缀模式串,所以能找到所有出现过的;

慕名已久的AC自动机原来也挺简单的嘛!

代码如下:

P3808:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
int const maxn=1e6+;
int n,cnt;
queue<int>q;
struct N{
int fail,son[],end;
}AC[maxn];
void build(string s)
{
int l=s.length();
int nw=;
for(int i=;i<l;i++)
{
if(AC[nw].son[s[i]-'a']==)AC[nw].son[s[i]-'a']=++cnt;
nw=AC[nw].son[s[i]-'a'];
}
AC[nw].end++;
}
void get_fail()
{
AC[].fail=;
for(int i=;i<;i++)
{
if(AC[].son[i]==)continue;
AC[AC[].son[i]].fail=;
q.push(AC[].son[i]);
}
while(q.size())
{
int x=q.front(); q.pop();
for(int i=;i<;i++)
{
if(AC[x].son[i])
{
AC[AC[x].son[i]].fail=AC[AC[x].fail].son[i];
q.push(AC[x].son[i]);
}
else AC[x].son[i]=AC[AC[x].fail].son[i];
}
}
}
int query(string s)
{
int l=s.length();
int ret=,nw=;
for(int i=;i<l;i++)
{
nw=AC[nw].son[s[i]-'a'];
for(int t=nw;t&&AC[t].end!=-;t=AC[t].fail)
{
ret+=AC[t].end;
AC[t].end=-;
}
}
return ret;
}
int main()
{
scanf("%d",&n);
string s;
for(int i=;i<=n;i++)
{
cin>>s;
build(s);
}
get_fail();
cin>>s;
printf("%d\n",query(s));
return ;
}

P3796:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
int const maxn=1e6+;
int n,cnt;
queue<int>q;
string s[];
struct N{
int son[],fail,end;
}AC[maxn];
struct P{int num,pos;}ans[];
bool cmp(P x,P y)
{
if(x.num==y.num)return x.pos<y.pos;
else return x.num>y.num;
}
void clear(int x)
{
memset(AC[x].son,,sizeof AC[x].son);
AC[x].fail=; AC[x].end=;
}
void build(string s,int num)
{
int l=s.length();
int nw=;
for(int i=;i<l;i++)
{
if(!AC[nw].son[s[i]-'a'])AC[nw].son[s[i]-'a']=++cnt,clear(cnt);
nw=AC[nw].son[s[i]-'a'];
}
AC[nw].end=num;
}
void get_fail()
{
while(q.size())q.pop();
AC[].fail=;
for(int i=;i<;i++)
{
if(AC[].son[i]==)continue;
AC[AC[].son[i]].fail=; q.push(AC[].son[i]);
}
while(q.size())
{
int x=q.front(); q.pop();
for(int i=;i<;i++)
{
if(AC[x].son[i])
{
AC[AC[x].son[i]].fail=AC[AC[x].fail].son[i];
q.push(AC[x].son[i]);
}
else AC[x].son[i]=AC[AC[x].fail].son[i];
}
}
}
void query(string s)
{
int l=s.length();
int nw=;
for(int i=;i<l;i++)
{
nw=AC[nw].son[s[i]-'a'];
for(int t=nw;t/*&&t.end!=-1*/;t=AC[t].fail)
{
ans[AC[t].end].num++;
// AC[t].end=-1;
}
}
}
int main()
{
while()
{
scanf("%d",&n);
if(!n)return ;
cnt=; clear();
for(int i=;i<=n;i++)
{
cin>>s[i];
build(s[i],i);
ans[i].pos=i;
ans[i].num=;//
}
get_fail();
cin>>s[];
query(s[]);
sort(ans+,ans+n+,cmp);
printf("%d\n",ans[].num);
cout<<s[ans[].pos]<<endl;
for(int i=;i<=n;i++)
{
if(ans[i].num==ans[i-].num)
cout<<s[ans[i].pos]<<endl;
else break;
}
}
}

洛谷P3808 & P3796 AC自动机模板的更多相关文章

  1. 洛谷 - P3966 - 单词 - AC自动机

    https://www.luogu.org/problemnew/show/P3966 因为文本串就是字典本身,所以这个和平时的AC自动机不太一样.平时的query要沿着fail树把子树的出现次数依次 ...

  2. 洛谷.3121.审查(AC自动机 链表)

    题目链接 //删掉一个单词需要前移一段位置,用链表维护就好了 复杂度O(sum(len)) #include <cstdio> #include <cstring> #defi ...

  3. 洛谷 - P2444 - 病毒 - AC自动机

    https://www.luogu.org/problemnew/show/P2444 有点恶心,不太明白fail的意义. #include<bits/stdc++.h> using na ...

  4. 洛谷P3808 【模板】AC自动机(简单版)

    题目背景 这是一道简单的AC自动机模板题. 用于检测正确性以及算法常数. 为了防止卡OJ,在保证正确的基础上只有两组数据,请不要恶意提交. 管理员提示:本题数据内有重复的单词,且重复单词应该计算多次, ...

  5. 【刷题】洛谷 P3808 【模板】AC自动机(简单版)

    题目背景 这是一道简单的AC自动机模板题. 用于检测正确性以及算法常数. 为了防止卡OJ,在保证正确的基础上只有两组数据,请不要恶意提交. 管理员提示:本题数据内有重复的单词,且重复单词应该计算多次, ...

  6. 浅谈AC自动机模板

    什么是AC自动机? 百度百科 Aho-Corasick automaton,该算法在1975年产生于贝尔实验室,是著名的多模匹配算法. 要学会AC自动机,我们必须知道什么是Trie,也就是字典树.Tr ...

  7. (模板)AC自动机模板

    模板1. 给出模式串和文本串,文本串长度小于1e6,模式串长度之和小于1e6,求文本串中有多少模式串出现. 题目链接:https://www.luogu.org/problem/P3808 AC co ...

  8. HDU 2222 AC自动机模板题

    题目: http://acm.hdu.edu.cn/showproblem.php?pid=2222 AC自动机模板题 我现在对AC自动机的理解还一般,就贴一下我参考学习的两篇博客的链接: http: ...

  9. Match:Keywords Search(AC自动机模板)(HDU 2222)

    多模匹配 题目大意:给定很多个字串A,B,C,D,E....,然后再给你目标串str字串,看目标串中出现多少个给定的字串. 经典AC自动机模板题,不多说. #include <iostream& ...

随机推荐

  1. 用u盘安装黑苹果10.12.3

    链接: https://pan.baidu.com/s/1eR9GgwE 密码: rubh 主机和显示器必须是数字口连接,如dvi.displayport,VGA不能进安装界面 下载苹果镜像文件10. ...

  2. MVCHelper 请求检验

    public class MVCHelper { //有 两 个ModelStateDictionary类,别弄混乱了要使用 System.Web.Mvc 下的 //如果添加引用中找不到System. ...

  3. JeePlus 工作流版本 sping mvc oa crm erp java html5 源码

    https://shop108220642.taobao.com/search.htm?spm=2013.1.w5002-5297459241.1.mnhAZ5&search=y http:/ ...

  4. MySQL中的分页操作结合python

    mysql中的分页操作结合python --分页: --方式1: ;-- 读取十行 , --从第十行读取 往后再读十行 --方式2: offset ; --从第二十行开始读取10行 -- 结合pyth ...

  5. grep,cut,wc,sort,diff,uniq,patch命令

    文本处理工具: Linux上文本处理三剑客: grep,egrep,fgrep: 文本过滤工具(模式: pattern)工具; grep:基本正则表达式,-E,-F egrep:扩展正则表达式,-G, ...

  6. 35.分组聚合操作—bucket+metric

    主要知识点: bucket+metric 计算分种颜色的电视的平均价格     语法: GET /tvs/sales/_search { "size" : 0, "agg ...

  7. python 函数编写指南

    #函数编写指南:1.给函数指定描述性名称,且只在其中是用小写字母和下划线 2.每个函数都应包含简要的阐述其功能的注释,该注释应紧跟在函数定义后面,且采用文档字符串格式 3.给形参指定默认值时,等号两边 ...

  8. Golang之路

    目录 Golang之路 Golang之路 Golang(一) - 开篇必须吹牛逼 Golang(二) - 第一个go程序和基本语法 Golang(三) - 函数 Golang(四) - 流程控制 Go ...

  9. Thesis Viva checklist

    This list gives you suggestions helpful in preparing to defend your thesis: I know my thesis thoroug ...

  10. ubuntu_linux /boot/grub/grub.conf

    ==========================================UBUNTU  /boot/grub/grub.conf文件============================ ...