AC自动机基本操作

(1) 在AC自动机中,我们首先将每一个模式串插入到Trie树中去,建立一棵Trie树,然后构建fail指针。

(2) fail指针,是穿插在Trie树中各个结点之间的指针,顾名思义,就是当匹配失败的时候,用于引导p指针回溯,就和KMP算法中的next数组道理相同。

#include<bits/stdc++.h>
using namespace std;
#define MAX 26 //字典树关键字为‘a’~‘b’
char str[]; //主串(文章)
int n; //模式串共有n串 //字典树结点定义
struct Node
{
Node*next[MAX];
Node*fail;
int sum;
}*qu[]; void init(Node*root)
{
for(int i=;i<MAX;i++)
root->next[i]=NULL;
} //向字典树内添加模式串
void Insert(Node*root,char*ch)
{
Node*p=root;
while(*ch)
{
int index=*ch-'a';
if(p->next[index]==NULL)
{
p->next[index]=(Node*)malloc(sizeof(Node));
init(p->next[index]);
p->next[index]->sum=;
}
p=p->next[index];
ch++;
}
p->sum++;
} //利用模式串 字典树建树
Node*TrieCreate()
{
char ch[];
Node*root=(Node*)malloc(sizeof(Node));
init(root);
for(int i=;i<n;i++)
{
scanf("%s",ch);
Insert(root,ch);
}
return root;
} //在字典树内构建 失配指针fail
void BuildFail(Node*root)
{
int head=,tail=,i;
root->fail=NULL;
qu[tail++]=root;
while(head<tail)
{
Node*t=qu[head++];
Node*p=NULL;
for(i=;i<;i++)
{
if(t->next[i])
{
if(t==root)
t->next[i]->fail=root;
else
{
p=t->fail;
while(p)
{
if(p->next[i])
{
t->next[i]->fail=p->next[i];
break;
}
p=p->fail;
}
if(!p)
t->next[i]->fail=root;
}
qu[tail++]=t->next[i];
}
}
}
} //AC自动机 ,返回主串中模式串的数量(文章中关键字的数量)
int AC(Node*root,char*str)
{
int len=strlen(str),cnt=;
Node*p=root;
while(*str)
{
while(p->next[*str-'a']==NULL&&p!=root)p=p->fail;
p=p->next[*str-'a'];
p=(p==NULL)?root:p;
Node*t=p;
while(t!=root&&t->sum!=-)
{
cnt+=t->sum;
t->sum=-;
t=t->fail;
}
str++;
}
return cnt;
}
int main()
{
cin>>n;
Node*root=TrieCreate();
scanf("%s",str);
BuildFail(root);
printf("%d\n",AC(root,str));
return ;
}

算法竞赛模板 AC自动机的更多相关文章

  1. 算法模板——AC自动机

    实现功能——输入N,M,提供一个共计N个单词的词典,然后在最后输入的M个字符串中进行多串匹配(关于AC自动机算法,此处不再赘述,详见:Aho-Corasick 多模式匹配算法.AC自动机详解.考虑到有 ...

  2. 算法总结篇---AC自动机

    目录 写在前面 算法流程 引例: 概述: Trie树的构建(第一步) 失配指针(第二步) 构建失配指针 字典树和字典图 多模式匹配 例题 写在前面 鸣谢: OiWiki 「笔记」AC 自动机---Lu ...

  3. luoguP3808[模板]AC自动机(简单版)

    传送门 ac自动机模板题,裸的多串匹配 代码: #include<cstdio> #include<iostream> #include<algorithm> #i ...

  4. luoguP3796[模板]AC自动机(加强版)

    传送门 ac自动机模板,可能我写的ac自动机是有点问题的,所以跑的有些慢 暴力跳fail统计 代码: #include<cstdio> #include<iostream> # ...

  5. 模板 AC自动机

    题目描述 有$N$ 个由小写字母组成的模式串以及一个文本串$T$ .每个模式串可能会在文本串中出现多次.你需要找出哪些模式串在文本串$T$ 中出现的次数最多. 输入输出格式 输入格式: 输入含多组数据 ...

  6. 洛谷.3808/3796.[模板]AC自动机

    题目链接:简单版,增强版 简单版: #include <cstdio> #include <cstring> const int N=1e6+5,S=26; char s[N] ...

  7. 算法竞赛模板 KMP

    KMP算法图解: ① 首先,字符串“BBC ABCDAB ABCDABCDABDE”的第一个字符与搜索词“ABCDABD”的第一个字符,进行比较.因为B与A不匹配,所以搜索词后移一位. ② 因为B与A ...

  8. 模板—AC自动机

    #include<iostream> #include<cstdio> #include<cstring> using namespace std; struct ...

  9. 算法竞赛模板 动态规划之背包DP

    ① 01背包 有n件物品和一个容量为v的背包.第i件物品的价值是c[i],体积是w[i].求解将哪些物品装入背包可使价值总和最大. 这是最基础的背包问题,特点是:每种物品仅有一件,可以选择放或不放. ...

随机推荐

  1. Linux Kernel中所應用的數據結構及演算法

    Linux Kernel中所應用的數據結構及演算法 Basic Data Structures and Algorithms in the Linux kernel Links are to the  ...

  2. Codefores 506A Mr. Kitayuta, the Treasure Hunter( DP && dfs )

    A. Mr. Kitayuta, the Treasure Hunter time limit per test 1 second memory limit per test 256 megabyte ...

  3. java.lang.IllegalAccessError: Class ref in pre-verified class resolved to unexpected implementation getting while running test project?

    转摘:http://stackoverflow.com/questions/11155340/java-lang-illegalaccesserror-class-ref-in-pre-verifie ...

  4. HTTP Request 422 Unprocessable Entity

    最近接了一个接口,在调用接口时,返回这个错误代码. 百度到的解释是:请求格式正确,但是由于含有语义错误,无法响应. 开始一直在纠结,语义错误到底是什么?对照了无数次参数名,传参方式,无解. 后来用Fi ...

  5. shell函数的定义和调用

  6. shell条件测试语句

  7. js实用小函数收集

      格式化金额 var val='212312.235423' var rex = /\d{1,3}(?=(\d{3})+$)/g; val.replace(/^(-?)(\d+)((\.\d+)?) ...

  8. 如何用Word制作斜线表头?

    如何用Word制作斜线表头?遇到这种问题,你一般是如何操作?本期企业网盘坚果云干货分享与大家分享有关斜线表头的制作方法. 斜线表头分单斜线表头和多斜线表头,下面分情况来了解相关的解决办法. 单斜线表头 ...

  9. PHP实现笛卡尔积算法

    概念 在数学中,两个集合X和Y的笛卡儿积(Cartesian product),又称直积,表示为 X × Y.设A.B是任意两个集合,在集合A中任意取一个元素x,在集合B中任意取一个元素y,组成一个有 ...

  10. Application对象详解

    定义// import javax.servlet.ServletContext;// ServletContext类:用于表示应用程序的上下文// 取得application对象ServletCon ...