AC自动机的裸题。学了kmp和Trie以后不难看懂。

有一些变化,比如0的定义和f的指向,和建立失配边,以及多了后缀连接数组last。没有试过把失配边直接当成普通边(一开始还是先这样写吧)。

#include<bits/stdc++.h>
using namespace std; const int maxlen = 1e6+, maxnds = *+, sigma_size = , maxsubs = ;
char str[maxlen]; #define idx(x) x-'a';
int last[maxnds];//后缀连接 为0表示空
int ch[maxnds][sigma_size];
int val[maxnds];
int nds;
char subs[maxsubs][];
int cnt[maxsubs+];
int f[maxnds]; void init() {
memset(ch,,sizeof(ch)); last[] = ; val[] = ; nds = ;
memset(cnt,,sizeof(cnt));
} void cal(int j)
{
while(j){
cnt[val[j]]++;
j = last[j];
}
} void Find(char *t)
{
for(int i = , j = ; t[i] ; i++){
int c = idx(t[i]);
while(j && !ch[j][c]) j = f[j];
j = ch[j][c];
if(val[j]) cal(j);
else if(last[j]) cal(last[j]);
}
} int getF()
{
queue<int> q;
f[] = ;
for(int c = ; c < sigma_size; c++){
int u = ch[][c];
if(u) { f[u] = ; q.push(u); last[u] = ; }
} while(q.size()){
int r = q.front(); q.pop();
for(int c = ; c < sigma_size; 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 add(char *s,int str_id)
{
int u = ;
for(int i = ; s[i]; i++){
int c = idx(s[i]);
if(!ch[u][c]){
memset(ch[nds],,sizeof(ch[nds]));
val[nds] = ;
ch[u][c] = nds++;
}
u = ch[u][c];
}
val[u] = str_id;
} int main()
{
//freopen("in.txt","r",stdin);
int n;
while(scanf("%d\n",&n),n){
init();
for(int i = ; i <= n; i++){
scanf("%s",subs[i]);
add(subs[i],i);
}
getF();
scanf("%s",str);
Find(str);
int best = *max_element(cnt+,cnt++n);
printf("%d\n",best);
for(int i = ; i <= n; i++){
if(cnt[i] == best) puts(subs[i]);
}
}
return ;
}

UVALive 4670 Dominating Patterns (AC自动机)的更多相关文章

  1. UVALive 4670 Dominating Patterns --AC自动机第一题

    题意:多个模板串,一个文本串,求出那些模板串在文本串中出现次数最多. 解法:AC自动机入门模板题. 代码: #include <iostream> #include <cstdio& ...

  2. UVALive - 4670 Dominating Patterns AC 自动机

    input n 1<=n<=150 word1 word2 ... wordn 1<=len(wirdi)<=70 s 1<=len(s)<=1000000 out ...

  3. LA 4670 Dominating Patterns (AC自动机)

    题意:给定一个一篇文章,然后下面有一些单词,问这些单词在这文章中出现过几次. 析:这是一个AC自动机的裸板,最后在匹配完之后再统计数目就好. 代码如下: #pragma comment(linker, ...

  4. 【暑假】[实用数据结构]UVAlive 4670 Dominating Patterns

    UVAlive 4670 Dominating Patterns 题目:   Dominating Patterns   Time Limit: 3000MS   Memory Limit: Unkn ...

  5. uvalive 4670 Dominating Patterns

    在文本串中找出现次数最多的子串. 思路:AC自动机模板+修改一下print函数. #include<stdio.h> #include<math.h> #include< ...

  6. UVa1449 - Dominating Patterns(AC自动机)

    题目大意 给定n个由小写字母组成的字符串和一个文本串T,你的任务是找出那些字符串在文本中出现的次数最多 题解 一个文本串,多个模式串,这刚好是AC自动机处理的问题 代码: #include <i ...

  7. UVa 1449 - Dominating Patterns (AC自动机)

    题目大意:给出多个字符串模板,并给出一个文本串,求在文本串中出现最多的模板,输出最多的次数并输出该模板(若有多个满足,则按输入顺序输出). 思路:赤裸裸的 AC自动机,上模板. 代码: #includ ...

  8. LA4670 Dominating Patterns AC自动机模板

    Dominating Patterns 每次看着别人的代码改成自己的模板都很头大...空间少了个0卡了好久 裸题,用比map + string更高效的vector代替蓝书中的处理方法 #include ...

  9. UVa Live 4670 Dominating Patterns - Aho-Corasick自动机

    题目传送门 快速的通道I 快速的通道II 题目大意 给定一堆短串,和一个文本串,问哪些短串在文本串中出现的次数最多. 我觉得刘汝佳的做法,时间复杂度有问题.只是似乎这道题短串串长太短不好卡.比如给出的 ...

随机推荐

  1. SQL 截取时间

    -- 获取系统时间 print getdate() -- 获取3天前的时间 print dateadd(day, -3 , getdate()) -- 获取3天后的时间 print dateadd(d ...

  2. HDU - 2689 Sort it与2016蓝桥杯B 交换瓶子 排序(相邻交换与任意交换)

    Sort it You want to processe a sequence of n distinct integers by swapping two adjacent sequence ele ...

  3. Excel .net读取

    public void LoadData(string StyleSheet) { string strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Dat ...

  4. 痞子衡嵌入式:微处理器CPU性能测试基准(Dhrystone)

    大家好,我是痞子衡,是正经搞技术的痞子.今天痞子衡给大家介绍的是微处理器CPU性能测试基准Dhrystone. 在嵌入式系统行业用于评价CPU性能指标的标准主要有三种:Dhrystone.MIPS.C ...

  5. 洛谷P4099 [HEOI2013]SAO(树形dp)

    传送门 HEOI的题好珂怕啊(各种意义上) 然后考虑树形dp,以大于为例 设$f[i][j]$表示$i$这个节点在子树中排名第$j$位时的总方案数(因为实际只与相对大小有关,与实际数值无关) 我们考虑 ...

  6. 前端三部曲之Html -- 1(html的基本结构和常见的meta标签的作用)

    一个H5页面的基本结构是什么 我么在编辑器中输入html:5可以得到 <!DOCTYPE html> <!-- 声明文档类型 --> <html lang="e ...

  7. ADO.NET 学习链接

    在博客园上,这个系列的文章对ADO.NET 总结的很好. ADO.NET 系列文章

  8. HDU1854 Q-Sequence

    http://acm.hdu.edu.cn/showproblem.php?pid=1854 随手练习 #include <bits/stdc++.h> using namespace s ...

  9. guacamole的复制粘贴

    一.发送中文或文本(针对开发环境,生产环境不需变动) 官方下载的完整版js缺失了一部分关于粘贴板的代码,调用setclipboard方法,将外部内容复制到粘贴板的时候,提示方法不存在.需要补齐这部分源 ...

  10. setTimeout的异步传输机制

    setTimeout是异步的,在设置完setTimeout后,指定代码会在设定的时间后加入到任务队列,但并不是立即执行,js是单线程语言,所有的代码按顺序执行,即同步执行,同步执行的代码放在执行队列中 ...