为了一言不合就徒手敲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. Android 禁止屏幕旋转 & 旋转屏幕时保持Activity内容

    Android 禁止屏幕旋转 & 旋转屏幕时保持Activity内容   1.在应用中固定屏幕方向.        在AndroidManifest.xml的activity中加入:     ...

  2. JavaScript 对象笔记

    1. JS 将对象看成是属性的无序集合, 每个属性是一个key/value, 属性名是字符串, 值为任意类型; 对象除了键值对, 还从一个称为 "原型" 的 对象 继承属性(为啥是 ...

  3. 四则运算生成器(java) 蔡苑菲,陆海燕

    github地址:https://github.com/Nancy0611/Myapp.git 一.项目相关要求 使用 -n 参数控制生成题目的个数,例如 Myapp.exe -n 10 将生成10个 ...

  4. update chnroute

    curl -0 'http://ftp.apnic.net/apnic/stats/apnic/delegated-apnic-latest' | grep ipv4 | grep CN | awk ...

  5. 在ASP.NET Core2上操作MongoDB就是能这么的简便酷爽(自动完成分库分表)

    NoSQL是泛指非关系型的数据库,现今在我们的项目中也多有使用,其独特的优点为我们的项目架构带来了不少亮点,而我们这里的主角(MongoDB)则是NoSQL数据库家族中的一种.事实上,NoSQL数据库 ...

  6. Linux中连接mysql执行sql文件

    数据量小的时候可以把sql语句内容粘贴执行,但是文件很大的时候,这样执行效率很慢很慢,需要使用source执行sql文件 1.客户端连接mysql数据库 [root@iZbp1bb2egi7w0uey ...

  7. Devexpress Tab Control 文档

    https://documentation.devexpress.com/WPF/8078/Controls-and-Libraries/Layout-Management/Tab-Control/P ...

  8. WinForm 窗体应用程序(初步)之三

    进程: 进程,简单的说,就是让你的程序启动另一个程序. 1.Process.Start("calc");//启动计算器 弊端:只认识系统自带的程序,如果写错系统会崩溃. 2. // ...

  9. UWP开发砸手机系列(二)—— “讲述人”识别自定义控件Command

    上一篇我们提到如何让“讲述人”读出自定义的CanReadGrid,但“讲述人”仍然无法识别CanReadGrid上绑定的Command.XAML代码如下: <StackPanel> < ...

  10. python网络编程--socket,网络协议,TCP

    一. 客户端/服务端架构(用到网络通信的地方) 我们使用qq.微信和别人聊天,通过浏览器来浏览页面.看京东的网站,通过优酷.快播(此处只是怀念一下)看片片啥的等等,通过无线打印机来打印一个word文档 ...