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. layui之input里格式验证

    form.verify({          title: function(value){            if(value.length < 5){              retu ...

  2. NEERC 2015 Adjustment Office /// oj25993

    题目大意: 输入n,q: 矩阵大小为n*n 每个位置的值为该位置的行数+列数 接下来q行 “R m”表示输出第m行的总和并整行消去 “C m”表示输出第m列的总和并整列消去 Sample Input ...

  3. CAS机制详解

    目录 1. 定义 2. 实现原理 3. 无版本号CAS实战说明 4. CAS机制在Java中的应用 5. CAS的缺点 1. CPU开销过大 2. 不能保证代码块的原子性 3. ABA问题 6. JA ...

  4. adb 使用记录

    127.0.0.1:21503 adb kill -server adb start -server adb devices adb logcat | fing "cocos" a ...

  5. JavaScript中定义函数的几种方式

    函数的组成:函数名 + 函数体 1.使用function关键字定义函数 -- 具有优先级,优先将function关键字定义的函数优先执行 function  functionName(arg0, ar ...

  6. soj97 旅行

    题意:给你一棵n个点的树.m个操作,op 1:在点i上建立银行.op 2:询问从点x开始可以经过至少一个银行走到的点中编号第二大的点. n,m<=1e5. 标程: #include<bit ...

  7. task code

    using System; using System.Collections.Generic; using System.Threading; using System.Threading.Tasks ...

  8. python requests 高级用法 -- 包括SSL 证书错误的解决方案

    Session Objects会话对象 Session对象在请求时允许你坚持一定的参数.此外,还坚持由Session实例的所有请求的cookie. 让我们坚持在请求时使用 s = requests.S ...

  9. CentOS7服务器中apache、php7以及mysql5.7的安装配置代码

    CentOS7服务器中apache.php7以及mysql5.7的配置代码如下所示: yum upgradeyum install net-tools 安装apache (http://m.86822 ...

  10. 廖雪峰Java14Java操作XML和JSON-1XML-2DOM

    XML是一种数据表示形式. 可以描述非常复杂的数据数据结构 用于传输和传输数据 DOM:Document Object Model DOM模型就是把XML文档作为一个树形结构,从根结点开始,每个节点都 ...