LA 4670 Dominating Patterns (AC自动机)
题意:给定n个字符串和一个文本串,查找哪个字符串出现的次数的最多。
析:一匹配多,很明显是AC自动机。只需要对原来的进行修改一下,就可以得到这个题的答案,
计算过程中,要更新次数,并且要映射字符串。如果用KMP肯定会超时。
代码如下:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <map>
#include <string>
#include <queue> using namespace std;
const int maxn = 1000000 + 5;
const int sigma = 26;
const int maxnode = 70 * 150 + 5;
map<string, int> ms;
//AC自动机
struct AhoCorasickAutomata{
int cnt[155];
int ch[maxnode][sigma];
int f[maxnode];//失配函数
int val[maxnode];//每个字符串结尾都有一个非0的val
int last[maxnode];//链表的下一个结点
int sz; void init(){
sz = 1;
memset(ch[0], 0, sizeof(ch[0]));
memset(cnt, 0, sizeof(cnt));
ms.clear();
} int idx(char c){ return c - 'a'; }//进行编号
//插入字符串
void insert(char *s, int v){
int u = 0, n = strlen(s);
for(int i = 0; i < n; ++i){
int c = idx(s[i]);
if(!ch[u][c]){
memset(ch[sz], 0, sizeof(ch[sz]));
val[sz] = 0;
ch[u][c] = sz++;
}
u = ch[u][c];
}
val[u] = v;
ms[s] = v;
}
//查找字符串
void find(char *T){
int n = strlen(T);
int j = 0;// 当前结点编号,初始为根结点
for(int i = 0; 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]);
}
}
//获得失配函数
int getFail(){
queue<int> q;
f[0] = 0;
//初始化队列
for(int c = 0; c < sigma; ++c){
int u = ch[0][c];
if(u){ f[u] = 0; q.push(u); last[u] = 0; }
}
//bfs
while(!q.empty()){
int r = q.front(); q.pop();
for(int c = 0; c < sigma; ++c){
int u = ch[r][c];
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]];
}
}
} }; AhoCorasickAutomata ac;
char text[maxn], p[155][75]; int main(){
int n;
while(scanf("%d", &n) == 1 && n){
ac.init();
for(int i = 1; i <= n; ++i){
scanf("%s", p[i]);
ac.insert(p[i], i);
} ac.getFail();
scanf("%s", text);
ac.find(text);
int m = -1;
for(int i = 1; i <= n; ++i)
m = max(m, ac.cnt[i]);
printf("%d\n", m); for(int i = 1; i <= n; ++i)
if(ac.cnt[ms[p[i]]] == m) printf("%s\n", p[i]); }
return 0;
}
LA 4670 Dominating Patterns (AC自动机)的更多相关文章
- LA 4670 Dominating Patterns (AC自动机)
题意:给定一个一篇文章,然后下面有一些单词,问这些单词在这文章中出现过几次. 析:这是一个AC自动机的裸板,最后在匹配完之后再统计数目就好. 代码如下: #pragma comment(linker, ...
- UVALive 4670 Dominating Patterns --AC自动机第一题
题意:多个模板串,一个文本串,求出那些模板串在文本串中出现次数最多. 解法:AC自动机入门模板题. 代码: #include <iostream> #include <cstdio& ...
- UVALive - 4670 Dominating Patterns AC 自动机
input n 1<=n<=150 word1 word2 ... wordn 1<=len(wirdi)<=70 s 1<=len(s)<=1000000 out ...
- UVa1449 - Dominating Patterns(AC自动机)
题目大意 给定n个由小写字母组成的字符串和一个文本串T,你的任务是找出那些字符串在文本中出现的次数最多 题解 一个文本串,多个模式串,这刚好是AC自动机处理的问题 代码: #include <i ...
- UVa 1449 - Dominating Patterns (AC自动机)
题目大意:给出多个字符串模板,并给出一个文本串,求在文本串中出现最多的模板,输出最多的次数并输出该模板(若有多个满足,则按输入顺序输出). 思路:赤裸裸的 AC自动机,上模板. 代码: #includ ...
- LA4670 Dominating Patterns AC自动机模板
Dominating Patterns 每次看着别人的代码改成自己的模板都很头大...空间少了个0卡了好久 裸题,用比map + string更高效的vector代替蓝书中的处理方法 #include ...
- AC自动机 LA 4670 Dominating Patterns
题目传送门 题意:训练指南P216 分析:求出现最多次数的字串,那么对每个字串映射id,cnt记录次数求最大就可以了. #include <bits/stdc++.h> using nam ...
- UVa Live 4670 Dominating Patterns - Aho-Corasick自动机
题目传送门 快速的通道I 快速的通道II 题目大意 给定一堆短串,和一个文本串,问哪些短串在文本串中出现的次数最多. 我觉得刘汝佳的做法,时间复杂度有问题.只是似乎这道题短串串长太短不好卡.比如给出的 ...
- 【暑假】[实用数据结构]UVAlive 4670 Dominating Patterns
UVAlive 4670 Dominating Patterns 题目: Dominating Patterns Time Limit: 3000MS Memory Limit: Unkn ...
随机推荐
- UI5-文档-2.3-使用SAPUI5工具为Eclipse开发应用程序
用于为简单用例开发应用程序.用于Eclipse的SAPUI5应用程序开发工具提供向导来支持您以一种简单的方式创建应用程序.使用application project向导,将自动创建包含视图和控制器的必 ...
- Redis用在哪里
1. 高并发缓存/共享session: UserInfo getUserInfo (long id) {} 取: userRedisKey = "user:info: ...
- ubuntu 命令安装软件
终端安装(命令安装).第一,找到终端或者按住Ctrl+Alt+t 打开终端;第二输入命令,命令如下:cd /xxx/xxx/,(xxx代表软件包路径,一直到你放置软件包的文件夹),之后输入命令:sud ...
- Object-c中的单例
#import <UIKit/UIKit.h> @interface UniAudioPlayer:NSObject{ } +(UniAudioPlayer*) getInstance; ...
- How to Pronounce INTERNATIONAL
How to Pronounce INTERNATIONAL Share Tweet Share Tagged With: Dropped T How do you pronounce this lo ...
- Haskell语言学习笔记(61)Distributive
Distributive class Functor g => Distributive g where distribute :: Functor f => f (g a) -> ...
- TensorFlow RNN MNIST字符识别演示快速了解TF RNN核心框架
TensorFlow RNN MNIST字符识别演示快速了解TF RNN核心框架 http://blog.sina.com.cn/s/blog_4b0020f30102wv4l.html
- python闭包和装饰器(转)
一.python闭包 1.内嵌函数 >>> def func1(): ... print ('func1 running...') ... def func2(): ... prin ...
- JDK、JRE和JAR区别(转载)
JDK里面的工具也是用Java编写的,它们本身运行的时候也需要一套JRE,如C:/Program Files/Java/jdk1.5.x/目录下的JRE.而C:/Program Files/Java/ ...
- Spring WebMVC 4.1返回json时 406(Not Acceptable)
1.问题现象Tomcat7+Spring4.1.4,返回json字符串时发生406错误 The resource identified by this request is only capable ...