为了一言不合就徒手敲AC自动机,决定看一下原理

于是花了一张图,

参考HDU2222的样例

于是看懂这张图的你很快就敲出了如下代码并且AC了

#include<bits/stdc++.h>
#define rep(i,j,k) for(int i=j;i<=k;i++)
#define rrep(i,j,k) for(int i=j;i>=k;i--)
#define println(a) printf("%lld\n",(ll)(a))
#define printbk(a) printf("%lld ",(ll)(a))
typedef long long ll;
using namespace std;
const int MAXN = 2e6+11;
const ll oo = 0x3f3f3f3f3f3f3f3f;
const ll ooo= 0x3f3f3f3f;
ll read(){
ll x=0,f=1;register char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
struct AHO{
int cnt[MAXN];
int fail[MAXN];
int son[MAXN][26];
int tot,root;
void init(){
tot=root=1;
cnt[root]=fail[root]=0;
rep(i,0,25) son[root][i]=0;
}
int node(){
++tot;
cnt[tot]=fail[tot]=0;
rep(i,0,25) son[tot][i]=0;
return tot;
}
void insert(char *str){
int o=root;
int len=strlen(str+1);
rep(i,1,len){
int c=str[i]-'a';
if(son[o][c]==0){
son[o][c]=node();
}
o=son[o][c];
}
cnt[o]++;
}
void build(){
queue<int> que;
que.push(root);
while(!que.empty()){
int o=que.front();
que.pop();
rep(i,0,25){
if(!son[o][i]) continue;
if(o==root) fail[son[o][i]]=root;
else{
int t=fail[o];
while(t){
if(son[t][i]){// if exists
fail[son[o][i]]=son[t][i];
break;
}
t=fail[t];
}
if(t==0) fail[son[o][i]]=root;
}
que.push(son[o][i]);
}
}
}
int query(char *str){
int len=strlen(str+1);
int o=root;
int res=0;
rep(i,1,len){
int c=str[i]-'a';
while(son[o][c]==0&&o!=root) o=fail[o];
o=son[o][c];
if(o==0) o=root;
int t=o;
while(t!=root){
if(cnt[t]>=0){
res+=cnt[t];
cnt[t]=-1;
}else break;
t=fail[t];
}
}
return res;
}
}aho;
char str[MAXN];
int main(){
int T=read();
while(T--){
aho.init();
int n=read();
rep(i,1,n){
scanf("%s",str+1);
aho.insert(str);
}
aho.build();
scanf("%s",str+1);
println(aho.query(str));
}
return 0;
}

[意识流]简单易懂的AC自动机的更多相关文章

  1. 基于trie树做一个ac自动机

    基于trie树做一个ac自动机 #!/usr/bin/python # -*- coding: utf-8 -*- class Node: def __init__(self): self.value ...

  2. AC自动机-算法详解

    What's Aho-Corasick automaton? 一种多模式串匹配算法,该算法在1975年产生于贝尔实验室,是著名的多模式匹配算法之一. 简单的说,KMP用来在一篇文章中匹配一个模式串:但 ...

  3. python爬虫学习(11) —— 也写个AC自动机

    0. 写在前面 本文记录了一个AC自动机的诞生! 之前看过有人用C++写过AC自动机,也有用C#写的,还有一个用nodejs写的.. C# 逆袭--自制日刷千题的AC自动机攻克HDU OJ HDU 自 ...

  4. BZOJ 2434: [Noi2011]阿狸的打字机 [AC自动机 Fail树 树状数组 DFS序]

    2434: [Noi2011]阿狸的打字机 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 2545  Solved: 1419[Submit][Sta ...

  5. BZOJ 3172: [Tjoi2013]单词 [AC自动机 Fail树]

    3172: [Tjoi2013]单词 Time Limit: 10 Sec  Memory Limit: 512 MBSubmit: 3198  Solved: 1532[Submit][Status ...

  6. BZOJ 1212: [HNOI2004]L语言 [AC自动机 DP]

    1212: [HNOI2004]L语言 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 1367  Solved: 598[Submit][Status ...

  7. [AC自动机]【学习笔记】

    Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)To ...

  8. AC自动机 HDU 3065

    大概就是裸的AC自动机了 #include<stdio.h> #include<algorithm> #include<string.h> #include< ...

  9. AC自动机 HDU 2896

    n个字串 m个母串 字串在母串中出现几次 #include<stdio.h> #include<algorithm> #include<string.h> #inc ...

随机推荐

  1. C语言条件编译及编译预处理阶段(转)

    一.C语言由源代码生成的各阶段如下: C源程序->编译预处理->编译->优化程序->汇编程序->链接程序->可执行文件 其中 编译预处理阶段,读取c源程序,对其中的 ...

  2. Apache ab.exe压测工具使用

    Apache的ab命令模拟多线程并发请求,测试服务器负载压力,也可以测试nginx.lighthttp.IIS等其它Web服务器的压力. 1,打开cmd,找到安装apache的目录,cd  目录进去, ...

  3. 【转】Defunct进程 僵尸进程

    在测试基于 DirectFB+Gstreamer 的视频联播系统的一个 Demo 的时候,其中大量使用 system 调用的语句,例如在 menu 代码中的 system("./play&q ...

  4. LCD相关基础知识

    1.什么是LCD? (1)LCD(Liquid Crystal Display)俗称液晶.液晶是一种材料,液晶这种材料具有一种特点:可以在电信号的驱动下液晶分子进行旋转,旋转时会影响透光性, 因此我们 ...

  5. Multi-Sensor, Multi- Network Positioning

    Ruizhi Chen, Heidi Kuusniemi, Yuwei Chen, Ling Pei, Wei Chen, Jingbin Liu, Helena Leppäkoski, Jarmo ...

  6. What Are Tango Poses?Tango姿态是什么?

     What Are Tango Poses?什么是Tango姿态? As your device moves through 3D space, it calculates where it is ( ...

  7. Ubuntu再图形登录中以root的身份进入???

    Ubuntu再图形登录中以root的身份进入??? 这样做的需求,应该就是,可以再图形页面以root的身份进行图形化操作,比较方便更改配置文件. 1. 可以实现,但是不建议这么做,之后会出现一个警告提 ...

  8. Quartus II 软件生成FFT、NCO、FIR等IP核时卡住不动的解决办法

    据网友表示,遇到这个问题时,在任务管理器中手动关闭quartus_map进程就可以了,由于我的电脑最近一直没有出问题,因此也无法验证.欢迎大家针对这个问题讨论,提出肯定.否定的说法. 另外,很多人表示 ...

  9. 【JAVA 学习笔记1】代码注释

    在JAVA中支持单行注释和多行注释 1.单行注释,只要在注释的一行代码中加上双斜杠即可 例如: // int a=2,b=4,c=8; 2.多行注释,在开始位置加上/* 结束位置加上*/ 例如 /* ...

  10. 自定义Team Foundation Server (TFS) 与Project Professional的集成字段

    用户可以象使用Office Excel一样,使用Project Professional连接TFS,将数据下载到本地修改,并且发布到TFS服务器上,如果你习惯使用Project来计划你的项目,那么Pr ...