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自动机模板的更多相关文章

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

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

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

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

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

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

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

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

  5. UVALive - 4670 Dominating Patterns AC 自动机

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

  6. HDU 2222 AC自动机模板题

    题目: http://acm.hdu.edu.cn/showproblem.php?pid=2222 AC自动机模板题 我现在对AC自动机的理解还一般,就贴一下我参考学习的两篇博客的链接: http: ...

  7. Match:Keywords Search(AC自动机模板)(HDU 2222)

    多模匹配 题目大意:给定很多个字串A,B,C,D,E....,然后再给你目标串str字串,看目标串中出现多少个给定的字串. 经典AC自动机模板题,不多说. #include <iostream& ...

  8. HDU 3065 (AC自动机模板题)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3065 题目大意:多个模式串,范围是大写字母.匹配串的字符范围是(0~127).问匹配串中含有哪几种模 ...

  9. HDU 2896 (AC自动机模板题)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2896 题目大意:多个模式串.多个匹配串.其中串的字符范围是(0~127).问匹配串中含有哪几个模式串 ...

随机推荐

  1. MVC中利用ViewBag传递Json数据时的前端处理方法

    用viewBag传递Json字符串到前端时,json字符串中的“会被转义为& quot,前端处理方法为@Html.Raw(Json.Encode(ViewBag.Data)),再用eval() ...

  2. 【JZOJ3423】Vani和Cl2捉迷藏&【BZOJ1143】祭祀river

    description vani和cl2在一片树林里捉迷藏-- 这片树林里有N座房子,M条有向道路,组成了一张有向无环图. 树林里的树非常茂密,足以遮挡视线,但是沿着道路望去,却是视野开阔.如果从房子 ...

  3. 新一代云WAF:防御能力智能化,用户享有规则“自主权”

    近日,在国际权威分析机构Frost & Sullivan发布的<2017年亚太区Web应用防火墙市场报告>中,阿里云以市场占有率45.8%的绝对优势连续两年领跑大中华区云WAF市场 ...

  4. VS2010-MFC(对话框:非模态对话框的创建及显示)

    转自:http://www.jizhuomi.com/software/162.html 前面已经说过,非模态对话框显示后,程序其他窗口仍能正常运行,可以响应用户输入,还可以相互切换.本节会将上一讲中 ...

  5. Cesium官方教程11--建模人员必读

    原文地址:https://cesium.com/blog/2014/12/15/gltf-tips-for-artists/ 这篇文章是Branden Coker, an artist from AG ...

  6. 《DSP using MATLAB》Problem 8.8

    代码: %% ------------------------------------------------------------------------ %% Output Info about ...

  7. gstore安装

    gstore要求安装在linux系统中,如果你的电脑是windows系统,最好安装一个linux虚拟机或者安装一个Docker 在我的Docker安装中已经讲了Docker的安装过程,下面接着讲如何安 ...

  8. java调js基础

    public static void main(String[] args)throws Exception { ScriptEngine se = new ScriptEngineManager() ...

  9. python 第三方库的安装方法

    一.看更大的python世界 python 全球计算生态的主站:python 社区 www.pypi.org 二.安装第三方库 1) pip 命令安装方法 1.1 安装第三方库 命令行输入pip in ...

  10. 廖雪峰Java9正则表达式-2正则表达式进阶-5非贪婪匹配

    1.贪婪匹配 问题:给定一个字符串表示的数字,判断该数字末尾0的个数? "123000": 3个0 "10100": 2个0 "1001": ...