描述


https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2671

给出一个字符串和一些子串,求其中出现次数最多的子串.

分析


在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)的更多相关文章

  1. BZOJ 2754: [SCOI2012]喵星球上的点名 [AC自动机+map+暴力]

    2754: [SCOI2012]喵星球上的点名 Time Limit: 20 Sec  Memory Limit: 128 MBSubmit: 1902  Solved: 837[Submit][St ...

  2. BZOJ 2754 [SCOI2012]喵星球上的点名 (AC自动机+map维护Trie树)

    题目大意:略 由于字符集大,要用map维护Trie树 并不能用AC自动机的Trie图优化,不然内存会炸 所以我用AC自动机暴跳fail水过的 显然根据喵星人建AC自动机是不行的,所以要根据问题建 然而 ...

  3. BZOJ 2754 [SCOI2012]喵星球上的点名 (AC自动机、树状数组)

    吐槽: 为啥很多人用AC自动机暴力跳都过了?复杂度真的对么? 做法一: AC自动机+树状数组 姓名的问题,中间加个特殊字符连起来即可. 肯定是对点名串建AC自动机(map存儿子),然后第一问就相当于问 ...

  4. [C#] 逆袭——自制日刷千题的AC自动机攻克HDU OJ

    前言 做过杭电.浙大或是北大等ACM题库的人一定对“刷题”不陌生,以杭电OJ为例:首先打开首页(http://acm.hdu.edu.cn/),然后登陆,接着找到“Online Exercise”下的 ...

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

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

  6. 【原创】AC自动机小结

    有了KMP和Trie的基础,就可以学习神奇的AC自动机了.AC自动机其实就是在Trie树上实现KMP,可以完成多模式串的匹配.           AC自动机 其实 就是创建了一个状态的转移图,思想很 ...

  7. bzoj 3172 单词 ac自动机|后缀数组

    题目大意: 给定n个字符串连成了一篇文章,问每个字符串在这篇文章中出现的次数,可重复覆盖 这里ac自动机和后缀数组都可以做 当然后缀数组很容易就解决,但是相对时间消耗高 这里就只讲ac自动机了 将每个 ...

  8. 【POJ2778】DNA Sequence(AC自动机,DP)

    题意: 生物课上我们学到,DNA序列中只有A, C, T和G四种片段. 经科学发现,DNA序列中,包含某些片段会产生不好的基因,如片段"ATC"是不好片段,则"AGATC ...

  9. POJ 1625 Censored!(AC自动机+DP+高精度)

    Censored! Time Limit: 5000MS   Memory Limit: 10000K Total Submissions: 6956   Accepted: 1887 Descrip ...

随机推荐

  1. jQuery 源码分析5: jQuery 基本静态方法(一)

    jQuery在初始化过程中会为自己扩展一些基本的静态方法和属性,以下是jQuery 1.11.3版本 239 ~ 564行间所扩展的静态属性和方法   jQuery.extend({ // 为每个jQ ...

  2. [Castle Windsor]学习依赖注入

    初次尝试使用Castle Windsor实现依赖注入DI,或者叫做控制反转IOC. 参考: https://github.com/castleproject/Windsor/blob/master/d ...

  3. XML, XPath, Xslt及解析/Parse

    XML及解析/Parse "Programming with libxml2 is like the thrilling embrace of an exotic stranger.&quo ...

  4. [cocos2d-x 2.0.4][iOS7]图片加载错误

    本篇文章由:http://www.sollyu.com/cocos2d-x-2-0-4-ios7-image-loading-errors/ 说明 错误提示 <Error>: CGBitm ...

  5. POJ 1276 Cash Machine -- 动态规划(背包问题)

    题目地址:http://poj.org/problem?id=1276 Description A Bank plans to install a machine for cash withdrawa ...

  6. ajax查询数据返回结果不变

    在使用流水号的时候,Google浏览器没有问题,但是IE有缓存,如果ajax请求的参数没有变化,那么就会返回缓存里的数据 解决方法:ajax请求的时候传值的参数设置一个时间戳就OK了(没什么特别意义, ...

  7. Head First 设计模式系列之二----备忘录模式(java版)

    申明:这几天无意中关注到备忘录模式,比较陌生回家一番参考书,只在附录里记录了该模式.后来在园子里有发现了有专门写设计模式的博客,并且写的也得牛逼.附上链接 http://www.cnblogs.com ...

  8. 浅谈mysql中不同事务隔离级别下数据的显示效果

    事务的概念 事 务是一组原子性的SQL查询语句,也可以被看做一个工作单元.如果数据库引擎能够成功地对数据库应用所有的查询语句,它就会执行所有查询,如果任何一条查 询语句因为崩溃或其他原因而无法执行,那 ...

  9. CentOS 安装jdk7

    1.下载jdk​​ http://www.oracle.com/technetwork/java/javase/downloads/jdk7-downloads-1880260.html​ 选择jdk ...

  10. 配置Apache+Mysql+Php

    以下操作均在Debian 6.0 64bit 环境root权限下进行,如果提示权限不足请切换至root用户或者sudo,本人比较喜欢自行安装,因为安装的过程中能最小化安装而且能够知道安装了什么,然后可 ...