题意:给定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自动机)的更多相关文章

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

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

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

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

  3. UVALive - 4670 Dominating Patterns AC 自动机

    input n 1<=n<=150 word1 word2 ... wordn 1<=len(wirdi)<=70 s 1<=len(s)<=1000000 out ...

  4. UVa1449 - Dominating Patterns(AC自动机)

    题目大意 给定n个由小写字母组成的字符串和一个文本串T,你的任务是找出那些字符串在文本中出现的次数最多 题解 一个文本串,多个模式串,这刚好是AC自动机处理的问题 代码: #include <i ...

  5. UVa 1449 - Dominating Patterns (AC自动机)

    题目大意:给出多个字符串模板,并给出一个文本串,求在文本串中出现最多的模板,输出最多的次数并输出该模板(若有多个满足,则按输入顺序输出). 思路:赤裸裸的 AC自动机,上模板. 代码: #includ ...

  6. LA4670 Dominating Patterns AC自动机模板

    Dominating Patterns 每次看着别人的代码改成自己的模板都很头大...空间少了个0卡了好久 裸题,用比map + string更高效的vector代替蓝书中的处理方法 #include ...

  7. AC自动机 LA 4670 Dominating Patterns

    题目传送门 题意:训练指南P216 分析:求出现最多次数的字串,那么对每个字串映射id,cnt记录次数求最大就可以了. #include <bits/stdc++.h> using nam ...

  8. UVa Live 4670 Dominating Patterns - Aho-Corasick自动机

    题目传送门 快速的通道I 快速的通道II 题目大意 给定一堆短串,和一个文本串,问哪些短串在文本串中出现的次数最多. 我觉得刘汝佳的做法,时间复杂度有问题.只是似乎这道题短串串长太短不好卡.比如给出的 ...

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

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

随机推荐

  1. Python之关系字段

    参考:https://blog.csdn.net/pugongying1988/article/details/72870264 关系字段:一对一,多对一,多对多 一对一:  现在有很多一对一辅导班, ...

  2. idea常用设置(持续更新)

    1.注释模板 Setting里找到live Templates (1)创建一个Templates Group (2)在Templates Group下创建Live Template 2.常用内置模板 ...

  3. ssh反向连接内网主机

    holer听别人说也挺好用不过本人没试过:https://github.com/Wisdom-Projects/holer 利用autossh建立稳定隧道,前提双方互加公钥信任. # yum inst ...

  4. keras—多层感知器MLP—IMDb情感分析

    import urllib.request import os import tarfile from keras.datasets import imdb from keras.preprocess ...

  5. 使用ASP.NET AJAX 从脚本中调用Web 服务的应用方法

    技能点:通过编写WebService,在页面js中调用WebService来进行数据查询. 网站开发,有些时候需要使用js在页面动态生成一些内容,但还有些数据要通过查询数据库才能获取的. 但由于诸如主 ...

  6. 转)安装svn服务器

    以下转载自:http://www.linuxidc.com/Linux/2015-01/111956.htm 安装 安装软件包: sudo apt-get install subversion 配置 ...

  7. time,datetime,时间戳 时间格式转换

    总结: time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) datetime.datetime.now().strftime( ...

  8. day12:vcp考试

    Q221. An administrator is creating a new Platform Service Controller Password Policy with the follow ...

  9. MongoDB的数据类型(四)

    JSON JSON是一种简单的数据表示方式,它易于理解.易于解析.易于记忆.但从另一方面来说,因为只有null.布尔.数字.字符串.数组和对象这几种数据类型,所以JSON有一定局限性.例如,JSON没 ...

  10. linq to sql之like

    contains——like '%提交%' StartsWith—— like '条件%' EndWith——like '%条件'