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 题意:给出一些单词,求多少个单词在字符串中出现过(单词表单词可能有相同的,这些相同的单词视为不同的分别计数 ...
随机推荐
- Alert提示框之后跳转指定页面
<li onclick="closes();">BTC</li> alert跳转到指定页面 <script type="text/javas ...
- 在vs2005中添加lib库的方法
方法一:在用到lib的地方加上 //强烈推荐这种方法.#pragma comment(lib,"libname.lib") 方法二: 点击菜单命令 “项目/属性”, ...
- 文件管理中心iOS版简介
App Store地址:https://itunes.apple.com/cn/app/id1023365565?mt=8 文件管理中心-装机必备的文件管家,专业的rar-zip 解压工具,局域网看片 ...
- BZOJ 2115: [Wc2011] Xor DFS + 线性基
2115: [Wc2011] Xor Time Limit: 10 Sec Memory Limit: 259 MB Description Input 第一行包含两个整数N和 M, 表示该无向图中 ...
- HDU 6073 Matching In Multiplication dfs遍历环 + 拓扑
Matching In Multiplication Problem DescriptionIn the mathematical discipline of graph theory, a bipa ...
- UOJ #35. 后缀排序[后缀数组详细整理]
#35. 后缀排序 统计 描述 提交 自定义测试 这是一道模板题. 读入一个长度为 nn 的由小写英文字母组成的字符串,请把这个字符串的所有非空后缀按字典序从小到大排序,然后按顺序输出后缀的第一个字符 ...
- monitor and move the log content to our big data system
Apache Flume HDFS Sink Tutorial | HowToProgram https://howtoprogram.xyz/2016/08/01/apache-flume-hdfs ...
- ZeroMQ 初步认识
http://www.danieleteti.it/zeromq-for-delphi/ https://my.oschina.net/zeroflamy/blog/109457 http://zer ...
- hdu 2066 一个人的旅行 解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2066 题目意思:给出T条路,和草儿家相邻的城市编号,以及草儿想去的地方的编号.问从草儿家到达草儿想去的 ...
- WCF寄宿到Windows Service[1]
WCF寄宿到Windows Service 返回 在前面创建一个简单的WCF程序,我们把WCF的服务寄宿到了Host这个控制台项目中了.下面将介绍如何把WCF的服务寄宿到Windows服务中(源代码) ...