AC自动机模板2
题目链接:https://www.luogu.org/problemnew/show/P3796
#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <cstdlib>
#include <sstream>
#include <iostream>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <algorithm>
#include <functional>
using namespace std;
#define ll long long
#define re register
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define P pair<int,int>
const int N=1e6+;
const int mod=1e9+;
void read(int &a)
{
a=;
int d=;
char ch;
while(ch=getchar(),ch>''||ch<'')
if(ch=='-')
d=-;
a=ch-'';
while(ch=getchar(),ch>=''&&ch<='')
a=a*+ch-'';
a*=d;
}
void write(int x)
{
if(x<)
putchar(),x=-x;
if(x>)
write(x/);
putchar(x%+'');
}
struct note
{
int next;
int to[];
int bz;
}trie[N];
int tot;
struct node
{
int sum,pos;
}ans[];
bool cmp(node x,node y)
{
if(x.sum!=y.sum)
return x.sum>y.sum;
else
return x.pos<y.pos;
}
string s[];
inline void clean(int x)
{
trie[x].next=;
trie[x].bz=;
memset(trie[x].to,,sizeof(trie[x].to));
}
inline void ins(string s,int k)
{
int len=s.length();
int now=;
for(re int i=;i<len;i++)
{
int x=s[i]-;
if(!trie[now].to[x])
trie[now].to[x]=++tot,clean(tot);
now=trie[now].to[x];
}
trie[now].bz=k;
}
inline void built()
{
queue <int> q;
for(re int i=;i<=;i++)
if(trie[].to[i])
trie[trie[].to[i]].next=,q.push(trie[].to[i]);
while(!q.empty())
{
int p=q.front();
q.pop();
for(re int i=;i<=;i++)
{
if(trie[p].to[i])
trie[trie[p].to[i]].next=trie[trie[p].next].to[i],q.push(trie[p].to[i]);
else
trie[p].to[i]=trie[trie[p].next].to[i];
}
}
}
inline void solve(string s)
{
int len=s.length();
int now=;
for(re int i=;i<len;i++)
{
int x=s[i]-;
now=trie[now].to[x];
for(re int t=now;t;t=trie[t].next)
ans[trie[t].bz].sum++;
}
}
int main()
{
int n;
while(~scanf("%d",&n)&&n)
{
tot=;
clean();
for(re int i=;i<=n;i++)
{
cin>>s[i];
ans[i].sum=;
ans[i].pos=i;
ins(s[i],i);
}
trie[].next=;
built();
cin>>s[];
solve(s[]);
sort(ans+,ans++n,cmp);
cout<<ans[].sum<<endl;
cout<<s[ans[].pos]<<endl;
for(re int i=;i<=n;i++)
{
if(ans[i].sum==ans[i-].sum)
cout<<s[ans[i].pos]<<endl;
else
break;
}
}
return ;
}
AC自动机模板2的更多相关文章
- HDU 2222 AC自动机模板题
题目: http://acm.hdu.edu.cn/showproblem.php?pid=2222 AC自动机模板题 我现在对AC自动机的理解还一般,就贴一下我参考学习的两篇博客的链接: http: ...
- Match:Keywords Search(AC自动机模板)(HDU 2222)
多模匹配 题目大意:给定很多个字串A,B,C,D,E....,然后再给你目标串str字串,看目标串中出现多少个给定的字串. 经典AC自动机模板题,不多说. #include <iostream& ...
- HDU 3065 (AC自动机模板题)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3065 题目大意:多个模式串,范围是大写字母.匹配串的字符范围是(0~127).问匹配串中含有哪几种模 ...
- HDU 2896 (AC自动机模板题)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2896 题目大意:多个模式串.多个匹配串.其中串的字符范围是(0~127).问匹配串中含有哪几个模式串 ...
- HDU 2222(AC自动机模板题)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2222 题目大意:多个模式串.问匹配串中含有多少个模式串.注意模式串有重复,所以要累计重复结果. 解题 ...
- HDU 2222 (AC自动机模板题)
题意: 给一个文本串和多个模式串,求文本串中一共出现多少次模式串 分析: ac自动机模板,关键是失配函数 #include <map> #include <set> #incl ...
- hdu 2222 Keywords Search ac自动机模板
题目链接 先整理一发ac自动机模板.. #include <iostream> #include <vector> #include <cstdio> #inclu ...
- KMP与AC自动机模板
HDU 1711 Number Sequence(KMP模板题) http://acm.hdu.edu.cn/showproblem.php?pid=1711 #include<bits/std ...
- HDU3695(AC自动机模板题)
题意:给你n个字符串,再给你一个大的字符串A,问你着n个字符串在正的A和反的A里出现多少个? 其实就是AC自动机模板题啊( ╯□╰ ) 正着query一次再反着query一次就好了 /* gyt Li ...
- POJ2222 Keywords Search AC自动机模板
http://acm.hdu.edu.cn/showproblem.php?pid=2222 题意:给出一些单词,求多少个单词在字符串中出现过(单词表单词可能有相同的,这些相同的单词视为不同的分别计数 ...
随机推荐
- ELK之从6.3.1升级至6.6.2
需要把原6.3.1版本升级为6.6.2版本 1,官网下载rpm包 2,升级elasticsearch和kibana rpm -U elasticsearch-6.6.2.rpm rpm -U kiba ...
- xiv存储操作
XIV存储操作维护手册 二○一二年七月 目录 1. 存储划分... 3 1.1. 定义Storage Pool 3 1.2. ...
- 浅谈原生JavaScript实现remove()和recover()
利用原生JavaScript实现: 1.remove(selectors)删除指定的一个或一组元素. 2.recover(selectors)恢复刚才删除的元素. function remove(se ...
- .NET Core开发日志——Peachpie
.NET Core的生态圈随着开源社区的力量不断注入至其中,正在变得越来越强盛,并且不时得就出现些有意思的项目,比如Peachpie,它使得PHP的代码迁移到.NET Core项目变得可能. 从创建简 ...
- [No0000FD]C# 正则表达式
正则表达式 是一种匹配输入文本的模式..Net 框架提供了允许这种匹配的正则表达式引擎.模式由一个或多个字符.运算符和结构组成. 定义正则表达式 下面列出了用于定义正则表达式的各种类别的字符.运算符和 ...
- Exception 04 : java.lang.ClassNotFoundException: Could not load requested class : org.hsqldb.jdbcDriver
异常详细信息 org.hibernate.boot.registry.classloading.spi.ClassLoadingException: Unable to load class [org ...
- transformations 变换集合关系 仿射变换
http://groups.csail.mit.edu/graphics/classes/6.837/F03/lectures/04_transformations.ppt https://group ...
- tornado框架&三层架构&MVC&MTV&模板语言&cookie&session
web框架的本质其实就是socket服务端再加上业务逻辑处理, 比如像是Tornado这样的框架. 有一些框架则只包含业务逻辑处理, 例如Django, bottle, flask这些框架, 它们的使 ...
- LeetCode 961 N-Repeated Element in Size 2N Array 解题报告
题目要求 In a array A of size 2N, there are N+1 unique elements, and exactly one of these elements is re ...
- 洛谷 P3684 机棚障碍Hangar Hurdles [CERC2016] 图论
正解: 解题报告: 传送门! 首先不难想到这题主要有两个问题需要解决,一个是预处理出各个点的箱子半径最大值,一个是求ans 然后分别港下QwQ 首先关于预处理要说下昂 预处理有三种方法,分别港下 第一 ...