http://codeforces.com/problemset/problem/666/E (题目链接)

题意

  给出一个主串$S$,$n$个匹配串编号从$1$到$n$。$m$组询问,每次询问主串的一个子串$S[p_l,p_r]$在编号为$[l,r]$的匹配串的哪一个中出现次数最多。

Solution

  首先我们把匹配串相连,中间用分隔符隔开,构建后缀自动机,并且给自动机主链上的节点打上标记,避免之后找后缀的时候重复计算。

  每个节点与其对应$parent$相连,构建$parent$树。由于$parent$树的性质,那么问题就转化为了对于每一个询问求解在$parent$树上对应$S[p_l,p_r]$的节点子树中,编号为$[l,r]$的节点的众数。那么我们现在就要解决两个问题:第一,如何知道对于每一个询问$S[p_l,p_r]$,它在自动机上匹配以后到达的节点位置;第二,如何求解子树中编号为$[l,r]$的节点的众数。

  第一个问题,我们将整个主串$S$在自动机上匹配,那么我们可以得到一个$pos$数组,$pos[i]$表示主串$S$匹配到第$i$位时在自动机上的节点。于是我们就知道了对应子串$S[1,1],S[1,2],S[1,3],S[1,4]······S[1,|S|]$这些子串在自动机上的对应位置。那么对于每一个$S[p_l,p_r]$,我们根据右端点找到$pos[p_r]$,因为长度变短,所以它的实际位置可能是$pos[p_r]$的$parent$树上的祖先,所以我们倍增跳$parent$就可以了。

  第二个问题,考虑线段树合并。按照一定的顺序线段树合并,确保随着每个节点的处理完成,位置在这个节点的询问也会跟随着一起处理掉。我们求出$parent$树的$dfs$序,将询问按照其位置的$dfs$序升序排列,然后在树上一边走一边线段树合并,在树上走的时候按照$dfs$序从大往小的顺序走。同时按照从后往前的顺序处理询问即可。

细节

  这里线段树的询问返回值用的是pair,感觉应该更好处理一些。出现次数为0记得要特判一下。

代码

// codeforces 666E
#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<vector>
#include<cmath>
#define LL long long
#define inf (1ll<<30)
#define Pi acos(-1.0)
#define free(a) freopen(a".in","r",stdin),freopen(a".out","w",stdout)
using namespace std; const int maxn=500010;
int n,m,Q,tot,cnt,rt[maxn],dfn[maxn],fa[maxn][30],id[maxn],Len[maxn],r[maxn];
char s[maxn],st[maxn],ss[maxn];
vector<int> v[maxn];
struct data {int l,r,sl,sr,n,id;}q[maxn],t[maxn];
struct Pair {
int x,y;
friend bool operator < (Pair a,Pair b) {return a.x==b.x ? a.y>b.y : a.x<b.x;}
}ans[maxn];
struct node {
int son[2];Pair sum;
int& operator [] (int x) {return son[x];}
}tr[maxn*40]; namespace SAM {
int last;
int par[maxn<<1],ch[maxn<<1][27],len[maxn<<1],pos[maxn];
void Extend(int c,int x) {
int np=++m,p=last;last=np;
len[np]=len[p]+1;id[np]=x;r[np]=1;
for (;p && !ch[p][c];p=par[p]) ch[p][c]=np;
if (!p) par[np]=1;
else {
int q=ch[p][c];
if (len[q]==len[p]+1) par[np]=q;
else {
int nq=++m;len[nq]=len[p]+1;id[nq]=id[q];
memcpy(ch[nq],ch[q],sizeof(ch[q]));
par[nq]=par[q];
par[np]=par[q]=nq;
for (;p && ch[p][c]==q;p=par[p]) ch[p][c]=nq;
}
}
}
void build(char *r) {
int len=strlen(r+1),id=1;
last=m=1;
for (int i=1;i<=len;i++) {
Extend(r[i]-'a',id);
if (r[i]==123) id++;
}
for (int i=2;i<=m;i++) v[par[i]].push_back(i);
}
void match(char *r) {
int p=1,e=strlen(r+1),l=0;
for (int i=1;i<=e;i++) {
while (p>1 && !ch[p][r[i]-'a']) p=par[p],l=len[p];
if (ch[p][r[i]-'a']) pos[i]=p=ch[p][r[i]-'a'],Len[i]=++l;
}
}
void position() {
for (int i=1;i<=Q;i++) {
int p=pos[q[i].sr];
int tmp=q[i].sr-q[i].sl+1;
if (Len[q[i].sr]<tmp) continue;
t[++tot]=q[i];
for (int j=20;j>=0;j--) if (len[fa[p][j]]>=tmp) p=fa[p][j];
t[tot].n=p;
}
}
}
using namespace SAM; namespace Segtree {
int sz;
void insert(int &k,int l,int r,int x) {
if (!k) k=++sz;
if (l==r) {tr[k].sum=(Pair){1,l};return;}
int mid=(l+r)>>1;
if (x<=mid) insert(tr[k][0],l,mid,x);
else insert(tr[k][1],mid+1,r,x);
tr[k].sum=max(tr[tr[k][0]].sum,tr[tr[k][1]].sum);
}
int merge(int x,int y,int l,int r) {
if (!x || !y) return x|y;
int mid=(l+r)>>1;
if (l==r) {tr[x].sum.x+=tr[y].sum.x;return x;}
tr[x][0]=merge(tr[x][0],tr[y][0],l,mid);
tr[x][1]=merge(tr[x][1],tr[y][1],mid+1,r);
tr[x].sum=max(tr[tr[x][0]].sum,tr[tr[x][1]].sum);
return x;
}
Pair query(int k,int s,int t,int l,int r) {
if (s==l && r==t) return tr[k].sum;
int mid=(l+r)>>1;
if (t<=mid) return query(tr[k][0],s,t,l,mid);
else if (s>mid) return query(tr[k][1],s,t,mid+1,r);
else return max(query(tr[k][0],s,mid,l,mid),query(tr[k][1],mid+1,t,mid+1,r));
}
}
using namespace Segtree; namespace Basic {
bool cmp(data a,data b) {return dfn[a.n]<dfn[b.n];}
void dfs(int x) {
dfn[x]=++cnt;
for (int i=1;i<=20;i++) fa[x][i]=fa[fa[x][i-1]][i-1];
int len=v[x].size();
for (int i=0;i<len;i++) {
fa[v[x][i]][0]=x;
dfs(v[x][i]);
}
}
void work(int x) {
int len=v[x].size();
reverse(v[x].begin(),v[x].end());
for (int i=0;i<len;i++) work(v[x][i]);
if (r[x]) insert(rt[x],1,m,id[x]);
for (int i=0;i<len;i++) rt[x]=merge(rt[x],rt[v[x][i]],1,m);
for (;tot && t[tot].n==x;tot--)
ans[t[tot].id]=query(rt[x],t[tot].l,t[tot].r,1,m);
}
}
using namespace Basic; int main() {
scanf("%s",s+1);
scanf("%d",&n);
int len=0;
for (int i=1;i<=n;i++) {
scanf("%s",ss+1);
int l=strlen(ss+1);
for (int j=1;j<=l;j++) st[++len]=ss[j];
st[++len]=123;
}
build(st);
match(s);
scanf("%d",&Q);
for (int i=1;i<=Q;i++) scanf("%d%d%d%d",&q[i].l,&q[i].r,&q[i].sl,&q[i].sr),q[i].id=i;
dfs(1);
position();
sort(t+1,t+1+tot,cmp);
work(1);
for (int i=1;i<=Q;i++) if (!ans[q[i].id].x) ans[q[i].id].y=q[i].l;
for (int i=1;i<=Q;i++) printf("%d %d\n",ans[i].y,ans[i].x);
return 0;
}

【codeforces 666E】 Forensic Examination的更多相关文章

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

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

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

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

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

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

  4. 【codeforces 415D】Mashmokh and ACM(普通dp)

    [codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...

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

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

  6. 【codeforces 707E】Garlands

    [题目链接]:http://codeforces.com/contest/707/problem/E [题意] 给你一个n*m的方阵; 里面有k个联通块; 这k个联通块,每个连通块里面都是灯; 给你q ...

  7. 【codeforces 707C】Pythagorean Triples

    [题目链接]:http://codeforces.com/contest/707/problem/C [题意] 给你一个数字n; 问你这个数字是不是某个三角形的一条边; 如果是让你输出另外两条边的大小 ...

  8. 【codeforces 709D】Recover the String

    [题目链接]:http://codeforces.com/problemset/problem/709/D [题意] 给你一个序列; 给出01子列和10子列和00子列以及11子列的个数; 然后让你输出 ...

  9. 【codeforces 709B】Checkpoints

    [题目链接]:http://codeforces.com/contest/709/problem/B [题意] 让你从起点开始走过n-1个点(至少n-1个) 问你最少走多远; [题解] 肯定不多走啊; ...

随机推荐

  1. hung task机制

    最近在修改内核源码的时候一直出现格式化磁盘的时候,进程会出现状态D,看内核日志会看到如下信息: INFO: task filebench: blocked seconds. Oct :: localh ...

  2. ASP.NET Core 3.0 实战:构建多版本 API 接口

    第一次在博客写分享,请多多捧场,如有歧义请多多包含! 因为业务需求发展需要,所以API接口的变更升级是必不可少的事情,而原有的接口是不可能马上停止使用的.例如:Login接口为例,1.0版本之返回用户 ...

  3. MIPI Alliance (MIPI联盟)

    一.介绍 1.MIPI联盟,即移动产业处理器接口(Mobile Industry Processor Interface 简称MIPI)联盟.MIPI(移动产业处理器接口)是MIPI联盟发起的为移动应 ...

  4. Struts2将图片输出到页面

            在做CRUD的过程中,添加页面是个表单,表单里面有一项是上传头像文件.这样表单提交后,头像文件上传了. 但这个文件存的地址是本地硬盘的一个文件夹.在编辑页面要做这个头像的回显的话,就需 ...

  5. Android开发——高斯模糊效果的简单实现

    0. 前言 在Android开发中,经常在音乐软件中看到高斯模糊效果. 在找遍了所有高斯模糊的算法代码后,发现stackblur的Java实现是最快的.效果如下所示. 1.  高斯模糊效果实现 Bit ...

  6. 汇编 ADD指令

    知识点: 加法汇编指令ADD 一.加法指令 ADD(Addition) 格式 格式: ADD A,B //A=A+B; 功能: 两数相加 . OPRD1为任一通用寄存器或存储器操作数,可以是任意一个 ...

  7. Eclipse中Svn插件配置

    1. Svn插件配置教程 http://www.cnblogs.com/ruiati/p/3584120.html 2. Svn插件使用教程 http://wenku.baidu.com/link?u ...

  8. 洛咕 P3700 [CQOI2017]小Q的表格

    洛咕 P3700 [CQOI2017]小Q的表格 神仙题orz 首先推一下给的两个式子中的第二个 \(b\cdot F(a,a+b)=(a+b)\cdot F(a,b)\) 先简单的想,\(F(a,a ...

  9. 几个不常用的 Web API

    1. 设备震动 vibrate Navigator.vibrate() 方法使设备(有震动硬件)产生有频率的震动.若设备不支持震动,该方法将无效.若某震动方式已经在进行中(当该方法调用时),则前一个震 ...

  10. Java 中的 try catch 影响性能吗?

    前几天在 code review 时发现有一段代码中存在滥用try catch的现象.其实这种行为我们也许都经历过,刚参加工作想尽量避免出现崩溃问题,因此在很多地方都想着 try catch一下. 但 ...