AC自动机学习博客

  AC自动机理解要点:

    1)fail指针指向的是每个节点,在字典树上和这个节点后缀相同的最长单词,每次都这样匹配,必定不会漏过答案。

    2)字典树建立后,会在bfs求fail阶段把字典树变成一个字典树图(不知道理解的对不对),就是把字典树的末尾节点再往下添加一层,并且连接到fail指针指向的相同位子的儿子。

   两道模板题和AC代码。

P3808

#include<bits/stdc++.h>
#define clr(a,b) memset(a,b,sizeof(a))
using namespace std;
typedef long long ll;
const int maxn=;
char s[maxn],p[maxn];
int trie[maxn][],cntword[maxn],fail[maxn],cnt=;
int n;
void insert(char *s){
int root=;
int si=strlen(s);
for(int i=;i<si;i++)
{
int Next=s[i]-'a';
if(!trie[root][Next])trie[root][Next]=++cnt;
root=trie[root][Next];
}
cntword[root]++;
}
void getfail(){
queue<int >q;
for(int i=;i<;i++)
{
if(trie[][i]){
q.push(trie[][i]);
}
}
while(!q.empty())
{
int now=q.front();
q.pop();
for(int i=;i<;i++)
{
if(trie[now][i]){
fail[trie[now][i]]=trie[fail[now]][i];
q.push(trie[now][i]);
}else{
trie[now][i]=trie[fail[now]][i];
}
}
}
}
int query(char *s){
int now=,ans=;
int si=strlen(s);
for(int i=;i<si;i++)
{
now=trie[now][s[i]-'a'];
for(int j=now;j&&cntword[j]!=-;j=fail[j])
{
ans+=cntword[j];
cntword[j]=-;
}
}
return ans;
}
int main(){
cin>>n;
while(n--)
{
scanf("%s",p);
insert(p);
}
fail[]=;
getfail();
scanf("%s",s);
cout<<query(s)<<endl;
}

P3796

#include<bits/stdc++.h>
#define clr(a,b) memset(a,b,sizeof(a))
using namespace std;
typedef long long ll;
const int maxn=;
char s[maxn],p[maxn];
int trie[maxn][],cntword[maxn],fail[maxn],cnt=;
int n;
int num[maxn];
map<int,string>dio;
void insert(char *s){
int root=;
int si=strlen(s);
for(int i=;i<si;i++)
{
int Next=s[i]-'a';
if(!trie[root][Next])trie[root][Next]=++cnt;
root=trie[root][Next];
}
dio[root]=s;
cntword[root]++;
}
void getfail(){
queue<int >q;
for(int i=;i<;i++)
{
if(trie[][i]){
q.push(trie[][i]);
}
}
while(!q.empty())
{
int now=q.front();
q.pop();
for(int i=;i<;i++)
{
if(trie[now][i]){
fail[trie[now][i]]=trie[fail[now]][i];
q.push(trie[now][i]);
}else{
trie[now][i]=trie[fail[now]][i];
}
}
}
}
void query(char *s){
int now=,ans=;
int si=strlen(s);
for(int i=;i<si;i++)
{
now=trie[now][s[i]-'a'];
for(int j=now;j&&cntword[j]!=-;j=fail[j])
{
//ans+=cntword[j];
if(cntword[j]>){
num[j]+=cntword[j];
}
//cntword[j]=-1;
}
}
}
void init(){
clr(trie,);
clr(num,),clr(cntword,);
dio.clear();
}
int main(){
while(scanf("%d",&n),n){
init();
for(int i=;i<=n;i++)
{
scanf("%s",p);
insert(p);
}
int p,maxx=;
vector<int >ans;
fail[]=;
getfail();
scanf("%s",s);
query(s);
for(int i=;i<=cnt;i++)
{
if(num[i]>maxx){
maxx=num[i];
ans.clear();
ans.push_back(i);
}else if(num[i]==maxx)
{
ans.push_back(i);
}
}
cout<<maxx<<endl;
for(int i=;i<ans.size();i++)
{
cout<<dio[ans[i]]<<endl;
}
}
}

AC自动机模板题的更多相关文章

  1. HDU 2222 AC自动机模板题

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

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

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

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

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

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

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

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

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

  6. HDu-2896 病毒侵袭,AC自动机模板题!

    病毒侵袭 模板题,不多说了.. 题意:n个不同的字符串分别代表病毒特征,给出m次查询,每次一个字符串(网址),求这个字符串中有几个病毒特征,分别从大到小输出编号,最后输出所有的带病毒网址个数.格式请看 ...

  7. [Bzoj3940] [AC自动机,USACO 2015 February Gold] Censor [AC自动机模板题]

    AC自动机模板题(膜jcvb代码) #include <iostream> #include <algorithm> #include <cstdio> #incl ...

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

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

  9. HDU-2222 Keywords Search(AC自动机--模板题)

    题目大意:统计一共出现了多少次模板串. 题目分析:AC自动机的模板题.不过这题有坑,相同的模板串不能只算一次. 代码如下: # include<iostream> # include< ...

  10. HDU 2222 Keywords Search(AC自动机模板题)

    http://acm.hdu.edu.cn/showproblem.php?pid=2222 题意:给出多个单词,最后再给出一个模式串,求在该模式串中包含了多少个单词. 思路: AC自动机的模板题. ...

随机推荐

  1. Greeplum 系列(二) 安装部署

    Greeplum 系列(二) 安装部署 本章将介绍如何快速安装部署 Greenplum,以及 Greenplum 的一些常用命令及工具.本章不会涉及硬件选型.操作系统参数讲解.机器性能测试等高级内容, ...

  2. TIMER_PWM_CAPTURE

  3. How to add a date range picker to filter for dates on a GridView for Yii2 - See more at: http://www.2amigos.us/blog/how-to-add-a-date-range-picker-to-filter-for-dates-on-a-gridview-for-yii2#sthash.pf7

    Filtering the data we have on our GridView by dates are sometimes very important. On this article I ...

  4. 两款JSON类库Jackson与JSON-lib的性能对比(新增第三款测试)

    本篇文章主要介绍了"两款JSON类库Jackson与JSON-lib的性能对比(新增第三款测试)",主要涉及到两款JSON类库Jackson与JSON-lib的性能对比(新增第三款 ...

  5. CodeForces 288A Polo the Penguin and Strings (水题)

    题意:给定一个字符,让你用前 k 个字符把它排成 n 长度,相邻的字符不能相等,并且把字典序最小. 析:其实很简单么,我们只要多循环ab,就行,最后再把剩下的放上,要注意k为1的时候. 代码如下: # ...

  6. 设计模式14:Command 命令模式(行为型模式)

    Command 命令模式(行为型模式) 耦合与变化 耦合是软件不能抵御变化的根本性原因.不仅实体对象与实体对象之间存在耦合关系,实体对象与行为操作之间也存在耦合关系. 动机(Motivation) 在 ...

  7. 20169214 2016-2017-2 《移动平台开发实践》Android程序设计 实验报告

    实验四 Android程序设计 课堂练习 实验题目 采用后缀表达式法,设计一个建议计算器,实现+.-.*./四种运算. 代码实现 码云链接 关键代码部分及结果如下: Android程序实验 Andro ...

  8. vs下C# WinForm 解决方案里面生成的文件都是什么作用?干什么的?

    Properties文件夹 定义你程序集的属性 项目属性文件夹 一般只有一个 AssemblyInfo.cs 类文件,用于保存程序集的信息,如名称,版本等,这些信息一般与项目属性面板中的数据对应,不需 ...

  9. JavaScript的词法作用域问题

    多年以前,当我怀揣着前端工程师的梦想时,曾经认真阅读过<JavaScript高级程序设计(第2版)>.里面有一个问题(P147),让我一直百思不得其解. function createFu ...

  10. JVM 方法调用之解析

    方法调用并不等同于方法执行,方法调用阶段唯一的任务就是确定被调用方法的版本(即调用哪一个方法),暂时还没有涉及到方法内部的具体运行过程.在程序运行时,进行方法调用是最普遍最频繁的操作,但Class文件 ...