题解 P4094 [HEOI2016/TJOI2016]字符串

· · 题解

P4094 [HEOI2016/TJOI2016]字符串 题面传送门

题意简述:给出字符串 s,多次询问 a,b,c,d 求 s[a:b] 的所有子串与 s[c:d] 的最长公共前缀的最大值。

我的 SAM 学习笔记 例题 X.

首先,SAM 不太方便处理前缀,所以将整个串翻转(询问不要忘记翻转),这样就转化为了最长公共后缀。接下来求 s[1:d] 所代表的状态,设为 p,直接在建 SAM 时预处理即可。

直接不管 c 的限制,问题转化为求出 s[a:b] 所有子串与 s[1:d] 的最长公共后缀长度,并与 d-c+1 取 \min。

根据 SAM 的性质,link 树上所有 p 的祖先都表示 s[1:d] 的一个或多个后缀。我们可以找到一个状态 q 满足 q 是 p 的祖先且 \left(\max_{x\in endpos(q),x\leq b}x\right)-a+1\leq len(q)(也就是该状态所表示的字符串在 b 或 b 之前出现的最靠右的结束位置,至于为什么要最靠右显而易见(右边的出现位置肯定优于左边的出现位置,因为有左端点 a 的限制),读者可自行理解),且 len(q) 的值最小,那么最长公共后缀肯定在 q 或 link(q) 所表示的子串中。

说完了思路,接下来讲讲怎么实现:用线段树合并维护 endpos 集合可以轻松在 \log 时间内求出 maxp。同时,因为满足条件的 q 满足二分条件,所以求 q 直接用 p 在 link 树上倍增即可。那么最后答案即为 \min(\max(maxp(q,b)-a+1,len(link(q))),d-c+1)。(不需要特判答案为 0 的情况,因为此时 maxp(q,b)-a+1 不小于 0,而 len(link(q)) 显然为 0)

时间复杂度 \mathcal{O}(q\log^2 n)。

/*
    Powered by C++11.
    Author : Alex_Wei.
*/

#include <bits/stdc++.h>
using namespace std;

//#pragma GCC optimize(3)

//using int = long long
//using i128 = __int128;

using uint = unsigned int;
using ll = long long;
using ull = unsigned long long;
using db = double;
using ld = long double;
using pii = pair <int,int>;
using pll = pair <ll,ll>;
using pdd = pair <double,double>;
using vint = vector <int>;
using vpii = vector <pii>;

#define fi first
#define se second
#define pb emplace_back
#define mpi make_pair
#define all(x) x.begin(),x.end()
#define sor(x) sort(all(x))
#define rev(x) reverse(all(x))
#define mem(x,v) memset(x,v,sizeof(x))
#define mcpy(x,y) memcpy(x,y,sizeof(y))
#define Time 1.0*clock()/CLOCKS_PER_SEC

pii operator + (pii a,pii b){return {a.fi+b.fi,a.se+b.se};}
pll operator + (pll a,pll b){return {a.fi+b.fi,a.se+b.se};}

namespace IO{
    char buf[1<<23],*p1=buf,*p2=buf,obuf[1<<24],*O=obuf;
    #ifdef __WIN32
        #define gc getchar()
    #else
        #define gc (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<22,stdin),p1==p2)?EOF:*p1++)
    #endif
    #define pc(x) (*O++=x)
    #define flush() fwrite(obuf,O-obuf,1,stdout)
    inline ll read(){
        ll x=0; bool sign=0; char s=gc;
        while(!isdigit(s))sign|=s=='-',s=gc;
        while(isdigit(s))x=(x<<1)+(x<<3)+(s-'0'),s=gc;
        return sign?-x:x;
    }
    inline void print(ll x){
        if(x<0)pc('-'),print(-x);
        else{
            if(x>9)print(x/10);
            pc(x%10+'0');
        }
    }
} using namespace IO;

const int N=2e5+5;
const int S=26;

int node,rt[N],ls[N<<5],rs[N<<5],val[N<<5];
void push(int x){
    val[x]=max(val[ls[x]],val[rs[x]]);
} void ins(int l,int r,int p,int &x){
    x=++node;
    if(l==r)return val[x]=p,void();
    int m=l+r>>1;
    if(p<=m)ins(l,m,p,ls[x]);
    else ins(m+1,r,p,rs[x]);
    push(x);
} int merge(int l,int r,int x,int y){
    if(!x||!y)return x|y;
    int z=++node,m=l+r>>1;
    if(l==r)return val[z]=max(val[x],val[y]),z;
    ls[z]=merge(l,m,ls[x],ls[y]),rs[z]=merge(m+1,r,rs[x],rs[y]);
    return push(z),z;
} int query(int l,int r,int ql,int qr,int x){
    if(!x)return 0;
    if(ql<=l&&r<=qr)return val[x];
    int m=l+r>>1,ans=0;
    if(ql<=m)ans=query(l,m,ql,qr,ls[x]);
    if(m<qr)ans=max(ans,query(m+1,r,ql,qr,rs[x]));
    return ans;
}

// Suffix_Automaton
int a,b,c,d;
int n,m,K,cnt,las;
int fa[N],len[N],son[N][S];
int buc[N],id[N],f[N][S],ed[N];
vector <int> e[N];
void ins(int it){
    int p=las,cur=++cnt;
    len[cur]=len[las]+1,las=cur;
    ins(1,n,len[cur],rt[cur]),ed[len[cur]]=cur;
    while(p&&!son[p][it])son[p][it]=cur,p=fa[p];
    if(!p)return fa[cur]=1,void();
    int q=son[p][it];
    if(len[p]+1==len[q])return fa[cur]=q,void();
    int cl=++cnt;
    fa[cl]=fa[q],fa[q]=fa[cur]=cl,len[cl]=len[p]+1;
    mcpy(son[cl],son[q]);
    while(p&&son[p][it]==q)son[p][it]=cl,p=fa[p];
} void build(char *s){
    las=cnt=1,K=log2(n);
    for(int i=1;i<=n;i++)ins(s[i]-'a');
    for(int i=1;i<=cnt;i++)buc[len[i]]++;
    for(int i=1;i<=n;i++)buc[i]+=buc[i-1];
    for(int i=cnt;i;i--)id[buc[len[i]]--]=i;
    for(int i=cnt;i>1;i--)rt[fa[id[i]]]=merge(1,n,rt[fa[id[i]]],rt[id[i]]);
    for(int j=0;j<=K;j++)for(int i=1;i<=cnt;i++)f[i][j]=j?f[f[i][j-1]][j-1]:fa[i];
} int qpos(int pos){
    return query(1,n,1,b,rt[pos]);
}

char s[N];
int main(){
    cin>>n>>m,scanf("%s",s+1);
    reverse(s+1,s+n+1),build(s);
    while(m--){
        cin>>a>>b>>c>>d;
        a=n-a+1,b=n-b+1,c=n-c+1,d=n-d+1,swap(a,b),swap(c,d);
        int p=ed[d];
        for(int i=K;~i;i--)if(f[p][i]){
            int pp=f[p][i],pos=qpos(pp);
            if(len[pp]>=pos-a+1)p=pp;
        } int pos=qpos(p);
        cout<<min(d-c+1,max(pos-a+1,len[f[p][0]]))<<endl;
    }
    return 0;
}