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 题意:给出一些单词,求多少个单词在字符串中出现过(单词表单词可能有相同的,这些相同的单词视为不同的分别计数 ...
随机推荐
- [PHP]PDO调用存储过程
1. 数据库中已创建存储过程user_logon_check, PHP调用示例如下, <?php $dsn = 'mssql:dbname=MyDbName;host=localhost'; $ ...
- 【Android开发-8】生命周期,Activity中打开另外一个Activity
前言:生命中有很多人陪伴自己走过一生中的某段旅程,仅仅是有些人仅仅是某阶段出现,有些人却陪伴自己非常久.就像小学.中学.高中.大学,那些以前以为会长久拥有的,当经历过天涯各地地忙碌于生活,或如意.或失 ...
- Android手机摇一摇的实现SensorEventListener
Android手机摇一摇的实现SensorEventListener 看实例 package com.example.shakeactivity; import android.content.Con ...
- LeetCode(38)题解: Count and Say
https://leetcode.com/problems/count-and-say/ 题目: The count-and-say sequence is the sequence of integ ...
- Anaconda和Pycharm安装和配置教程
1.下载Anaconda2 (最好选Python2.7的,兼容性好点) 在官网下载:https://www.continuum.io/downloads 2.安装Pycharm ...
- MapReduce算法形式二:去重(shuffle)
案例二:去重(shuffle/HashSet等方法)shuffle主要针对的是key去重HashSet主要针对values去重
- eclipse debug configurations arguments指定文件路径参数
1 eclipse debug configurations arguments指定文件路径参数 使用绝对路径,但是这个文件必须要放在该project的源码路径的外面才行,否则eclipse不认这个文 ...
- BAPI_PO_CEATE 与PO_1
- 自己写的canvas 手写画板
<!DOCTYPE html> <html> <head> <title>canvas</title> <meta charset=& ...
- iOS中区分照片的来源
原理就是通过枚举出每个assets group,然后取得group property,group property是个整数,对应头文件中的一些枚举值.用这个可以判断照片是从哪来的(相机胶卷.照片流.相 ...