AC自动机的初步学习 hdu2222 AC自动机入门题
2016-02-29 23:12
585 查看
本来KMP和trie树就不太熟悉,但是就是想弄明白AC自动机是什么东东,所以就找了一些博客学习了一下,用hdu2222练了一下手。
推荐几篇很好的博客:
http://www.cppblog.com/menjitianya/archive/2014/07/10/207604.html
这篇博客绝对是良心之作,尤其是那些图片,可以很好地讲述AC自动机的失败指针和trie图构造。
还有几篇不错的:
http://blog.csdn.net/mobius_strip/article/details/22549517
http://blog.csdn.net/niushuai666/article/details/7002823
对于做题的话,可以看kuangbin巨巨的专题,我的代码也是照着他的敲得。
http://www.cnblogs.com/kuangbin/p/3164106.html
如果基本明白了,就结合题目来练一下,hdu2222是基本题,推荐一篇注释详细的题解。
http://blog.sina.com.cn/s/blog_69c3f0410100tztt.html
代码:
#include<iostream>
#include<vector>
#include<cstring>
#include<string>
#include<cstdio>
#include<queue>
using namespace std;
typedef long long ll;
const int N=10000*50+5;
struct Trie
{
int next
[26],fail
,end
;
int rt,L;
int newnode()
{
memset(next[L],-1,sizeof(next[L]));
end[L++]=0;
return L-1;
}
void init()
{
L=0;
rt=newnode();
}
void insert(char *s)
{
int n=strlen(s);
int p=rt;
for(int i=0;i<n;i++){
if(next[p][s[i]-'a']==-1)
next[p][s[i]-'a']=newnode();
p=next[p][s[i]-'a'];
}
end[p]++;
}
void build()
{
queue<int>Q;
fail[rt]=rt;
for(int i=0;i<26;i++){
if(next[rt][i]==-1)next[rt][i]=rt;
else fail[next[rt][i]]=rt,Q.push(next[rt][i]);
}
while(!Q.empty()){
int p=Q.front();
Q.pop();
for(int i=0;i<26;i++){
if(next[p][i]==-1)next[p][i]=next[fail[p]][i];
else{
fail[next[p][i]]=next[fail[p]][i];
Q.push(next[p][i]);
}
}
}
}
int query(char *s)
{
int n=strlen(s);
int p=rt,ans=0;
for(int i=0;i<n;i++){
p=next[p][s[i]-'a'];
int t=p;
while(t!=rt){
ans+=end[t];
end[t]=0;
t=fail[t];
}
}
return ans;
}
void debug()
{
for(int i = 0;i < L;i++)
{
printf("id = %3d,fail = %3d,end = %3d,chi = [",i,fail[i],end[i]);
for(int j = 0;j < 26;j++)
printf("%2d",next[i][j]);
printf("]\n");
}
}
};
char str[1000010];
Trie ac;
int main()
{
int T;
int n;
//freopen("f.txt","r",stdin);
scanf("%d",&T);
while(T--){
scanf("%d",&n);
ac.init();
while(n--){
scanf("%s",str);
ac.insert(str);
}
ac.build();
// ac.debug();
scanf("%s",str);
printf("%d\n",ac.query(str));
}
return 0;
}
推荐几篇很好的博客:
http://www.cppblog.com/menjitianya/archive/2014/07/10/207604.html
这篇博客绝对是良心之作,尤其是那些图片,可以很好地讲述AC自动机的失败指针和trie图构造。
还有几篇不错的:
http://blog.csdn.net/mobius_strip/article/details/22549517
http://blog.csdn.net/niushuai666/article/details/7002823
对于做题的话,可以看kuangbin巨巨的专题,我的代码也是照着他的敲得。
http://www.cnblogs.com/kuangbin/p/3164106.html
如果基本明白了,就结合题目来练一下,hdu2222是基本题,推荐一篇注释详细的题解。
http://blog.sina.com.cn/s/blog_69c3f0410100tztt.html
代码:
#include<iostream>
#include<vector>
#include<cstring>
#include<string>
#include<cstdio>
#include<queue>
using namespace std;
typedef long long ll;
const int N=10000*50+5;
struct Trie
{
int next
[26],fail
,end
;
int rt,L;
int newnode()
{
memset(next[L],-1,sizeof(next[L]));
end[L++]=0;
return L-1;
}
void init()
{
L=0;
rt=newnode();
}
void insert(char *s)
{
int n=strlen(s);
int p=rt;
for(int i=0;i<n;i++){
if(next[p][s[i]-'a']==-1)
next[p][s[i]-'a']=newnode();
p=next[p][s[i]-'a'];
}
end[p]++;
}
void build()
{
queue<int>Q;
fail[rt]=rt;
for(int i=0;i<26;i++){
if(next[rt][i]==-1)next[rt][i]=rt;
else fail[next[rt][i]]=rt,Q.push(next[rt][i]);
}
while(!Q.empty()){
int p=Q.front();
Q.pop();
for(int i=0;i<26;i++){
if(next[p][i]==-1)next[p][i]=next[fail[p]][i];
else{
fail[next[p][i]]=next[fail[p]][i];
Q.push(next[p][i]);
}
}
}
}
int query(char *s)
{
int n=strlen(s);
int p=rt,ans=0;
for(int i=0;i<n;i++){
p=next[p][s[i]-'a'];
int t=p;
while(t!=rt){
ans+=end[t];
end[t]=0;
t=fail[t];
}
}
return ans;
}
void debug()
{
for(int i = 0;i < L;i++)
{
printf("id = %3d,fail = %3d,end = %3d,chi = [",i,fail[i],end[i]);
for(int j = 0;j < 26;j++)
printf("%2d",next[i][j]);
printf("]\n");
}
}
};
char str[1000010];
Trie ac;
int main()
{
int T;
int n;
//freopen("f.txt","r",stdin);
scanf("%d",&T);
while(T--){
scanf("%d",&n);
ac.init();
while(n--){
scanf("%s",str);
ac.insert(str);
}
ac.build();
// ac.debug();
scanf("%s",str);
printf("%d\n",ac.query(str));
}
return 0;
}
相关文章推荐
- js作用域
- Codeforces Round #320 (Div. 1) C. Weakness and Poorness
- 微信网页开发
- Hush Framework配置
- 安装 V-Play 笔记
- 使用XML Schema的简化配置方式
- SURF算子(1)
- React Native移动框架功能研究
- 遗传算法解决背包问题
- eventlet引发的学习-背景
- USACO 灌水
- PAT 1102. Invert a Binary Tree (25)
- python sklearn包——混淆矩阵、分类报告等自动生成
- Mac OSX 10.11.1+ 安装 Cocoapods
- lua 类、继承、多重继承
- OnTriggerEnter和OnCollisionEnter
- ListView与SimpleAdapter(三)
- BZOJ3607 : 数据网络
- 电商行业的技术特点以及架构的弊端传统
- Paxos算法深入分析