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. 实用的ES6特性

    1. 函数参数默认值 不使用ES6 为函数的参数设置默认值: function foo(height, color) { var height = height || 50; var color = ...

  2. File Searching

    Description Have you ever used file searching tools provided by an operating system? For example, in ...

  3. Thunder团队第一周 - Scrum会议7

    Scrum会议7 小组名称:Thunder 项目名称:爱阅app Scrum Master:宋雨 工作照片: 参会成员: 王航:http://www.cnblogs.com/wangh013/ 李传康 ...

  4. Pipeline组项目Postmortem

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

  5. 通过设置窗体的AcceptButton属性,可以设置窗体的“接受”按钮,若此设计,则用户每次按下Enter键都相当于单击该按钮

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  6. Delphi XE4 TStringHelper用法详解

    原文地址:Delphi XE4 TStringHelper用法详解作者:天下为公 Delphi XE4的TStringHelper,对操作字符串进一步带来更多的方法,估计XE5还能继续用到. Syst ...

  7. UDP发送的数据 以数据包形式发送

    UDP发送的数据 以数据包形式发送

  8. 【bzoj3312】[Usaco2013 Nov]No Change 状态压缩dp+二分

    题目描述 Farmer John is at the market to purchase supplies for his farm. He has in his pocket K coins (1 ...

  9. C# 面向对象——多态

    多态分三种:1.虚方法 2.抽象类 3.接口 1.虚方法1.将父类的方法标记为虚方法 ,使用关键字 virtual,这个函数可以被子类重新写一个遍. 如: class Program { static ...

  10. Python单例模式的四种方法

    在这之前,先了解super()和__new__()方法 super()方法: 返回一个父类或兄弟类类型的代理对象,让你能够调用一些从继承过来的方法. 它有两个典型作用: a. 在单继承的类层次结构中, ...