https://vjudge.net/problem/UVALive-4670

中文题面:https://www.luogu.org/problem/show?pid=3796

AC自动机模板

注意如果有重复字符串,要输出所有的重复字符串

可以用重复字符串中标号最小的字符串来表示所有的重复字符串

例:

aba

abba

aba

aba出现了2次,

令mp[1]=mp[3]=1

代码实现的话,只需要在insert的最后判断,

如果这个单词节点还没有标记,标记上这个单词的编号

如果有标记,令当前单词的mp等于单词节点的标记

#include<queue>
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std; char s[],ss[][];
int sum[],bl[*];
int trie[*][],tot;
int len,root,id;
int f[*];
int mp[];
queue<int>q; struct AhoCorasickAutomata
{
void insert(int j)
{
len=strlen(ss[j]);
root=;
for(int i=;i<len;i++)
{
id=ss[j][i]-'a';
if(!trie[root][id])
{
trie[root][id]=++tot;
bl[tot]=;
memset(trie[tot],,sizeof(trie[tot]));
}
root=trie[root][id];
}
if(!bl[root]) bl[root]=j;
mp[j]=bl[root];
}
void getfail()
{
memset(f,,sizeof(f));
for(int i=;i<;i++) trie[][i]=;
q.push();
int now,j;
while(!q.empty())
{
now=q.front();q.pop();
for(int i=;i<;i++)
{
if(!trie[now][i]) continue;
q.push(trie[now][i]);
j=f[now];
while(!trie[j][i]) j=f[j];
f[trie[now][i]]=trie[j][i];
}
}
}
void find()
{
len=strlen(s);
memset(sum,,sizeof(sum));
root=; int j;
for(int i=;i<len;i++)
{
id=s[i]-'a';
while(!trie[root][id]) root=f[root];
root=trie[root][id];
j=root;
while(j)
{
sum[bl[j]]++;
j=f[j];
}
}
}
}; AhoCorasickAutomata AC; int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
if(!n) return ;
tot=;
memset(trie[],,sizeof(trie[]));
memset(mp,,sizeof(mp));
for(int i=;i<=n;i++)
{
scanf("%s",ss[i]);
AC.insert(i);
}
AC.getfail();
scanf("%s",s);
AC.find();
int maxn=;
for(int i=;i<=n;i++) maxn=max(maxn,sum[i]);
printf("%d\n",maxn);
for(int i=;i<=n;i++)
if(sum[mp[i]]==maxn) puts(ss[i]);
}
}

UVALive-4670 Dominating Patterns / 洛谷 3796 【模板】AC自动机的更多相关文章

  1. 【暑假】[实用数据结构]UVAlive 4670 Dominating Patterns

    UVAlive 4670 Dominating Patterns 题目:   Dominating Patterns   Time Limit: 3000MS   Memory Limit: Unkn ...

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

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

  3. uvalive 4670 Dominating Patterns

    在文本串中找出现次数最多的子串. 思路:AC自动机模板+修改一下print函数. #include<stdio.h> #include<math.h> #include< ...

  4. UVALive - 4670 Dominating Patterns AC 自动机

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

  5. 洛谷.3808/3796.[模板]AC自动机

    题目链接:简单版,增强版 简单版: #include <cstdio> #include <cstring> const int N=1e6+5,S=26; char s[N] ...

  6. UVALive 4670 Dominating Patterns (AC自动机)

    AC自动机的裸题.学了kmp和Trie以后不难看懂. 有一些变化,比如0的定义和f的指向,和建立失配边,以及多了后缀连接数组last.没有试过把失配边直接当成普通边(一开始还是先这样写吧). #inc ...

  7. 洛谷 P3804 [模板] 后缀自动机

    题目:https://www.luogu.org/problemnew/show/P3804 模仿了一篇题解,感觉很好写啊. 代码如下: #include<cstdio> #include ...

  8. 洛谷P3808 & P3796 AC自动机模板

    题目:P3808:https://www.luogu.org/problemnew/show/P3808 P3796:https://www.luogu.org/problemnew/show/P37 ...

  9. 洛谷.3121.审查(AC自动机 链表)

    题目链接 //删掉一个单词需要前移一段位置,用链表维护就好了 复杂度O(sum(len)) #include <cstdio> #include <cstring> #defi ...

随机推荐

  1. Pipeline组项目Postmortem

    Pipeline组项目Postmortem 1.     设想和目标 1)目标我们的软件要解决什么问题?是否定义得很清楚?是否对典型用户和典型场景有清晰的描述? 我们的项目是学霸系统PipeLine, ...

  2. Alpha冲刺——第二天

    Alpha第二天 听说 031502543 周龙荣(队长) 031502615 李家鹏 031502632 伍晨薇 031502637 张柽 031502639 郑秦 1.前言 任务分配是VV.ZQ. ...

  3. LintCode-71.二叉树的锯齿形层次遍历

    二叉树的锯齿形层次遍历 给出一棵二叉树,返回其节点值的锯齿形层次遍历(先从左往右,下一层再从右往左,层与层之间交替进行) 样例 给出一棵二叉树 {3,9,20,#,#,15,7}, 返回其锯齿形的层次 ...

  4. php mongodb扩展 其他扩展也类似

    MongoDBPHP 扩展 本教程将向大家介绍如何在Linux.window.Mac平台上安装MongoDB扩展. Linux上安装 MongoDB PHP扩展 在终端上安装 你可以在linux中执行 ...

  5. C#中Console.ReadLine()和Console.Read()有何区别?

    Console.Read 表示从控制台读取字符串,不换行. Console.ReadLine 表示从控制台读取字符串后进行换行. Console.Read() Console.ReadLine()方法 ...

  6. Mysql查询优化从入门到跑路(三)查询的基本操作

    查询的基本操作 1.选择操作 对应的是限制条件,操作对象是二维表的行.     优化方式:选择操作下推     目的:尽量减少连接操作前的元租数,使得中间临时关系尽量少(元祖数少,连接得到的元组数就少 ...

  7. 命名空间(namespace)// 友元函数

    17.2.命名空间 命名空间(namespace)为防止名字冲突提供了更加可控的机制.命名空间能够划分全局命名空间,这样使用独立开发的库更加容易了.一个命名空间就是一个作用域,通过在命名空间内部定义库 ...

  8. 用select (多路复用)模拟一个 socket server

    需求:用select (多路复用)模拟一个 socket server.可以接收多并发. 1. 一开始是检测自己,如果我有活动了,就说明有客户端要连我了. #用select去模拟socket,实现单线 ...

  9. CentOS7 从查看、启动、停止服务说起systemctl

    执行命令“systemctl status 服务名.service”可查看服务的运行状态,其中服务名后的.service 可以省略,这是CenOS7以后采用systemd作为初始化进程后产生的变化. ...

  10. POJ3415:Common Substrings——题解

    http://poj.org/problem?id=3415 给定两个字符串A 和B,求长度不小于k 的公共子串的个数(可以相同). 论文题,和上道题(POJ2774)类似,首先想到现将AB串合并,然 ...