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 题意:给出一些单词,求多少个单词在字符串中出现过(单词表单词可能有相同的,这些相同的单词视为不同的分别计数 ...
随机推荐
- linux 输入子系统(4) intput_dev 接口描述
Name struct input_dev - represents an input device Synopsis struct input_dev { const char * name; // ...
- hiho1079 线段树区间改动离散化
题目链接: hihocoder1079 代码: #include<iostream> #include<cstdio> #include<cstring> #inc ...
- thinkphp getField( )和field( )
thinkphp getField( )和field( ) 做数据库查询的时候,比较经常用到这两个,总是查手册,记不住,现在把它总结下,希望以后用的时候不查手册了. 不管是用select 查询数据 ...
- 三种方法打印 main函数的返回地址的值(old EIP)(用途,你懂得!)
这里能够简单的改动随意函数的返回地址.能够做到自己定义EIP的指向,就可以运行当前进程空间的随意指令,这里仅仅是让大家更清楚栈帧结构,没有涉及跨进程的inline HOOK 等,后面会陆续讲下读取随意 ...
- Entity Framework 6 Code First 实践系列(1):实体类配置-根据依赖配置关系和关联
EF实体类的配置可以使用数据注释或Fluent API两种方式配置,Fluent API配置的关键在于搞清实体类的依赖关系,按此方法配置,快速高效合理.为了方便理解,我们使用简化的实体A和B以及A.B ...
- POJ 1183 反正切函数的应用(数学代换,基本不等式)
题目链接:http://poj.org/problem?id=1183 这道题关键在于数学式子的推导,由题目有1/a=(1/b+1/c)/(1-1/(b*c))---------->a=(b*c ...
- Hibernate Jar包官方下载
1.新手入门,从官网下载Hibernate,选择 Hibernate ORM 2.选择Releases-Overview 3.上面列出的是最新版本,下面有一个see older series 直接下载 ...
- 深入解析Hibernate核心接口
Hibernate有很多值得学习的地方,这里我们主要介绍Hibernate核心接口,包括介绍SessionFactory接口.Query和Criteria接口等方面. Session 接口对于Hibe ...
- 有奖试读&征文——我们在互联网上奋斗的故事 获奖名单发布
互联网是一个年轻的行业,同一时候也是一个推陈出新.不断进化的行业. 中国互联网行业在近期的十五年里.以如何的方式在"进化".我相信非常多奋斗在互联网战线上的你们最深有感触.读一读& ...
- windows下关闭指定端口服务,解决tomcat端口占用问题
http://blog.aizhet.com/Server/640.html 在windows下做java EE的开发时:搭建 Eclipse+tomcat的java web开发环境:在应用之中经常遇 ...