UvalLive4670(AC自动机模板)
放上刘汝佳的模板:
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <queue>
#include <map>
using namespace std; const int maxnod = * + ; int n;
char str[][], T[];
map<string, int> mp; struct AC_Automata {
int ch[maxnod][];//Trie树转移
int cnt[];//每个子串出现了几次
int val[maxnod];//某节点是否为子串结尾以及是哪个串的结尾
int f[maxnod];//失配,转移到别的树枝继续找
int last[maxnod];//找到了后缀相同的其他可行子串
int sz; void init() {
sz = ;
mp.clear();//与ac自动机无关
memset(ch[], , sizeof ch[]);
memset(cnt, , sizeof cnt);
} int idx(char c) { return c - 'a'; } void insert(char* s, int v) {//Trie树的插入
int u = , n =strlen(s);
for (int i = ; i < n; ++i) {
int 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;
} void getfail() {
queue<int> Q;
f[] = ;
for (int i = ; i < ; i++) {
int u = ch[][i];
if (u) {
f[u] = last[u] = ;
Q.push(u);
}
}
while (!Q.empty()) {
int r = Q.front(); Q.pop();
for (int c = ; c < ; c++) {
int u = ch[r][c];
if (!u) continue;
Q.push(u);
int 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]];
}
}
} void Success(int j) {
if (j) {
cnt[val[j]]++;
Success(last[j]);
}
} void find(char* T) {
int n = strlen(T);
for (int i = , j = ; i < n; ++i) {
int c = idx(T[i]);
while (j && !ch[j][c]) j = f[j];
j = ch[j][c];
if (val[j]) Success(j);
else if (last[j]) Success(last[j]);
}
}
}ac; int main() {
while (~scanf("%d", &n) && n) {
ac.init();
for (int i = ; i <= n; i++) {
scanf("%s", str[i]);
ac.insert(str[i], i);
mp[str[i]] = i;
}
ac.getfail();
scanf("%s", T);
ac.find(T); int ans = -;
for (int i = ; i <= n; i++) {
ans = max(ans, ac.cnt[i]);
}
printf("%d\n", ans);
for (int i = ; i <= n; i++) {
if (ac.cnt[mp[str[i]]] == ans) puts(str[i]);
}
}
return ;
}
UvalLive4670(AC自动机模板)的更多相关文章
- HDU 2222 AC自动机模板题
题目: http://acm.hdu.edu.cn/showproblem.php?pid=2222 AC自动机模板题 我现在对AC自动机的理解还一般,就贴一下我参考学习的两篇博客的链接: http: ...
- Match:Keywords Search(AC自动机模板)(HDU 2222)
多模匹配 题目大意:给定很多个字串A,B,C,D,E....,然后再给你目标串str字串,看目标串中出现多少个给定的字串. 经典AC自动机模板题,不多说. #include <iostream& ...
- HDU 3065 (AC自动机模板题)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3065 题目大意:多个模式串,范围是大写字母.匹配串的字符范围是(0~127).问匹配串中含有哪几种模 ...
- HDU 2896 (AC自动机模板题)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2896 题目大意:多个模式串.多个匹配串.其中串的字符范围是(0~127).问匹配串中含有哪几个模式串 ...
- HDU 2222(AC自动机模板题)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2222 题目大意:多个模式串.问匹配串中含有多少个模式串.注意模式串有重复,所以要累计重复结果. 解题 ...
- HDU 2222 (AC自动机模板题)
题意: 给一个文本串和多个模式串,求文本串中一共出现多少次模式串 分析: ac自动机模板,关键是失配函数 #include <map> #include <set> #incl ...
- hdu 2222 Keywords Search ac自动机模板
题目链接 先整理一发ac自动机模板.. #include <iostream> #include <vector> #include <cstdio> #inclu ...
- KMP与AC自动机模板
HDU 1711 Number Sequence(KMP模板题) http://acm.hdu.edu.cn/showproblem.php?pid=1711 #include<bits/std ...
- HDU3695(AC自动机模板题)
题意:给你n个字符串,再给你一个大的字符串A,问你着n个字符串在正的A和反的A里出现多少个? 其实就是AC自动机模板题啊( ╯□╰ ) 正着query一次再反着query一次就好了 /* gyt Li ...
- POJ2222 Keywords Search AC自动机模板
http://acm.hdu.edu.cn/showproblem.php?pid=2222 题意:给出一些单词,求多少个单词在字符串中出现过(单词表单词可能有相同的,这些相同的单词视为不同的分别计数 ...
随机推荐
- Direct3D 9 入门样例程序 圆锥体
介绍 Directx3D 9 什么是DirectX,非常好说了,Win32 C++ API.主要是多媒体编程方面的,长处体如今高性能了,如今我知道的版本号最高是D3D11,可是我是学习入门的,从D3D ...
- MySQL(6)--复制,docker容器中
MySQL5.7.11实现replication 启动两个安装好mysql的空的docker image ----------------- shell1 master $docker run -i ...
- Spring源码深度解析——笔记
1.spring容器的基本用法 xml配置 <bean id="myTestBean" class="bean.MyTestBean"/> 调用 B ...
- Spring中的事务管理(学习笔记)
什么是事物? 事物是指逻辑上的一组操作,这组操作要么全部成功,要么全部失败. 事物的特性: 原子性.一致性.隔离性.持久性 Spring事务管理的高级接口: PlatformTransactionMa ...
- 关于yum的一些基本的东西
1 sqlite数据库的使用 第一,某个centos版本的包相关的信息都放在一个服务器端的sqlite数据库文件中,yum会下载到本地,进行包安装的时候用. 第二,安装的历史记录放在sqlite数据库 ...
- HTML——使用表格进行页面布局
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- Netty实现时间服务演示样例
相关知识点: [1] ChannelGroup是一个容纳打开的通道实例的线程安全的集合,方便我们统一施加操作.所以在使用的过程中能够将一些相关的Channel归类为一个有意义的集合.关闭的通道会自己主 ...
- javascript if(xx)
js判断某个值知否存在或者为空,可以直接用if(xx)过滤. <!DOCTYPE html> <html> <head> <meta charset=&quo ...
- oracle服务丢失的处理方法之OracleServiceORCL不存在示例
oracle服务是oracle数据库的重要组成部分,下面就教您oracle服务丢失的处理方法,如果您之前遇到过oracle服务丢失的问题,不妨一看. 今天发现数据库服务器上的所有oracle服务都丢失 ...
- 【opencv】opencv在图片、视频嵌中英文字符的方法
转自:http://www.cnblogs.com/hujingshuang/p/5119015.html 说明:本博文是根据前人已有的成果并结合自己的理解而成的.为了避免让读者感到繁琐,我将运用小学 ...