题目描述

给你一个串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的更多相关文章

  1. 【Codeforces666E】Forensic Examination 后缀自动机 + 线段树合并

    E. Forensic Examination time limit per test:6 seconds memory limit per test:768 megabytes input:stan ...

  2. Codeforces 666E E - Forensic Examination SA + 莫队 + 线段树

    E - Forensic Examination 我也不知道为什么这个复杂度能过, 而且跑得还挺快, 数据比较水? 在sa上二分出上下界, 然后莫队 + 线段树维护区间众数. #include< ...

  3. 【CF666E】Forensic Examination 广义后缀自动机+倍增+线段树合并

    [CF666E]Forensic Examination 题意:给你一个字符串s和一个字符串集合$\{t_i\}$.有q个询问,每次给出$l,r,p_l,p_r$,问$s[p_l,p_r]$在$t_l ...

  4. 【CF666E】Forensic Examination(后缀自动机,线段树合并)

    [CF666E]Forensic Examination(后缀自动机,线段树合并) 题面 洛谷 CF 翻译: 给定一个串\(S\)和若干个串\(T_i\) 每次询问\(S[pl..pr]\)在\(T_ ...

  5. Codeforces 666E Forensic Examination SAM or SA+线段树合并

    E. Forensic Examination http://codeforces.com/problemset/problem/666/E 题目大意:给模式串S以及m个特殊串,q个询问,询问S的子串 ...

  6. CF 666E Forensic Examination 【SAM 倍增 线段树合并】

    CF 666E Forensic Examination 题意: 给出一个串\(s\)和\(n\)个串\(t_i\),\(q\)次询问,每次询问串\(s\)的子串\(s[p_l:p_r]\)在串\(t ...

  7. 【codeforces666E】Forensic Examination 广义后缀自动机+树上倍增+线段树合并

    题目描述 给出 $S$ 串和 $m$ 个 $T_i$ 串,$q$ 次询问,每次询问给出 $l$ .$r$ .$x$ .$y$ ,求 $S_{x...y}$ 在 $T_l,T_{l+1},...,T_r ...

  8. Codeforces 666E Forensic Examination

    题意:给定主串s和m个模式串,每次询问[l,r]的模式串中出现s[pl...pr]次数最多的串和次数. 这题挺简单的,先把所有模式串拿来建广义后缀自动机,询问相当于子树众数,用线段树合并即可. 那我为 ...

  9. [CF 666E] Forensic Examination

    Description 传送门 Solution 对 \(T[1..m]\) 建立广义后缀自动机,离线,找出代表 \(S[pl,pr]\) 的每个节点,线段树合并. Code #include < ...

随机推荐

  1. C# 发送电子邮件源码片段

    下面代码内容是关于C# 发送电子邮件片段的代码,应该对各位有所用途. using System;using System.Web;using System.Web.Mail;public class ...

  2. SourceTree下载bitbucket代码

    SourceTree安装方法 下载地址:https://www.sourcetreeapp.com/ 列几个安装过程中的注意点: 根URL(Root URL):https://bitbucket.or ...

  3. python打印电脑串口的信息

    # -*- coding:utf-8 -*- from serial.tools.list_ports import comports port_list = list(comports()) if ...

  4. Django REST framework基础:分页

    DRF分页组件 为什么要使用分页 我们数据表中可能会有成千上万条数据,当我们访问某张表的所有数据时,我们不太可能需要一次把所有的数据都展示出来,因为数据量很大,对服务端的内存压力比较大还有就是网络传输 ...

  5. DRF项目创建流程(1)

    一 web应用模式 前后端不分离 前后端分离 二 RESTFUL API规范 REST全称是Representational State Transfer,中文意思是表述(编者注:通常译为表征)性状态 ...

  6. gradle构建项目失败:Unzipping /home/.gradle/wrapper/dists/gradle-3.3-all/55gk2rcmfc6p2dg9u9ohc3hw9/gradle-3.3-all.zip to /home/.gradle/wrapper/dists/gradle-3.3-all/55gk2rcmfc6p2dg9u9ohc3hw9

    Unzipping /home/.gradle/wrapper/dists/gradle-3.3-all/55gk2rcmfc6p2dg9u9ohc3hw9/gradle-3.3-all.zip to ...

  7. 基于SVM的鸢尾花数据集分类实现[使用Matlab]

    iris数据集的中文名是安德森鸢尾花卉数据集,英文全称是Anderson’s Iris data set.iris包含150个样本,对应数据集的每行数据.每行数据包含每个样本的四个特征和样本的类别信息 ...

  8. Example of DenseCRF with non-RGB data

    本笔记本通过一个示例说明如何在非rgb数据上使用DenseCRFs.同时,它将解释基本概念并通过一个示例进行演示,因此即使您正在处理RGB数据,它也可能是有用的,不过也请查看PyDenseCRF's ...

  9. fliplr函数

    fliplr  左右翻转矩阵 语法: B = fliplr(A) 将矩阵A的列绕垂直轴进行左右翻转 matabc 如果A是一个行向量,fliplr(A)将A中元素的顺序进行翻转. 如果A是一个列向量, ...

  10. Python脱产8期 Day13 2019/4/28

    一 函数的嵌套定义 1在一个函数的内部定义另一个函数. 2.为什么有函数的嵌套定义: # 1)函数fn2想直接使用fn1函数的局部变量,可以讲fn2直接定义到fn1的内部,这样fn2就可以直接访问fn ...