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 ...
随机推荐
- focusin 事件| focusout事件
focusin 定义和用法 当元素(或在其内的任意元素)获得焦点时发生 focusin 事件. 当在元素或在其内的任意元素上发生 focus 事件时,focusin() 方法添加要运行的函数. 与 f ...
- JAVA发送HttpClient请求及接收请求结果过程
1.写一个HttpRequestUtils工具类,包括post请求和get请求 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 ...
- 2018.8.14-C#复习笔记总
using System; using System.Collections.Generic; //using System.Linq; using System.Text; using System ...
- 为什么要在linux命令前加上 ./
为什么要在linux命令前加上 ./ ? 简述 执行unix或linux中除了path系统变量外的目录下的命令都要加./. 修改用户的 .bash_profile,在 PATH一行最后加上 “:.” ...
- springboot 使用的配置
1,控制台打印sql logging: level: com.sdyy.test.mapper: debug 2,开启驼峰命名 mybatis.configuration.map-underscore ...
- c++实现贪食蛇
今天老师布置了作业 让我们观察c++实现贪食蛇的代码 #include<windows.h> #include<time.h> #include<stdlib.h> ...
- pipeenv error
[root@mhc tmp]# pipenv --python 3.6Creating a virtualenv for this project…Using /root/python3/bin/py ...
- 非换行空白:non-breaking space
一 维基百科(英文版)词条 In word processing and digital typesetting, a non-breaking space (" ") (also ...
- mysql数据库的最基本的命令
#查看mysql有哪些数据库:show databases; 创建一个数据库名称为DataBaseName,字符编码为utf8支持中文create database DataBaseName char ...
- 14- Servlet.service() for servlet [mvc-dispatcher] in context with path [/collegeservice] threw exception [Request processing failed; nested exception is java.lang.NullPointerException] with root caus
有的service没有依赖注入: