#include<iostream>
#include<cstring>
#include<queue>
#include<cstdio>
#include<map>
#include<string>
using namespace std; const int SIGMA_SIZE = ;
const int MAXNODE = ;
const int MAXS = + ; map<string,int> ms; struct AhoCorasickAutomata
{
int ch[MAXNODE][SIGMA_SIZE];
int f[MAXNODE]; // fail函数
int val[MAXNODE]; // 每个字符串的结尾结点都有一个非0的val
int last[MAXNODE]; // 输出链表的下一个结点
int cnt[MAXS];
int sz; void init()
{
sz = ;
memset(ch[], , sizeof(ch[]));
memset(cnt, , sizeof(cnt));
ms.clear();
} // 字符c的编号
int idx(char c)
{
return c-'a';
} // 插入字符串。v必须非0
void insert(char *s, int v)
{
int u = ,i,c,n = strlen(s);
for(i = ; i < n; i++)
{
c = idx(s[i]);
if(!ch[u][c])
{
memset(ch[sz], , sizeof(ch[sz]));
val[sz] = ;
ch[u][c] = sz++;
}
u = ch[u][c];
}
val[u] = v;
ms[string(s)] = v;
} // 递归打印以结点j结尾的所有字符串
void print(int j)
{
if(j)
{
cnt[val[j]]++;
print(last[j]);
}
} // 在T中找模板
void find(char* T)
{
int i,j,c,n;
n = strlen(T);
j = ; // 当前结点编号,初始为根结点
for(i = ; i < n; i++)
{ // 文本串当前指针
c = idx(T[i]);
while(j && !ch[j][c]) j = f[j]; // 顺着细边走,直到可以匹配
j = ch[j][c];
if(val[j]) print(j);
else if(last[j]) print(last[j]); // 找到了!
}
} // 计算fail函数
void getFail()
{
int c,u,v,r;
queue<int> q;
f[] = ;
// 初始化队列
for(c = ; c < SIGMA_SIZE; c++)
{
u = ch[][c];
if(u) { f[u] = ; q.push(u); last[u] = ; }
}
// 按BFS顺序计算fail
while(!q.empty())
{
r = q.front(); q.pop();
for(c = ; c < SIGMA_SIZE; c++)
{
u = ch[r][c];
if(!u) continue;
q.push(u);
v = f[r];
while(v && !ch[v][c]) v = f[v];
f[u] = ch[v][c];
last[u] = val[f[u]] ? f[u] : last[f[u]];
}
}
} }; AhoCorasickAutomata ac;
char text[], P[][];
int n; int main()
{
int i,best;
while(scanf("%d", &n) == && n)
{
ac.init();
for(i = ; i <= n; i++)
{
scanf("%s", P[i]);
ac.insert(P[i], i);
}
ac.getFail();
scanf("%s", text);
ac.find(text);
best = -;
for(i = ; i <= n; i++)
if(ac.cnt[i] > best) best = ac.cnt[i];
printf("%d\n", best);
for(i = ; i <= n; i++)
if(ac.cnt[ms[string(P[i])]] == best) printf("%s\n", P[i]);
}
return ;
}

La 4670 AC自动机(模版)的更多相关文章

  1. LA 4670 (AC自动机 模板题) Dominating Patterns

    AC自动机大名叫Aho-Corasick Automata,不知道的还以为是能自动AC的呢,虽然它确实能帮你AC一些题目.=_=|| AC自动机看了好几天了,作用就是多个模式串在文本串上的匹配. 因为 ...

  2. LA 4670 AC自动机

    题意:给一个字典,看这个字典中匹配最多次数的是哪个单词(可以有多个). 分析: AC自动机就是用来解决多模式匹配问题的工具. 模板用的lrj的,相比HDU 2222,动态开辟字典树.用last数组统计 ...

  3. HDU 2222 Keywords Search(AC自动机模版题)

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

  4. HDU 3695 Computer Virus on Planet Pandora(AC自动机模版题)

    Computer Virus on Planet Pandora Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 256000/1280 ...

  5. HDU 2222 AC自动机模版题

    所学的AC自动机都源于斌哥和昀神的想法. 题意:求目标串中出现了几个模式串. 使用一个int型的end数组记录,查询一次. #include <cstdio> #include <c ...

  6. hdu 2896 AC自动机模版题

    题意:输出出现模式串的id,还是用end记录id就可以了. 本题有个关键点:“以上字符串中字符都是ASCII码可见字符(不包括回车).”  -----也就说AC自动机的Trie树需要128个单词分支. ...

  7. hdu 2222(AC自动机模版题)

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

  8. AC自动机模版

    我以前一直觉得AC自动机就是我打一个代码,然后可以帮我自动AC题目,现在才知道原来是处理字符串的一个有意思的东西,然后我们现在就来看一下这个东西 1464: [视频][AC自动机]统计单词出现个数 时 ...

  9. UVALive 4670 AC自动机

    第二道AC自动机的题目了,之前参考的是网上一个博客算法,不怎么好,难写而且占空间 后来参照大白书做的这题,代码简洁多了 #include <iostream> #include <c ...

随机推荐

  1. Unity调用Windows窗口句柄,选择文件和目录

    T:2019-6-25 10:06:59 C:Scatt Kang using System; using System.Collections; using System.Collections.G ...

  2. Oracle 闪回 找回数据的实现方法

    Oracle 闪回 找回数据的实现方法 闪回技术是Oracle强大数据库备份恢复机制的一部分,在数据库发生逻辑错误的时候,闪回技术能提供快速且最小损失的恢复.这篇文章主要介绍了Oracle 闪回 找回 ...

  3. Apache超时配置

    Apache超时配置 1. KeepAliveTimeout 语法 KeepAliveTimeout seconds 默认 5 上下文 server config, virtual host 说明 服 ...

  4. ssh整合思想初步 struts2与Spring的整合 struts2-spring-plugin-2.3.4.1.jar下载地址 自动加载Spring中的XML配置文件 Struts2下载地址

    首先需要JAR包 Spring整合Structs2的JAR包 struts2-spring-plugin-2.3.4.1.jar 下载地址 链接: https://pan.baidu.com/s/1o ...

  5. c++ 创建路径方法

    linux.unix平台 #include "stdio.h" #include "stdlib.h" #include <sys/types.h> ...

  6. @private@protected@public@package

    @private@protected@public@package 为了强制一个对象隐藏其数据,编译器限制实例变量范围以限制其在程序中的可见性 但是为了提供灵活性,苹果也让开发者显式设置范围(四选一) ...

  7. javascript实现原生ajax的几种方法介绍

    自从javascript有了各种框架之后,比如jquery,使用ajax已经变的相当简单了.但有时候为了追求简洁,可能项目中不需要加载jquery这种庞大的js插件.但又要使用到ajax这种功能该如何 ...

  8. php生成zip压缩文件的方法,支持文件和压缩包路径查找

    /* * new creatZip($_dir,$_zipName); *@ _dir是被压缩的文件夹名称,可使用路径,例 'a'或者'a/test.txt'或者'test.txt' *@ _zipN ...

  9. 21.Yii2.0框架多表关联一对多查询之性能优化--模型的使用

    控制器里 功能: 通过分类,查分类下的所有文章 //关联查询 public function actionRelatesearch(){ //关联查询 //查询方法一(查一行) 一维数组下的值是obj ...

  10. python-函数基础、函数参数

    目录 函数的基础 什么是函数 为何用函数 如何调用函数 定义函数的三种形式 无参函数 有参函数 空函数 函数的返回值 什么是返回值 为什么要有返回值 函数的调用 函数参数的应用 形参和实参 位置参数 ...