CodeForces666E Forensic Examination
题目描述
给你一个串S以及一个字符串数组T[1..m],q次询问,每次问S的子串S[pl..pr]在T[l..r]中的哪个串里的出现次数最多,并输出出现次数。
如有多解输出最靠前的那一个。
题解
算是道字符串比较套路的题吧。
对模式串建SAM,对所有模式串的所有前缀维护right集合。
然后对于每个询问,倍增找到关键点,查子树众数。
坑:在最匹配串做匹配的时候,要记录匹配长度,如果匹配长度不够询问长度,直接判无解。
代码
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#define N 1000009
using namespace std;
typedef long long ll;
int ch[N][],tott,tr[N*],id[N*],ls[N*],rs[N*],ans1,ans2,fa[N],mat[N];
int cnt,last,l[N],tot,head[N],n,p[][N],deep[N],T[N],tag[N];
char s[N],s1[N];
inline int rd(){
int x=;char c=getchar();bool f=;
while(!isdigit(c)){if(c=='-')f=;c=getchar();}
while(isdigit(c)){x=(x<<)+(x<<)+(c^);c=getchar();}
return f?-x:x;
}
void upd(int &cnt,int l,int r,int x){
if(!cnt)cnt=++tott;
if(l==r){tr[cnt]++;id[cnt]=l;return;}
int mid=(l+r)>>;
if(mid>=x)upd(ls[cnt],l,mid,x);
else upd(rs[cnt],mid+,r,x);
tr[cnt]=max(tr[ls[cnt]],tr[rs[cnt]]);
id[cnt]=tr[ls[cnt]]>=tr[rs[cnt]]?id[ls[cnt]]:id[rs[cnt]];
}
int merge(int x,int y,int l,int r){
if(!x||!y)return x^y;
int p=++tott;
if(l==r){tr[p]=tr[x]+tr[y];id[p]=l;return p;}
int mid=(l+r)>>;
ls[p]=merge(ls[x],ls[y],l,mid);rs[p]=merge(rs[x],rs[y],mid+,r);
tr[p]=max(tr[ls[p]],tr[rs[p]]);
id[p]=tr[ls[p]]>=tr[rs[p]]?id[ls[p]]:id[rs[p]];
return p;
}
void query(int cnt,int l,int r,int L,int R){
if(!cnt)return;
if(l>=L&&r<=R){
if(tr[cnt]>ans1){
ans1=tr[cnt];
ans2=id[cnt];
}
return;
}
int mid=(l+r)>>;
if(mid>=L)query(ls[cnt],l,mid,L,R);
if(mid<R)query(rs[cnt],mid+,r,L,R);
}
inline void ins(int x,int id){
if(ch[last][x]){
int p=last,q=ch[last][x];
if(l[p]+==l[q])last=q;
else{
int nq=++cnt;l[nq]=l[p]+;
memcpy(ch[nq],ch[q],sizeof(ch[nq]));
fa[nq]=fa[q];fa[q]=nq;
for(;ch[p][x]==q;p=fa[p])ch[p][x]=nq;
last=nq;
}
}
else{
int p=last,np=++cnt;l[np]=l[p]+;last=np;
for(;p&&!ch[p][x];p=fa[p])ch[p][x]=np;
if(!p)fa[np]=;
else{
int q=ch[p][x];
if(l[p]+==l[q])fa[np]=q;
else{
int nq=++cnt;l[nq]=l[p]+;
memcpy(ch[nq],ch[q],sizeof(ch[nq]));
fa[nq]=fa[q];fa[q]=fa[np]=nq;
for(;ch[p][x]==q;p=fa[p])ch[p][x]=nq;
}
}
}
upd(T[last],,n,id);
}
struct edge{int n,to;}e[N];
inline void add(int u,int v){e[++tot].n=head[u];e[tot].to=v;head[u]=tot;}
void dfs(int u){
for(int i=;(<<i)<=deep[u];++i)p[i][u]=p[i-][p[i-][u]];
for(int i=head[u];i;i=e[i].n){
int v=e[i].to;deep[v]=deep[u]+;p[][v]=u;
dfs(v);T[u]=merge(T[u],T[v],,n);
}
}
int main(){
scanf("%s",s+);
n=rd();cnt=;
for(int i=;i<=n;++i){
last=;
scanf("%s",s1);int len=strlen(s1);
for(int j=;j<len;++j)ins(s1[j]-'a',i);
}
for(int i=;i<=cnt;++i)if(fa[i])add(fa[i],i);
dfs();int len=strlen(s+),now=,le=;
for(int i=;i<=len;++i){
while(now&&!ch[now][s[i]-'a'])now=fa[now],le=l[now];
if(!now)now=;
if(ch[now][s[i]-'a'])now=ch[now][s[i]-'a'],le++;
tag[i]=now;mat[i]=le;
}
int q=rd(),l1,r1,l2,r2;
while(q--){
l2=rd();r2=rd();l1=rd();r1=rd();
int x=tag[r1];
if(!x||mat[r1]<r1-l1+){
printf("%d 0\n",l2);continue;
}
for(int i=;i>=;--i)if(l[p[i][x]]>=r1-l1+)x=p[i][x]; ans1=;ans2=l2;;
query(T[x],,n,l2,r2);
printf("%d %d\n",ans2,ans1);
}
return ;
}
CodeForces666E Forensic Examination的更多相关文章
- 【Codeforces666E】Forensic Examination 后缀自动机 + 线段树合并
E. Forensic Examination time limit per test:6 seconds memory limit per test:768 megabytes input:stan ...
- Codeforces 666E E - Forensic Examination SA + 莫队 + 线段树
E - Forensic Examination 我也不知道为什么这个复杂度能过, 而且跑得还挺快, 数据比较水? 在sa上二分出上下界, 然后莫队 + 线段树维护区间众数. #include< ...
- 【CF666E】Forensic Examination 广义后缀自动机+倍增+线段树合并
[CF666E]Forensic Examination 题意:给你一个字符串s和一个字符串集合$\{t_i\}$.有q个询问,每次给出$l,r,p_l,p_r$,问$s[p_l,p_r]$在$t_l ...
- 【CF666E】Forensic Examination(后缀自动机,线段树合并)
[CF666E]Forensic Examination(后缀自动机,线段树合并) 题面 洛谷 CF 翻译: 给定一个串\(S\)和若干个串\(T_i\) 每次询问\(S[pl..pr]\)在\(T_ ...
- Codeforces 666E Forensic Examination SAM or SA+线段树合并
E. Forensic Examination http://codeforces.com/problemset/problem/666/E 题目大意:给模式串S以及m个特殊串,q个询问,询问S的子串 ...
- CF 666E Forensic Examination 【SAM 倍增 线段树合并】
CF 666E Forensic Examination 题意: 给出一个串\(s\)和\(n\)个串\(t_i\),\(q\)次询问,每次询问串\(s\)的子串\(s[p_l:p_r]\)在串\(t ...
- 【codeforces666E】Forensic Examination 广义后缀自动机+树上倍增+线段树合并
题目描述 给出 $S$ 串和 $m$ 个 $T_i$ 串,$q$ 次询问,每次询问给出 $l$ .$r$ .$x$ .$y$ ,求 $S_{x...y}$ 在 $T_l,T_{l+1},...,T_r ...
- Codeforces 666E Forensic Examination
题意:给定主串s和m个模式串,每次询问[l,r]的模式串中出现s[pl...pr]次数最多的串和次数. 这题挺简单的,先把所有模式串拿来建广义后缀自动机,询问相当于子树众数,用线段树合并即可. 那我为 ...
- [CF 666E] Forensic Examination
Description 传送门 Solution 对 \(T[1..m]\) 建立广义后缀自动机,离线,找出代表 \(S[pl,pr]\) 的每个节点,线段树合并. Code #include < ...
随机推荐
- Mysql增量写入Hdfs(二) --Storm+hdfs的流式处理
一. 概述 上一篇我们介绍了如何将数据从mysql抛到kafka,这次我们就专注于利用storm将数据写入到hdfs的过程,由于storm写入hdfs的可定制东西有些多,我们先不从kafka读取,而先 ...
- WinServer-FTP搭建
FTP服务器(File Transfer Protocol Server)是在互联网上提供文件存储和访问服务的计算机,它们依照FTP协议提供服务. FTP是File Transfer Protocol ...
- 使用Linq的泛型功能
泛型数据访问类: 业务抽象类使用数据访问类: 业务类继承业务抽象类: 使用业务类:
- ES6使用的一些方法
查找数组中符合条件的所有记录 var list=[ {id:1,name:"张三"}, {id:2,name:"李四"}, {id:3,name:"王 ...
- Failed to initialize component [org.apache.catalina.webresources.JarResource
去本地仓库里删除掉对应出错的jar包,然后回到pom里面加个空行 让他重新下载(最好把maven仓库全部都删了 重新下载一次 ) 先备份 在复制回来 完美解决
- 【PAT】A1034Head of a Gang
昨天准备学完图相关的知识,但是学起来挺懵的,理解起来不难,但自己一回想,又什么都想不起来. 翻来覆去看图的遍历,还是觉得有点没到位. 所以做题来检测一下,果然学和自己做是两码事. 先看的书,又看的柳婼 ...
- ansible学习基础知识和模块(一)
基础知识补充: 常用自动化运维工具 Ansible:使用python来开发的,无需设置Agentless(代理),一般管理几百台.与ssh的方式也不一样,ssh是基于c/s模式(客户端+服务器)来使用 ...
- ASP.NET MVC5学习系列——身份验证、授权
一.什么是身份验证和授权 人们有时对用户身份验证和用户授权之间的区别感到疑惑.用户身份验证是指通过某种形式的登录机制(包括用户名/密码.OpenID.OAuth等说明身份的项)来核实用户的身份.授权验 ...
- 网络流二十四题之P2764 最小路径覆盖问题
题目描述 给定有向图 G=(V,E)G=(V,E) .设 PP 是 GG 的一个简单路(顶点不相交)的集合.如果 VV 中每个定点恰好在PP的一条路上,则称 PP 是 GG 的一个路径覆盖.PP中路径 ...
- Nginx代理的几种模式
转载自一位大佬 通常我们都知道Nginx性能很高,尤其是作为一个代理服务器,因为它用的是epoll模型,就比如Python Django Web的性能不行,我们可能就会在前端加一个nginx代理,从而 ...