input

n 1<=n<=150

word1

word2

...

wordn

1<=len(wirdi)<=70

s 1<=len(s)<=1000000

output

最多出现次数

出现最多的串,按输入顺序输出,可能出现相同串,也要输出

做法:用一个end数组记下每个串结尾的字符的下标,对应val为1,每次找到就将val++,然后找到最大的val,输出最大val对应的字符串

 #include <cstdio>
#include <queue>
#include <cstring>
#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
#include <ctime>
#include <cmath>
#include <cctype>
#define MAX 100000
#define LL long long
int cas=,T,n,sz,maxt,last[*],f[*],ch[*][],val[*],end[];
char s[MAX*+],word[][];
int idx(char&c) {return c-'a';}
void inittrie()
{
memset(ch[],,sizeof(ch[]));
sz=;
val[]=;
}
int insert(char*s)
{
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]=;
return u;
}
void getFail()
{
std::queue<int>q;
f[]=;
for(int c=;c<;c++)
{
int u=ch[][c];
if(u) { f[u]=;q.push(u);last[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];
while(v&&!ch[v][c]) v=f[v];
f[u]=ch[v][c];
last[u]=val[f[u]]?f[u]:last[f[u]];
}
}
}
void print(int j)
{
if(j)
{
if(val[j]) val[j]++;
print(last[j]);
}
}
void find(char*s)
{
int n=strlen(s);
int j=;
for(int i=;i<n;i++)
{
int c=idx(s[i]);
j=ch[j][c];
//printf("val[%d]=%d\n",j,val[j]);
//if(val[j]) val[j]++;
//else if(last[j]) val[last[j]]++;
if(val[j]) print(j);
else if(last[j]) print(last[j]);
}
}
int main()
{
//freopen("/home/user/桌面/in","r",stdin);
//scanf("%d\n",&T);
while(scanf("%d",&n)==&&n)
{
inittrie();
for(int i=;i<n;i++)
{
scanf("%s",word[i]);
end[i]=insert(word[i]);
}
maxt=-;
// for(int i=0;i<n;i++) printf("%d %d %s\n",end[i],val[end[i]],word[i]);
getFail();
scanf("%s",s);
find(s);
for(int i=;i<n;i++) maxt=std::max(maxt,val[end[i]]);
printf("%d\n",maxt-);
for(int i=;i<n;i++) if(val[end[i]]==maxt) puts(word[i]);
}
//printf("time=%.3lf",(double)clock()/CLOCKS_PER_SEC);
return ;
}

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

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

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

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

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

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

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

  4. UVALive 4670 Dominating Patterns (AC自动机)

    AC自动机的裸题.学了kmp和Trie以后不难看懂. 有一些变化,比如0的定义和f的指向,和建立失配边,以及多了后缀连接数组last.没有试过把失配边直接当成普通边(一开始还是先这样写吧). #inc ...

  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. iOS 8 AutoLayout与Size Class自悟(转载)

    iOS 8 AutoLayout与Size Class自悟 Size classiOS 8 AutoLayout 前言 iOS8 和iPhone6发布已经过去蛮久了,广大的果粉终于迎来了大屏iPhon ...

  2. 兼容不同浏览器的 CSS Hack 写法

    所谓 CSS Hack,是指在 CSS 代码中嵌入诸如 *,*html  等代码,方便于独立控制某种浏览器的具体样式.比如有些 CSS Hack 只能被 IE6 或 IE7 识别,而 Firefox ...

  3. how to check if you have TURNIN successfully?

    For example, if I want to check if I have turnin my proj1 of cs130a successfully, I just use this co ...

  4. uitextfield银行卡加空格

    - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementS ...

  5. Knockout js 绑定 radio 时,当绑定整形的时候,绑定不生效

    解决方案: 使用checkedValue和checked 组合,如下代码. <div><input type="radio" name="flavorG ...

  6. loadrunner破解方法--lm70.dll,mlr5lprg.dll下载地址

    一.由于在压力测试执行中,出现一个-10803的错误 ,为解决这个错误,重新设置的环境变量,在次执行错误,这个问题解决了,但另外一个问题出来了,LR,打开脚本编辑器老提示找不到TEMP目录,当时没有想 ...

  7. linux 安装Gauss09 GaussView

  8. hadoop 常用配置项【转】

    hadoop 常用配置项[转] core-site.xml  name value  Description   fs.default.name hdfs://hadoopmaster:9000 定义 ...

  9. CSU 1639 队长,我想进集训队!

    水题 #include<cstdio> int main() { int x1, x2, x3, u, h; int n; while (~scanf("%d", &a ...

  10. [妙味Ajax]第三课:AJAX跨域解决方案:JSONP

    知识点总结: JSONP(JSON with Padding): 1.script标签 2.用script标签加载资源是没有跨域问题的 在资源加载进来之前定义好一个函数,这个函数接收一个参数(数据), ...