放上刘汝佳的模板:

 #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自动机模板)的更多相关文章

  1. HDU 2222 AC自动机模板题

    题目: http://acm.hdu.edu.cn/showproblem.php?pid=2222 AC自动机模板题 我现在对AC自动机的理解还一般,就贴一下我参考学习的两篇博客的链接: http: ...

  2. Match:Keywords Search(AC自动机模板)(HDU 2222)

    多模匹配 题目大意:给定很多个字串A,B,C,D,E....,然后再给你目标串str字串,看目标串中出现多少个给定的字串. 经典AC自动机模板题,不多说. #include <iostream& ...

  3. HDU 3065 (AC自动机模板题)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3065 题目大意:多个模式串,范围是大写字母.匹配串的字符范围是(0~127).问匹配串中含有哪几种模 ...

  4. HDU 2896 (AC自动机模板题)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2896 题目大意:多个模式串.多个匹配串.其中串的字符范围是(0~127).问匹配串中含有哪几个模式串 ...

  5. HDU 2222(AC自动机模板题)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2222 题目大意:多个模式串.问匹配串中含有多少个模式串.注意模式串有重复,所以要累计重复结果. 解题 ...

  6. HDU 2222 (AC自动机模板题)

    题意: 给一个文本串和多个模式串,求文本串中一共出现多少次模式串 分析: ac自动机模板,关键是失配函数 #include <map> #include <set> #incl ...

  7. hdu 2222 Keywords Search ac自动机模板

    题目链接 先整理一发ac自动机模板.. #include <iostream> #include <vector> #include <cstdio> #inclu ...

  8. KMP与AC自动机模板

    HDU 1711 Number Sequence(KMP模板题) http://acm.hdu.edu.cn/showproblem.php?pid=1711 #include<bits/std ...

  9. HDU3695(AC自动机模板题)

    题意:给你n个字符串,再给你一个大的字符串A,问你着n个字符串在正的A和反的A里出现多少个? 其实就是AC自动机模板题啊( ╯□╰ ) 正着query一次再反着query一次就好了 /* gyt Li ...

  10. POJ2222 Keywords Search AC自动机模板

    http://acm.hdu.edu.cn/showproblem.php?pid=2222 题意:给出一些单词,求多少个单词在字符串中出现过(单词表单词可能有相同的,这些相同的单词视为不同的分别计数 ...

随机推荐

  1. java之集合Collection 具体解释之4

    package cn.itcast_04; public class Student { private String name; private int age; public Student() ...

  2. 最长公共字序列.cpp

    <span style="color:#993399;">/* By yuan 2014/6/21 At nwpu.xf 1041.最长公共子序列 时限:1000ms ...

  3. webservice client setTimeOut

    一:eclipse生成的client,基于axis client_sub.getOptions().setTimeOutInMilliSeconds(1000*60); client_sub表示一个客 ...

  4. Maven 用法

    scope标签 provided:如果存在编译需要而发布不需要的jar包,使用provided属性值

  5. 树的深度优先遍历和广度优先遍历的原理和java实现代码

    import java.util.ArrayDeque; public class BinaryTree { static class TreeNode{ int value; TreeNode le ...

  6. js-easyUI格式化时间

    formatter : function(value, row) { if(value != null){ var date = new Date(value); var y = date.getFu ...

  7. date 命令 时间戳到标准格式转换

    1. 知道时间戳看标准时间, 时间戳到 秒: Wed Apr :: CST 2. 看到前时间时间戳格式 date +%s 3. 知道某个标准时间, 看时间戳 date -d "Wed Apr ...

  8. DEDECMS织梦自定义表单中必填项、电话邮箱过滤以及验证码规则

    织梦自定义表单必填项规则--->(wwwshu-acca.com网站表单) 1. 在plus/diy.php 的第 40行下加如下代码: 1 2 3 4 5 6 7 8 9 10 11 12 1 ...

  9. linux初级学习笔记十:linux grep及正则表达式!(视频序号:04_4)

    本节学习的命令:grep 本节学习的技能: grep对文本的匹配 正则表达式的使用 知识点十:grep及正则表达式(4_4) grep,egrep,fgrep: grep: 根据模式搜索文本,并将符合 ...

  10. 一步一步学Silverlight 2系列(29):使用Transform实现更炫的效果(上)

    概述 Silverlight 2 Beta 1版本发布了,无论从Runtime还是Tools都给我们带来了很多的惊喜,如支持框架语言Visual Basic, Visual C#, IronRuby, ...