LA 4670 Dominating Patterns (AC自动机)
题意:给定一个一篇文章,然后下面有一些单词,问这些单词在这文章中出现过几次。
析:这是一个AC自动机的裸板,最后在匹配完之后再统计数目就好。
代码如下:
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#define debug puts("+++++")
//#include <tr1/unordered_map>
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std;
//using namespace std :: tr1; typedef long long LL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f;
const LL LNF = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 1e6 + 5;
const LL mod = 2147493647;
const int N = 1e6 + 5;
const int dr[] = {-1, 0, 1, 0, 1, 1, -1, -1};
const int dc[] = {0, 1, 0, -1, 1, -1, 1, -1};
const char *Hex[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
inline LL gcd(LL a, LL b){ return b == 0 ? a : gcd(b, a%b); }
inline int gcd(int a, int b){ return b == 0 ? a : gcd(b, a%b); }
inline int lcm(int a, int b){ return a * b / gcd(a, b); }
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline int Min(int a, int b){ return a < b ? a : b; }
inline int Max(int a, int b){ return a > b ? a : b; }
inline LL Min(LL a, LL b){ return a < b ? a : b; }
inline LL Max(LL a, LL b){ return a > b ? a : b; }
inline bool is_in(int r, int c){
return r >= 0 && r < n && c >= 0 && c < m;
}
const int sigma = 26;
const int maxnode = 70 * 150 + 5; struct Aho{
int cnt[155];
int ch[maxnode][sigma];
int f[maxnode];
int val[maxnode];
int last[maxnode];
int sz;
void init(){
sz = 1;
memset(ch[0], 0, sizeof ch[0]);
memset(cnt, 0, sizeof cnt);
} int idx(char ch){ return ch - 'a'; } void print(int j){
if(j){
++cnt[val[j]];
print(last[j]);
}
}
void insert(char *s, int v){
int u = 0;
while(*s){
int c = idx(*s);
if(!ch[u][c]){
memset(ch[sz], 0, sizeof ch[sz]);
val[sz] = 0;
ch[u][c] = sz++;
}
u = ch[u][c]; ++s;
}
val[u] = v; } void find(char *s){
int j = 0;
while(*s){
int c = idx(*s);
while(j && !ch[j][c]) j = f[j];
j = ch[j][c];
if(val[j]) print(j);
else if(last[j]) print(last[j]);
++s;
}
} void 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; }
} 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]];
}
}
}
}; Aho aho;
char s[155][75];
char t[1000005];
map<string, int> mp; int main(){
while(scanf("%d", &n) == 1 && n){
aho.init();
mp.clear();
for(int i = 1; i <= n; ++i){
scanf("%s", s[i]);
aho.insert(s[i], i);
mp[s[i]] = i;
}
scanf("%s", t);
aho.getFail();
aho.find(t);
int mmax = -1;
for(int i = 1; i <= n; ++i) mmax = Max(mmax, aho.cnt[i]);
printf("%d\n", mmax);
for(int i = 1; i <= n; ++i)
if(aho.cnt[mp[s[i]]] == mmax) printf("%s\n", s[i]);
}
return 0;
}
LA 4670 Dominating Patterns (AC自动机)的更多相关文章
- 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 ...
- LA 4670 Dominating Patterns (AC自动机)
题意:给定n个字符串和一个文本串,查找哪个字符串出现的次数的最多. 析:一匹配多,很明显是AC自动机.只需要对原来的进行修改一下,就可以得到这个题的答案, 计算过程中,要更新次数,并且要映射字符串.如 ...
- 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 ...
随机推荐
- msp430入门编程35
msp430中C语言的可移植--规划软件层次
- [洛谷U22156]未曾届到游览(矩阵树定理)
题目背景 又到了某任*堂开关中学一年一度的自主招生考试的时间了,在考试完后许多家长决定带着自己的孩子参观一下这所距千年名校还有890周年的百年学校: 题目描述 这所学校的布局非常奇怪,是一个由N 个点 ...
- 学习日常笔记<day14>自定义标签
1自定义标签 1.1第一个自定义标签开发步骤 1)编写一个普通的java类,继承SimpleTagSupport类,叫标签处理器类 /** * 标签处理器类 * @author APPle * 1)继 ...
- 分享一下然让显卡满血复活的小技巧(GTX)
分享一下然让显卡满血复活的小技巧 笔者在玩大型游戏卡顿15fps下载如下操作 GTX950玩大型游戏都不会卡帧率稳定在30fps 下载GeForce Experience下载更新最新驱动 下载如下程序 ...
- Docker安装redis操作命令
最近学习了redis,那么今天我们来学习以下在Docker上安装我们的redis,并查看有关redis的一系列命令 查找redis docker search redis 拉取redis docker ...
- windows安装ZIP压缩版的Weblogic Server
以前要装Weblogic Server的时候都是装的安装版,最近发现ZIP版本的Weblogic Server是一个只包含Weblogic Server的版本,于是就想着弄一下它. 这里用到的Webl ...
- 百度统计的JS脚本原理解析
一句话:在你的网站上加载百度统计的脚本,这个脚本会收集你的本地信息,然后发送给百度统计网站 https://blog.csdn.net/iqzq123/article/details/8877645 ...
- 判断所ping主机的操作系统
根据它的值判断所ping主机的操作系统类型. TTL被称为生存期,也就是你所传输的数据在网络上经过的路由器的最大个数. 操作系统 TTLLINUX 64WIN2K/NT 128WINDOWS 系列 3 ...
- Angular2.x-服务
heroes之旅HeroesComponent目前正在获取并显示虚假数据. 在本教程重构之后,HeroesComponent将会精益求精并专注于支持视图.用模拟服务进行单元测试也会更容易. 为什么服务 ...
- 【Sublime】Sublime Text 2集成TortoiseSVN插件
作者:zhanhailiang 日期:2014-09-30 1. 下载TortoiseSVN.将其安装在默认位置: 2. 使用Sublime包管理器下载安装TortoiseSVN Plugin,安装后 ...