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. The Uncle_b's First Love

    Description ACM成立大会之后,uncle_b被其中一个大一女孩深深地吸引,但腼腆的B叔又不知道如何去表达自己内心的想法,经calmound神的指导,B叔决定写封情书给对方.他从Tamar ...

  2. c#非界面线程控制控件

    private delegate void FlushCilent(); Invoke(new FlushCilent(databaseConnect));

  3. zookeeper启动配置

    zookeeper安装和配置详解 转载 2014年04月16日 14:36:31 16812 摘自:http://www.ibm.com/developerworks/cn/opensource/os ...

  4. CodeForces - 792C Divide by Three (DP做法)

    C. Divide by Three time limit per test: 1 second memory limit per test: 256 megabytes input: standar ...

  5. keydown事件下调用trigger事件执行两次

    $('button[type=button]').on('click',login); //登录 $(document).keydown(function(event){ if(event.keyCo ...

  6. 《剑指offer》---输出链表倒数第k个结点

    本文算法使用python3实现 1 题目描述:   输入一个链表,输出该链表中倒数第k个结点.   时间限制:1s:空间限制:32768K 2 思路描述:   方法一:当链表长度为 $ n $ 时,输 ...

  7. IIS部署网部常用问题汇总

    1.unrecognized attribute 'targetframework' A: 需要注册.net framework到iis.步骤如下: (1)'Start' -> 'CMD' (2 ...

  8. BZOJ 2241 打地鼠(特技暴力)

    果然暴力出奇迹.. O(n^2m^2)=1e8 536ms能过. 枚举锤子的长和宽,再验证是否可以满足条件并更新答案. 我们先从左上角为(1,1)的先锤,显然锤的次数是a[1][1]. 锤(i,j)的 ...

  9. BZOJ4700 适者(贪心+cdq分治+斜率优化)

    首先考虑怎么安排攻击顺序.显然如果攻击了某台兵器就应该一直连续攻击直到将其破坏,破坏所需时间可以直接算出来,设其为b.假设确定了某个破坏顺序,如果交换相邻两个兵器,显然不会对其他兵器造成影响,两种顺序 ...

  10. subprocess模块详解

    subprocess是Python与系统交互的一个库,该模块允许生成新进程,连接到它们的输入/输出/错误管道,并获取它们的返回代码. 该模块旨在替换几个较旧的模块和功能: os.system os.s ...