UVALive-4670 AC自动机入门题 求出现次数最多的子串
/**
链接:http://vjudge.net/problem/UVALive-4670
详见lrj训练指南P216
*/
#include<bits/stdc++.h>
using namespace std;
#define P pair<int,int>
#define ms(x,y) memset(x,y,sizeof x)
#define LL long long
const int maxn = ;
const int mod = 1e9+;
const int maxnode = *;
const int sigma_size = ;
map<string,int> mp;
int cnt[];
struct AhoCorasickAutomata
{
int ch[maxnode][sigma_size];
int val[maxnode];
int sz;
int f[maxnode];
int last[maxnode];
void clear(){sz = ; memset(ch[],,sizeof ch[]); }
int idx(char c){return c-'a'; } void insert(char *s,int v)
{
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 find(char *T){
int n = strlen(T);
int j = ;
for(int i = ; i < n; i++){
int c = idx(T[i]);
//while(j&&!ch[j][c]) j = f[j];
j = ch[j][c];
if(val[j]) print(j);
else if(last[j]) print(last[j]);
}
} void print(int j)
{
if(j){
cnt[val[j]]++;
print(last[j]);
}
} void getFail(){
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.empty()){
int r = q.front(); q.pop();
for(int c = ; c < sigma_size; c++){
int u = ch[r][c];
if(!u){
ch[r][c] = ch[f[r]][c]; continue;
}//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]];
}
}
} } ac ;
char a[][];
char s[];
int main()
{
int n;
while(scanf("%d",&n)==&&n)
{
ac.clear();
mp.clear();
memset(cnt, , sizeof cnt);
for(int i = ; i <= n; i++){
scanf("%s",a[i]);
ac.insert(a[i],i);
mp[string(a[i])] = i;
}
scanf("%s",s);
ac.getFail();
ac.find(s);
int mas = ;
for(int i = ; i <= n; i++){
if(cnt[i]>mas){mas = cnt[i];}
}
printf("%d\n",mas);
for(int i = ; i <= n; i++){
if(cnt[mp[string(a[i])]]==mas){
printf("%s\n",a[i]);
}
}
}
return ;
}
UVALive-4670 AC自动机入门题 求出现次数最多的子串的更多相关文章
- hdu2222 KeyWords Search AC自动机入门题
/** 链接:http://acm.hdu.edu.cn/showproblem.php?pid=2222 题意:题意:给定N(N <= 10000)个长度不大于50的模式串,再给定一个长度为L ...
- hdu3065 病毒侵袭持续中 AC自动机入门题 N(N <= 1000)个长度不大于50的模式串(保证所有的模式串都不相同), 一个长度不大于2000000的待匹配串,求模式串在待匹配串中的出现次数。
/** 题目:hdu3065 病毒侵袭持续中 链接:http://acm.hdu.edu.cn/showproblem.php?pid=3065 题意:N(N <= 1000)个长度不大于50的 ...
- LA 4670 (AC自动机 模板题) Dominating Patterns
AC自动机大名叫Aho-Corasick Automata,不知道的还以为是能自动AC的呢,虽然它确实能帮你AC一些题目.=_=|| AC自动机看了好几天了,作用就是多个模式串在文本串上的匹配. 因为 ...
- UVALive 4670 AC自动机
第二道AC自动机的题目了,之前参考的是网上一个博客算法,不怎么好,难写而且占空间 后来参照大白书做的这题,代码简洁多了 #include <iostream> #include <c ...
- hdu2896 病毒侵袭 AC自动机入门题 N(N <= 500)个长度不大于200的模式串(保证所有的模式串都不相同), M(M <= 1000)个长度不大于10000的待匹配串,问待匹配串中有哪几个模式串,
/** 题目:hdu2896 病毒侵袭 链接:http://acm.hdu.edu.cn/showproblem.php?pid=2896 题意:N(N <= 500)个长度不大于200的模式串 ...
- HDU2222(AC自动机入门题)
Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others ...
- Dominating Patterns (AC 自动鸡模版题, 出现次数最多的子串)
传送门 题意: 给你n个模式串, 再给你一个 文本串,问模式串在文本串中出现次数最多是多少. 出现次数最多的模式串有哪些. 解: 模版题. #include <bits/stdc++.h> ...
- UVA 11468 AC自动机入门题 记忆化概率dp+ac自动机
/** 链接:https://vjudge.net/problem/UVA-11468 详见lrj训练指南P218 我的是反向求存在模板串的概率. dp[i][j]表示当前i位置选择字符,前面i-1个 ...
- 【hdu2222】【poj2945】AC自动机入门题
HDU2222 传送门 题目分析 裸题:注意构建自动机用的是模式串,思想和kmp很类似. code: #include<iostream> #include<cstdio> # ...
随机推荐
- 【ML】求解线性回归方程(Linear Regression)
参考资料:openclassroom 线性回归(Linear Regression) 为了拟合10岁以下儿童年龄(x1)与身高(y)之间的关系,我们假设一个关于x的函数h(x): h(x) = Θ0+ ...
- WordPress网站搬家经验总结
http://cnzhx.net/blog/move-wordpress-site-step-by-step/也许很多人都有跟我类似的经历:因为某种原因需要将自己的WordPress站点从一个空间转移 ...
- Cookies揭秘 [Asp.Net, Javascript]
一,前言 Cookies想必所有人都了解, 但是未必所有人都精通.本文讲解了Cookies的各方面知识, 并且提出来了最佳实践.这是笔者在日常工作中的积累和沉淀. 二,基础知识 1.什么是Cookie ...
- Linux命令-文件系统常用命令:df,du,fsck,dumpe2fs
df -h 人性化显示文件系统的分区信息 注意:在linux中目录也是文件,不要混淆下面的目录文件说法. 传统方式查看文件大小可以使用:ll -h /目录/文件名,就可以看到文件的大小,但是如果你想看 ...
- RabbitMQ消息队列(五):Routing 消息路由 2[原]
上一篇文章使用的是Direct的Exchange,但是没有指定Queue的名字,这样只能是先运行Consumer之后,Producer在运行发消息Consumer才能收到,否则先运行Producer发 ...
- 安卓请求网络错误 直接在main Thread 进行网络操作出现maintreamexception
StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder() .detectDiskReads().detectDiskWrites ...
- 傅立叶级数(Fourier Series)和周期现象
一.前言 如果你仔细观察,工作和生活中充满了周期现象:旁边linux driver工程师在调试audio driver的时候播放的1kHz的正弦信号,周末去公园游玩,游船推开水面的波纹,硬件工程师调试 ...
- C#模拟PrtScn实现截屏
有了之前的基础知识了解,如今開始实现PrtScn和Alt+PrtScn. 首先新建一个WPF应用程序,命名为PrintscreenAndAltPrintScreen 导入keybd_event方法: ...
- CTPN - 训练
源码地址:https://github.com/eragonruan/text-detection-ctpn 该地址提供了 CTPN 的 tf 版本的实现,代码文档写得很详细,issue 里面也帮助解 ...
- springmvc+spring+mybatis+maven项目构建
1.首先在myeclipse10中安装maven的插件,将插件放入D:\Program Files (x86)\myEclipse10\MyEclipse Blue Edition 10\dropin ...