LA4670 Dominating Patterns AC自动机模板
Dominating Patterns
每次看着别人的代码改成自己的模板都很头大。。。空间少了个0卡了好久
裸题,用比map + string更高效的vector代替蓝书中的处理方法
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <vector>
#include <cmath>
#define min(a, b) ((a) < (b) ? (a) : (b))
#define max(a, b) ((a) > (b) ? (a) : (b))
#define abs(a) ((a) < 0 ? (-1 * (a)) : (a))
inline void swap(int &a, int &b)
{
long long tmp = a;a = b;b = tmp;
}
inline void read(int &x)
{
x = ;char ch = getchar(), c = ch;
while(ch < '' || ch > '') c = ch, ch = getchar();
while(ch <= '' && ch >= '') x = x * + ch - '', ch = getchar();
if(c == '-') x = -x;
} const int INF = 0x3f3f3f3f;
const int MAXNODE = * * ; int ch[MAXNODE][], last[MAXNODE], root = , tag[MAXNODE], fail[MAXNODE], cnt, tot[MAXNODE];
std::vector<int> node[MAXNODE];
char s[][]; void insert(int x)
{
int now = root;
for(int i = ;s[x][i] != '\0';++ i)
{
int& tmp = ch[now][s[x][i] - 'a' + ];
if(tmp) now = tmp;
else now = tmp = ++ cnt;
}
++ tag[now];
node[now].push_back(x);
} int q[MAXNODE], he, ta, ma;
void build()
{
he = , ta = ; for(register int i = ;i <= ;++ i)
{
int u = ch[root][i];
if(u) q[ta ++] = u, fail[u] = last[u] = ;
} while(he < ta)
{
int now = q[he ++];
for(register int i = ;i <= ;++ i)
{
int u = ch[now][i];
if(!u)
{
ch[now][i] = ch[fail[now]][i];
continue;
}
q[ta ++] = u;
int v = fail[now];
while(v && !ch[v][i]) v = fail[v];
fail[u] = ch[v][i];
last[u] = tag[fail[u]] ? fail[u] : last[fail[u]];
}
}
} int n;
char T[]; void find()
{
int n = strlen(T + );
int j = root;
for(register int i = ;i <= n;++ i)
{
int c = T[i] - 'a' + ;
j = ch[j][c];
if(tag[j]) for(int k = ;k < node[j].size();++ k) ++ tot[node[j][k]], ma = max(ma, tot[node[j][k]]);
else if(last[j]) for(int k = ;k < node[last[j]].size();++ k) ++ tot[node[last[j]][k]], ma = max(ma, tot[node[last[j]][k]]);
}
} int main()
{
while(scanf("%d", &n) != EOF && n)
{
ma = , memset(tot, , sizeof(tot)), memset(ch, , sizeof(ch)), memset(tag, , sizeof(tag));
for(register int i = ;i <= cnt;++ i) node[i].clear();
cnt = ;
for(register int i = ;i <= n;++ i)
{
scanf("%s", s[i] + );
insert(i);
}
build();
scanf("%s", T + );
find();
printf("%d\n", ma);
for(register int i = ;i <= n;++ i)
if(tot[i] == ma) printf("%s\n", s[i] + );
}
return ;
}
LA4670
LA4670 Dominating Patterns AC自动机模板的更多相关文章
- UVALive 4670 Dominating Patterns --AC自动机第一题
题意:多个模板串,一个文本串,求出那些模板串在文本串中出现次数最多. 解法:AC自动机入门模板题. 代码: #include <iostream> #include <cstdio& ...
- UVa 1449 - Dominating Patterns (AC自动机)
题目大意:给出多个字符串模板,并给出一个文本串,求在文本串中出现最多的模板,输出最多的次数并输出该模板(若有多个满足,则按输入顺序输出). 思路:赤裸裸的 AC自动机,上模板. 代码: #includ ...
- UVa1449 - Dominating Patterns(AC自动机)
题目大意 给定n个由小写字母组成的字符串和一个文本串T,你的任务是找出那些字符串在文本中出现的次数最多 题解 一个文本串,多个模式串,这刚好是AC自动机处理的问题 代码: #include <i ...
- LA 4670 Dominating Patterns (AC自动机)
题意:给定一个一篇文章,然后下面有一些单词,问这些单词在这文章中出现过几次. 析:这是一个AC自动机的裸板,最后在匹配完之后再统计数目就好. 代码如下: #pragma comment(linker, ...
- UVALive - 4670 Dominating Patterns AC 自动机
input n 1<=n<=150 word1 word2 ... wordn 1<=len(wirdi)<=70 s 1<=len(s)<=1000000 out ...
- HDU 2222 AC自动机模板题
题目: http://acm.hdu.edu.cn/showproblem.php?pid=2222 AC自动机模板题 我现在对AC自动机的理解还一般,就贴一下我参考学习的两篇博客的链接: http: ...
- Match:Keywords Search(AC自动机模板)(HDU 2222)
多模匹配 题目大意:给定很多个字串A,B,C,D,E....,然后再给你目标串str字串,看目标串中出现多少个给定的字串. 经典AC自动机模板题,不多说. #include <iostream& ...
- HDU 3065 (AC自动机模板题)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3065 题目大意:多个模式串,范围是大写字母.匹配串的字符范围是(0~127).问匹配串中含有哪几种模 ...
- HDU 2896 (AC自动机模板题)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2896 题目大意:多个模式串.多个匹配串.其中串的字符范围是(0~127).问匹配串中含有哪几个模式串 ...
随机推荐
- Vue的指令和成员
目录 Vue的指令和成员 指令 表单 斗篷 条件 循环 成员 计算成员 监听成员 Vue的指令和成员 指令 表单 表单指令一般是和属性指令联合使用的,最常见的就是v-model="变量&qu ...
- shell脚本练习05
######################################################################### # File Name: -.sh # Author ...
- C# 中的三个高级参数 ref
今天在浏览博文时,看到这篇文章:C#中的ref 传进出的到底是什么 ? 在传对象时使用ref的疑问 引用类型就传的就是地址,值类型传的就是值,可是还仍有那么多人迷惑,网上虽然流传着很多ref 的相关文 ...
- 关系型数据库——MySQL
[MySQL架构图] MySQL简要架构图如下图所示,引擎层以插件方式集成了不同的存储引擎,它们共用Server层对外提供服务. 连接器:用于连接管理,进行身份认证及权限相关的管理.(登录MySQ ...
- 【JZOJ3301】家族
description 阿狸和桃子养了n 个小阿狸, 小阿狸们每天都在一起玩的很开心. 作为工程师的阿狸在对小阿狸们之间的关系进行研究以后发现了小阿狸的人际关系由某种神奇的相互作用决定, 阿狸称之为& ...
- thinkphp 表达式查询
上面的查询条件仅仅是一个简单的相等判断,可以使用查询表达式支持更多的SQL查询语法,也是ThinkPHP查询语言的精髓,查询表达式的使用格式: $map['字段名'] = array('表达式','查 ...
- 「题解」:[组合数学][DP]:地精部落
拿到这道题秒懂题意:波动序列. 然鹅不会打.想了一节课,想打纯组合数学,结果找不到规律. 想的是先假设拍出一个序列,然后交换其中的元素求组合, 无奈没啥规律可循,显然不能一口气求出来(我说的是我没办法 ...
- bootStrap-treeview插件
简要教程 bootstrap-treeview是一款效果非常酷的基于bootstrap的jQuery多级列表树插件.该jQuery插件基于Twitter Bootstrap,以简单和优雅的方式来显示一 ...
- pytorch基础2
下面是常见函数的代码例子 import torch import numpy as np print("分割线---------------------------------------- ...
- css过渡属性transition简单示例
2.transition 简单实例 demo1→在线预览源代码 效果 demo2→在线预览源代码 效果 demo3→在线预览源代码 效果