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 题意:给出一些单词,求多少个单词在字符串中出现过(单词表单词可能有相同的,这些相同的单词视为不同的分别计数 ...
随机推荐
- DOM元素查找
一.DOM是document的缩写,他是操作html文档的方法 二.常用查找元素的方法 直接 1.document.getElementById('标签的id') 在html中标签的id是不允许重 ...
- MySQL设置只读模式
MySQL设置了主从复制,为保证数据一致性需要在从库设置只读状态 查看默认读写状态 show global variables like "%read_only%"; 设置只读 # ...
- API(三)之Class-based Views
使用基于类的视图重写API 我们首先将根视图重写为基于类的视图.所有这一切都涉及到重构views.py. from snippets.models import Snippet from snippe ...
- [No0000B8]WPF或Winform调用系统Console控制台显示信息
using System; using System.Collections.Generic; using System.Diagnostics; using System.IO; using Sys ...
- Win10+Ubuntu18.04 UEFI启动模式SSD+HDD
新手操作徒手安装Ubuntu,踩坑无数. 分享一篇好的经验:https://blog.csdn.net/xrinosvip/article/details/80428133(下附原博客) 踩坑大集合: ...
- [dpdk] dpdk多线程任务调度
DPDK下的线程,叫做EAL线程. EAL线程默认是与CPU core一对一绑定的,这样的话,有一些实时性,计算量不高的任务独占CORE是一种浪费,大概找了如下几种解决方案. 1. dpdk seri ...
- [CentOS7][ssh][publickey][troubleshoot] 通过密钥登录ssh故障排查
通常情况下,我会使用非对称加密的方式来进行ssh的登录. 做法: 将公钥添加到 $HOME/.ssh/authorized_keys 文件中. 但是通常,会遇见各种各样的问题,导致失败.汇总如下: 0 ...
- iOS 修改TabBar的item间距
@interface HPTabBarController ()<UITabBarControllerDelegate>//继承自UITabBarController @property ...
- idea设置文件的编码格式为utf-8
file ---> settings... 然后 editor ---> file encoding 然后设置path和encoding什么的.
- 数组的filter与includes方法
题目:编写函数 array_diff(a,b),传入两个数组a,b,将数组a中包含b的值全部去掉,重复的也去掉,返回去掉之后新数组 function array_diff(a, b) { return ...