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> # ...
随机推荐
- YACC、LEX、JAVACC-------常用的编译工具
CC(Compiler Compiler) CC的意思就是"编译器的编译器". 你可以定义一种上下文无关文法(CFG),然后针对这个特定的CFG你可以写出一个C程序来解释这种CFG ...
- 让你的APP和你的服务器畅快通讯
做安卓开发有很多时候都是要和web交互的,我们很难制作本地应用,这次把小弟整出来的安卓和服务器通讯贡献出来,希望能帮到需要的朋友,同时也是加深印象. 我们先来搭建安卓客户端,首先写好布局文件: 1.布 ...
- Hadoop命令手册
原文地址:http://hadoop.apache.org/docs/r1.0.4/cn/commands_manual.html 概述 常规选项 用户命令 archive distcp fs fsc ...
- C# 获取当前打开的文件夹2
这一个则比较投机,准确性不能保证,可以参考: 这个类获取当前进程的句柄: public class MyProcess { private bool haveMainWi ...
- OAF_OAF控件系列1 - Region Type汇总(概念)
2014-06-22 Created By BaoXinjian
- DBA_实践指南系列4_Oracle Erp R12系统备份和恢复Backup(案例)
2013-12-04 Created By BaoXinjian
- openssl之EVP系列之9---EVP_Digest系列函数的一个样例
openssl之EVP系列之9---EVP_Digest系列函数的一个样例 ---依据openssl doc/crypto/EVP_DigestInit.pod翻译 (作者:Drago ...
- 更改Android应用程序的图标
对于android应用程序的开发.默认的图标是一个小机器人,图片名称为ic_launcher.png. 可是,大多数开发人员是会将这个图标在开发过程中改为自己设计的icon. 把apk图标更改为自己设 ...
- EC20 minipcie版4g模块开发笔记
插在电脑上实验时若出现 AT+CREG? +CREG: 0,2 可能是usb口供电不足所致,换至主机箱后面usb口后问题解决,返回值+CREG: 0,1
- MyEclipse中常用的快捷键大全
http://www.cnblogs.com/wl0000-03/p/5953989.htmlMyEclipse中常用的快捷键大全 复制当前行到下(上)一行中:ctrl+alt+上下键 自动补全alt ...