题目: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. 微信小程序php后台实现

    这里简单介绍用php后台实现获取openid并保存到数据库: 微信的登陆流程是这样的 首先前端发送请求到服务器: wx.login({ success: function (res) { var co ...

  2. 中望CAD VBA检测文件是否存在

    Option Explicit Private Declare Function PathFileExists Lib "shlwapi.dll" Alias "Path ...

  3. Git学习总结四(删除)

    一般情况下,你通常直接在文件管理器中把没用的文件删了,或者用rm命令删了: $ rm test.txt 这个时候,Git知道你删除了文件,因此,工作区和版本库就不一致了,git status命令会立刻 ...

  4. typeof和instanceof的区别

    typeof和instanceof的区别: typeof typeof 是一个一元运算,放在一个运算数之前,运算数可以是任意类型.它返回值是一个字符串,该字符串说明运算数的类型.typeof 一般只能 ...

  5. pandas.DataFrame.quantile

    pandas.DataFrame.quantile 用于返回数据中的 处于1/5    1/2(中位数)等数据

  6. js for 循环 添加tr td 算法

    StringBuffer sb=new StringBuffer(); int n = 5; sb.append("<tr>"); List<MenuBean&g ...

  7. 关于javascript原型链的记录

    构造函数拥有名为prototype属性,每个对象都拥有__proto__属性,而且每个对象的__proto__属性指向自身构造函数prototype. **当调用某种方法或属性时,首先会在自身调用或查 ...

  8. Luogu P2970 [USACO09DEC]自私的放牧

    https://www.luogu.org/problemnew/show/P2970 P2970 [USACO09DEC]自私的放牧 题目描述 Each of Farmer John's N (1 ...

  9. [LUOGU] 4933 大师

    \(Orz\) \(ljt12138!\) 设状态\(f[i][j]\)表示以\(i\)为结尾,公差为\(j\)的长度大于\(1\)的数列有几个. 然后转移方程就很好想了. \(k=H[i]-H[j] ...

  10. GlobalSign 多域型(SNAs) SSL 证书

    GlobalSign 多域型(SNAs) SSL 证书 GlobalSign 多域型(SNAs) SSL 证书,有别于通配符 SSL 证书可以同时保护一个域名下所有的子域名网站,SANs 证书更进一步 ...