首先要明白AC自动机是干什么的:

AC自动机其实就是一种多模匹配算法,那么你可能会问什么叫做多模匹配算法。下面是我对多模匹配的理解,与多模与之对于的是单模,单模就是给你一个单词,然后给你一个字符串,问你这个单词是否在这个字符串中出现过(匹配),这个问题可以用kmp算法在比较高效的效率上完成这个任务。那么现在我们换个问题,给你很多个单词,然后给你一段字符串,问你有多少个单词在这个字符串中出现过,当然我们暴力做,用每一个单词对字符串做kmp,这样虽然理论上可行,但是时间复杂度非常之高,当单词的个数比较多并且字符串很长的情况下不能有效的解决这个问题,所以这时候就要用到我们的ac自动机算法了。

一个母字符串,多个子字符串与其多次匹配,就是它的用处。

这里有很多例题:AC自动机小结


例题1:HDU2222

//======================
// HDU 2222
// 求目标串中出现了几个模式串
//====================
#include <stdio.h>
#include <algorithm>
#include <iostream>
#include <string.h>
#include <queue>
using namespace std; struct Trie
{
int next[500010][26],fail[500010],end[500010];
int root,L;
int newnode()
{
for(int i = 0;i < 26;i++)
next[L][i] = -1;
end[L++] = 0;
return L-1;
}
void init()
{
L = 0;
root = newnode();
}
void insert(char buf[])
{
int len = strlen(buf);
int now = root;
for(int i = 0;i < len;i++)
{
if(next[now][buf[i]-'a'] == -1)
next[now][buf[i]-'a'] = newnode();
now = next[now][buf[i]-'a'];
}
end[now]++;
}
void build()
{
queue<int>Q;
fail[root] = root;
for(int i = 0;i < 26;i++)
if(next[root][i] == -1)
next[root][i] = root;
else
{
fail[next[root][i]] = root;
Q.push(next[root][i]);
}
while( !Q.empty() )
{
int now = Q.front();
Q.pop();
for(int i = 0;i < 26;i++)
if(next[now][i] == -1)
next[now][i] = next[fail[now]][i];
else
{
fail[next[now][i]]=next[fail[now]][i];
Q.push(next[now][i]);
}
}
}
int query(char buf[])
{
int len = strlen(buf);
int now = root;
int res = 0;
for(int i = 0;i < len;i++)
{
now = next[now][buf[i]-'a'];
int temp = now;
while( temp != root )
{
res += end[temp];
end[temp] = 0;//如果这里没有清0(删去),那么就会变成重复统计
temp = fail[temp];
}
}
return res;
}
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 buf[1000010];
Trie ac;
int main()
{
int T;
int n;
scanf("%d",&T);
while( T-- )
{
scanf("%d",&n);
ac.init();
for(int i = 0;i < n;i++)
{
scanf("%s",buf);
ac.insert(buf);
}
ac.build();
scanf("%s",buf);
printf("%d\n",ac.query(buf));
}
return 0;
}

例题2:HDU5384

给你一堆母串,还有另一堆子串,询问每个母串中出现的子串总次数。(子串计数可重叠)

注:下面这个代码是阉割版的,需要将a变为a[maxn]数组同时遍历每个母串才可以使用。

#include<cstdio>
#include<cstring>
#include<string.h>
#include<algorithm>
#include<queue>
using namespace std; const int maxn = 1e6 + 5;
int n;
char a[maxn];
char s[maxn]; struct trie {
int next[maxn][26], fail[maxn], end[maxn];
int root, cnt;
int new_node() {
memset(next[cnt], -1, sizeof next[cnt]);
end[cnt++] = 0;
return cnt - 1;
}
void init() {
cnt = 0;
root = new_node();
}
void insert(char *buf) {//字典树插入一个单词
int len = strlen(buf);
int now = root;
for (int i = 0; i < len; i++) {
int id = buf[i] - 'a';
if (next[now][id] == -1) {
next[now][id] = new_node();
}
now = next[now][id];
}
end[now]++;
}
void build() {//构建fail指针
queue <int> q;
fail[root] = root;
for (int i = 0; i < 26; i++) {
if (next[root][i] == -1) {
next[root][i] = root;
}
else {
fail[next[root][i]] = root;
q.push(next[root][i]);
}
}
while (!q.empty()) {
int now = q.front(); q.pop();
for (int i = 0; i < 26; i++) {
if (next[now][i] == -1) {
next[now][i] = next[fail[now]][i];
}
else {
fail[next[now][i]] = next[fail[now]][i];
q.push(next[now][i]);
}
}
}
}
int query(string buf) {
int len = buf.length();
int now = root;
int res = 0;
for (int i = 0; i < len; i++) {
int id = buf[i] - 'a';
now = next[now][id];
int tmp = now;
while (tmp != root) {
res += end[tmp];
tmp = fail[tmp];
}
}
return res;
}
}ac; int main() {
int T;
scanf("%d", &T);
while (T--) {
ac.init();
scanf("%s", a);
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%s", s);
ac.insert(s);
}
ac.build();
printf("%d\n", ac.query(a));
}
return 0;
}

例题3:POJ2778

神仙打架、神仙打架。。。。

题意:

•题意:有m种DNA序列是有疾病的,问有多少种长度为n的DNA序列不包含任何一种有疾病的DNA序列。(仅含A,T,C,G四个字符)

•样例m=4,n=3,{“AA”,”AT”,”AC”,”AG”}

•答案为36,表示有36种长度为3的序列可以不包含疾病

简单来说就是:

算了我不说了。。。。看上面的讲解和代码吧

//https://blog.csdn.net/u013446688/article/details/47378255
#include <iostream>
#include <cstdio>
#include <queue>
#include <cstring>
using namespace std; const int MOD = 100000;
struct Matrix{
int mat[110][110], n; Matrix(){} Matrix(int _n){
n = _n;
for(int i = 0; i < n; i++)
for(int j = 0; j < n; j++)
mat[i][j] = 0;
} Matrix operator *(const Matrix &b) const{
Matrix ret = Matrix(n);
for(int i = 0; i < n; i++)
for(int j = 0; j < n; j++)
for(int k = 0; k < n; k++){
int tmp = (long long)mat[i][k] * b.mat[k][j] % MOD;
ret.mat[i][j] = (ret.mat[i][j] + tmp) % MOD;
}
return ret;
}
}; struct Trie{
int next[110][4], fail[110];
bool end[110];
int root, L; int newnode(){
for(int i = 0; i < 4; i++) next[L][i] = -1;
end[L++] = false;
return L-1;
} void init(){
L = 0;
root = newnode();
} int getch(char ch){
if(ch == 'A') return 0;
if(ch == 'C') return 1;
if(ch == 'G') return 2;
else return 3;
} void insert(char s[]){
int len = strlen(s);
int now = root;
for(int i = 0; i < len; i++){
if(next[now][getch(s[i])] == -1)
next[now][getch(s[i])] = newnode();
now = next[now][getch(s[i])];
}
end[now] = true;
} void build(){
queue<int> Q;
for(int i = 0; i < 4; i ++){
if(next[root][i] == -1) next[root][i] = root;
else{
fail[ next[root][i] ] = root;
Q.push(next[root][i]);
}
}
while(!Q.empty()){
int now = Q.front();
Q.pop();
if(end[ fail[now] ] == true) end[now] = true;
for(int i = 0; i < 4; i ++){
if(next[now][i] == -1)
next[now][i] = next[ fail[now] ][i];
else{
fail[ next[now][i] ] = next[ fail[now] ][i];
Q.push(next[now][i]);
}
}
}
} Matrix getMatrix(){
Matrix ret = Matrix(L);
for(int i = 0; i < L; i ++)
for(int j = 0; j < 4; j ++)
if(end[ next[i][j] ] == false)
ret.mat[i][ next[i][j] ] ++;
return ret;
}
}; Trie ac;
char buf[20]; Matrix pow_Mat(Matrix a, int n){ //快速幂
Matrix ret = Matrix(a.n);
for(int i = 0; i < ret.n; i ++) ret.mat[i][i] = 1;
Matrix tmp = a;
while(n){
if(n & 1) ret = ret * tmp;
tmp = tmp * tmp;
n >>= 1;
}
return ret;
} int main(){
#ifdef sxk
freopen("in.txt", "r", stdin);
#endif //sxk int n, m;
while(scanf("%d%d", &m, &n) == 2){
ac.init();
for(int i = 0; i < m; i ++){
scanf("%s", buf);
ac.insert(buf);
}
ac.build();
Matrix a = ac.getMatrix();
a = pow_Mat(a, n);
int ans = 0;
for(int i = 0; i < a.n; i ++)
ans = (ans + a.mat[0][i]) % MOD;
printf("%d\n", ans);
}
return 0;
}

AC自动机(模板+例题)的更多相关文章

  1. HDU 2222 AC自动机模板题

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

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

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

  3. HDU 3065 (AC自动机模板题)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3065 题目大意:多个模式串,范围是大写字母.匹配串的字符范围是(0~127).问匹配串中含有哪几种模 ...

  4. HDU 2896 (AC自动机模板题)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2896 题目大意:多个模式串.多个匹配串.其中串的字符范围是(0~127).问匹配串中含有哪几个模式串 ...

  5. HDU 2222(AC自动机模板题)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2222 题目大意:多个模式串.问匹配串中含有多少个模式串.注意模式串有重复,所以要累计重复结果. 解题 ...

  6. HDU 2222 (AC自动机模板题)

    题意: 给一个文本串和多个模式串,求文本串中一共出现多少次模式串 分析: ac自动机模板,关键是失配函数 #include <map> #include <set> #incl ...

  7. hdu 2222 Keywords Search ac自动机模板

    题目链接 先整理一发ac自动机模板.. #include <iostream> #include <vector> #include <cstdio> #inclu ...

  8. KMP与AC自动机模板

    HDU 1711 Number Sequence(KMP模板题) http://acm.hdu.edu.cn/showproblem.php?pid=1711 #include<bits/std ...

  9. HDU3695(AC自动机模板题)

    题意:给你n个字符串,再给你一个大的字符串A,问你着n个字符串在正的A和反的A里出现多少个? 其实就是AC自动机模板题啊( ╯□╰ ) 正着query一次再反着query一次就好了 /* gyt Li ...

  10. POJ2222 Keywords Search AC自动机模板

    http://acm.hdu.edu.cn/showproblem.php?pid=2222 题意:给出一些单词,求多少个单词在字符串中出现过(单词表单词可能有相同的,这些相同的单词视为不同的分别计数 ...

随机推荐

  1. sun.misc.Unsafe中一些常用方法记录

    sun.misc.Unsafe中一些常用方法记录 前情摘要 sun公司提供了可以用于直接操作内存的类,这个类就是sun.misc.Unsafe.因为Java本身是不会涉及到直接操作内存的,Java A ...

  2. c++头文件包含 #ifndef ##pragma once

    2013-04-14 17:03 (分类:计算机程序) 烦死了,这种垃圾小问题很多,你又必须要知道.......在编写c++程序时,会编写多个类或者多个cpp文件,免不了要多次使用include包含头 ...

  3. Java源码系列1——ArrayList

    本文简单介绍了 ArrayList,并对扩容,添加,删除操作的源代码做分析.能力有限,欢迎指正. ArrayList是什么? ArrayList 就是数组列表,主要用来装载数据.底层实现是数组 Obj ...

  4. TCP/IP分为几层?各层的作用是什么?

    1. 应用层 2.传输层 3.网络层 4.网络接口层* 1.应用层 TCP/IP协议族在这一层面有着很多协议来支持不同的应用,许多大家所熟悉的基于Internet的应用的实现就离不开这些协议.如我们进 ...

  5. jq模糊匹配(qq:2798641729)

    图灵学院--Java高级架构师-互联网企业级实战VIP课程(价值6380)(qq:1324981084) jq是一般程序员在前台开发的时候都会使用的技术,其中模糊匹配查询在动态添加标签的时候经常用到, ...

  6. Nginx Rewrite相关功能

    目录 Nginx Rewrite相关功能 ngx_http_rewrite_module模块指令: if指令: set指令: break指令: return指令: rewrite_log指令: rew ...

  7. OpenLayers动态测量距离和面积,并可自定义测量的线样式

    全局操作变量 /** * @description 标注弹出框 */ HtmlPopup = null; /** * @description 临时图层类数据源 */ VectorSource = n ...

  8. PyCharm专业版激活+破解到期时间2100年

    PyCharm专业版激活+破解到期时间2100年 转载文章:https://blog.51cto.com/13696145/2464312?source=dra 到2020年5月激活码: N7UR85 ...

  9. 简易音乐播放器主界面设计 - .NET CORE(C#) WPF开发

    微信公众号:Dotnet9,网站:Dotnet9,问题或建议:请网站留言, 如果对您有所帮助:欢迎赞赏. 简易音乐播放器主界面设计 - .NET CORE(C#) WPF开发 阅读导航 本文背景 代码 ...

  10. VEH帮你定位程序崩溃地址

    之前朋友有一个服务端程序,总是受到一些人的恶意漏洞攻击,没有源代码,只好反汇编修复了漏洞,并且使用WinLicense加保护授权. 漏洞总不是一次可以修复完的,恶意攻击并没有停止,然后加了WL保护程序 ...