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. linq to sql any和all的区别

    Any说明:用于判断集合中是否有元素满足某一条件:不延迟.(若条件为空,则集合只要不为空就返回True,否则为False).1.简单形式:仅返回没有订单的客户:var q =from c in db. ...

  2. HttpClientUitl工具类

    public class HttpClient { private CloseableHttpClient httpClient; public HttpClient() { this.httpCli ...

  3. Amazon DynamoDB

  4. Codeforces Round #478 Div2 975A 975B 975C 975D

    A. Aramic script 题目大意:   对于每个单词,定义一种集合,这个集合包含且仅包含单词中出现的字母.给你一堆单词,问有多少种这种集合. 题解:   状压,插入set,取size #in ...

  5. Entity Framework Code First 模式-建立一对多联系

    一.建立一对多联系 使用的例子为Product与Category,一个种类(Product)对应多个商品(Product) 1.外键列名默认约定 在“一”这边的实体增加一个集合属性(public vi ...

  6. Vue创建项目环境

    目录 Vue项目环境搭建 Vue项目创建 pycharm配置并启动vue项目 vue项目目录结构分析 vue组件(.vue文件) 全局脚本文件main.js(项目入口) 改写 Vue项目环境搭建 &q ...

  7. iOS之CAShapeLayer属性简介

    1.CAShapeLayer需要和贝塞尔曲线一块使用! #import <QuartzCore/CALayer.h> NS_ASSUME_NONNULL_BEGIN CA_CLASS_AV ...

  8. 《DSP using MATLAB》Problem 8.29

    来汉有一月,往日的高温由于最近几个台风沿海登陆影响,今天终于下雨了,凉爽了几个小时. 接着做题. %% ------------------------------------------------ ...

  9. HBase Ganglia

  10. 全面解决Html页面缓存的问题

    页面缓存的问题可能大家都遇到过,很多功能做完没起效果,那么怎么解决这个问题呢?这里给出我的使用的解决方法 对于一个html页面,缓存分3部分,一个是页面内容,一个是css样式,一个是JS文件1.页面内 ...