LA_4670_Dominating_Patterns_(AC自动机+map)
描述
给出一个字符串和一些子串,求其中出现次数最多的子串.
分析
在AC自动机上面跑就行了.但是有一个要注意的地方,就是在输入文件里同一个子串重复出现.如果不特殊处理的话,后一个子串就会把Trie里的前一个子串覆盖掉.,所以我们可以用个map...
#include <bits/stdc++.h>
using namespace std; const int maxn=+,maxl=1e6+,maxnode=*+;
int n;
char text[maxl],p[maxn][+];
map <string,int> ms;
struct Aho_Corasick{
int ch[maxnode][];
int f[maxnode],val[maxnode],last[maxnode],cnt[maxn];
int sz;
inline int idx(char c){ return c-'a'; }
void init(){
sz=;
memset(ch[],,sizeof ch[]);
memset(cnt,,sizeof cnt);
ms.clear();
}
void insert(char *s,int v){
ms[string(s)]=v;
int u=;
for(;*s;s++){
int c=idx(*s);
if(!ch[u][c]){
memset(ch[++sz],,sizeof ch[]);
val[sz]=;
ch[u][c]=sz;
}
u=ch[u][c];
}
val[u]=v;
}
void get_fail(){
queue <int> q;
f[]=;
for(int c=;c<;c++){
int u=ch[][c];
if(u){ f[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){ ch[r][c]=ch[f[r]][c]; continue; }
q.push(u);
int v=f[r];
f[u]=ch[v][c];
last[u]=val[f[u]]?f[u]:last[f[u]];
}
}
}
void work(int j){
if(j){
cnt[val[j]]++;
work(last[j]);
}
}
void find(char *T){
int j=;
for(;*T;T++){
int c=idx(*T);
while(j&&!ch[j][c]) j=f[j];
j=ch[j][c];
if(val[j]) work(j);
else work(last[j]);
}
}
}ac;
int main(){
while(scanf("%d",&n)&&n){
ac.init();
for(int i=;i<=n;i++){
scanf("%s",p[i]);
ac.insert(p[i],i);
}
ac.get_fail();
scanf("%s",text);
ac.find(text);
int best=-;
for(int i=;i<=n;i++) best=max(best,ac.cnt[i]);
printf("%d\n",best);
for(int i=;i<=n;i++) if(ac.cnt[ms[string(p[i])]]==best) printf("%s\n",p[i]);
}
return ;
}
4670
Dominating Patterns
The archaeologists are going to decipher a very mysterious “language”. Now, they know many language
patterns; each pattern can be treated as a string on English letters (only lower case). As a sub string,
these patterns may appear more than one times in a large text string (also only lower case English
letters).
What matters most is that which patterns are the dominating patterns. Dominating pattern is the
pattern whose appearing times is not less than other patterns.
It is your job to find the dominating pattern(s) and their appearing times.
Input
The entire input contains multi cases. The first line of each case is an integer, which is the number of
patterns N , 1 ≤ N ≤ 150. Each of the following N lines contains one pattern, whose length is in range
[1, 70]. The rest of the case is one line contains a large string as the text to lookup, whose length is up
to 10 6 .
At the end of the input file, number ‘0’ indicates the end of input file.
Output
For each of the input cases, output the appearing times of the dominating pattern(s). If there are more
than one dominating pattern, output them in separate lines; and keep their input order to the output.
Sample Input
2
aba
bab
ababababac
6
beta
alpha
haha
delta
dede
tata
dedeltalphahahahototatalpha
0
Sample Output
4
aba
2
alpha
haha
LA_4670_Dominating_Patterns_(AC自动机+map)的更多相关文章
- BZOJ 2754: [SCOI2012]喵星球上的点名 [AC自动机+map+暴力]
2754: [SCOI2012]喵星球上的点名 Time Limit: 20 Sec Memory Limit: 128 MBSubmit: 1902 Solved: 837[Submit][St ...
- BZOJ 2754 [SCOI2012]喵星球上的点名 (AC自动机+map维护Trie树)
题目大意:略 由于字符集大,要用map维护Trie树 并不能用AC自动机的Trie图优化,不然内存会炸 所以我用AC自动机暴跳fail水过的 显然根据喵星人建AC自动机是不行的,所以要根据问题建 然而 ...
- BZOJ 2754 [SCOI2012]喵星球上的点名 (AC自动机、树状数组)
吐槽: 为啥很多人用AC自动机暴力跳都过了?复杂度真的对么? 做法一: AC自动机+树状数组 姓名的问题,中间加个特殊字符连起来即可. 肯定是对点名串建AC自动机(map存儿子),然后第一问就相当于问 ...
- [C#] 逆袭——自制日刷千题的AC自动机攻克HDU OJ
前言 做过杭电.浙大或是北大等ACM题库的人一定对“刷题”不陌生,以杭电OJ为例:首先打开首页(http://acm.hdu.edu.cn/),然后登陆,接着找到“Online Exercise”下的 ...
- UVALive 4670 Dominating Patterns --AC自动机第一题
题意:多个模板串,一个文本串,求出那些模板串在文本串中出现次数最多. 解法:AC自动机入门模板题. 代码: #include <iostream> #include <cstdio& ...
- 【原创】AC自动机小结
有了KMP和Trie的基础,就可以学习神奇的AC自动机了.AC自动机其实就是在Trie树上实现KMP,可以完成多模式串的匹配. AC自动机 其实 就是创建了一个状态的转移图,思想很 ...
- bzoj 3172 单词 ac自动机|后缀数组
题目大意: 给定n个字符串连成了一篇文章,问每个字符串在这篇文章中出现的次数,可重复覆盖 这里ac自动机和后缀数组都可以做 当然后缀数组很容易就解决,但是相对时间消耗高 这里就只讲ac自动机了 将每个 ...
- 【POJ2778】DNA Sequence(AC自动机,DP)
题意: 生物课上我们学到,DNA序列中只有A, C, T和G四种片段. 经科学发现,DNA序列中,包含某些片段会产生不好的基因,如片段"ATC"是不好片段,则"AGATC ...
- POJ 1625 Censored!(AC自动机+DP+高精度)
Censored! Time Limit: 5000MS Memory Limit: 10000K Total Submissions: 6956 Accepted: 1887 Descrip ...
随机推荐
- HDU 3943 K-th Nya Number(数位DP)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3943 题目大意:求出区间 (P,Q] 中找到第K个满足条件的数,条件是该数包含X个4和Y个7 Samp ...
- [转载]《STL源码剖析》阅读笔记之 迭代器及traits编程技法
本文从三方面总结迭代器 迭代器的思想 迭代器相应型别及traits思想 __type_traits思想 一 迭代器思想 迭代器的主要思想源于迭代器模式,其定义如下:提供一种方法,使之能够依 ...
- nginx 默认会把header里的参数去掉下划线
做token验证的时候遇到问题:在本地可以获取前端header传的参数,但是部署到服务器获取的就是null(服务器地址用nginx做了代理) 原因: nginx代理默认会把header的参数的 &qu ...
- linux 终端快捷键
1. 移动光标快捷键 ctrl+f 向前移动一个字符 ctrl+b 向后移动一个字符 alt+f 向前移动一个单词 alt+b 向后移动一个单词 ctrl+a 移动到当前行首 ctrl+e 移动到当前 ...
- SQLite学习第03天:环境搭建
相比于其他数据库而言,SQLite的环境搭建十分简单,简单几步就可以完成: (1) 首先,从http://www.sqlite.org/download.html的网站上下载预编译的shell二进制文 ...
- Python OptionParser学习
from optparse import OptionParser import sys def main(): p = OptionParser() p.add_option('-n','--nam ...
- linux内核驱动模型
linux内核驱动模型,以2.6.32内核为例.(一边写一边看的,有点乱.) 1.以内核对象为基础.用kobject表示,相当于其它对象的基类,是构建linux驱动模型的关键.具有相同类型的内核对象构 ...
- cocos2dx3.4 保存json文件
头文件: #include "json/document.h" #include "json/stringbuffer.h" #include "js ...
- 《深入理解计算机系统》C程序中常见的内存操作有关的典型编程错误
对C/C++程序员来说,内存管理是个不小的挑战,绝对值得慎之又慎,否则让由上万行代码构成的模块跑起来后才出现内存崩溃,是很让人痛苦的.因为崩溃的位置在时间和空间上,通常是在距真正的错误源一段距离之后才 ...
- js格式化数字,金额按千位逗号分隔,负号用括号
// 返回数字 function removeFormatMoney(s) { s = s.toString().replace("(","-").replac ...