Description
Given a string \(s(|s|\leq10^5)\) and \(m\) queries, each query for all substrings of the substring \(s[x_1..x_2]\) and \ (s[y_1..y_2]\) The maximum value of the longest common prefix length.
Solution
Suffix array + binary answer + chair tree.
It is easy to know that all substrings of \(s[x_1..x_2]\) can be regarded as all suffixes of \(s[x_1..x_2]\) . Then you can get:
\[ ans=max\{ min(lcp(i,y_1),x_2-i+1,y_2-y_1+1)\} \quad (i\in [x_1,x_2])\] \( lcp(i,j)\) represents the longest common prefix length of the suffix \(i\) and the suffix \(j\) . Using a suffix array can be done in \(O(nm)\) .
Consider the binary answer \(ans\in [0,min(x_2-i+1,y_2-y_1+1)]\) , the binary result is \(len\) . Since \(lcp(i, y_1)\) is equivalent to the minimum value of an interval, then the farther away \( rnk[i]\) is from \(rnk[y_1]\), the smaller. Then we can divide again to find an interval \([fr,to]\) , \(\forall i\in [fr,to]\) , there is \(lcp(sa[i],y_1)\geq len\ ) . then check if there is\(sa[i]\in [x_1,x_2-len+1]\) , if there is, it means that \(len\) is legal, expand \(len\) ; otherwise, it is illegal, reduce \(len\) .
So what we have to do is to find whether there is a certain number in a certain range in a certain interval of \(sa\) . Build a \(n\) segment tree for \(sa\) , the \(j\) bit of the \(i\) tree is \(1\), which means that it is in \(sa[1..i]\) There is \(j\) in it . When querying, you can query the difference between two line segment trees.
Time complexity \(O((n+m)logn)\) .
Code
//[TJOI2016]字符串
#include <cstdio>
#include <cstring>
inline int read()
{
int x=0; char ch=getchar();
while(ch<'0'||'9'<ch) ch=getchar();
while('0'<=ch&&ch<='9') x=x*10+ch-'0',ch=getchar();
return x;
}
inline int min(int x,int y) {return x<y?x:y;}
inline int swap(int &x,int &y) {int t=x; x=y,y=t;}
const int N=1e5+10;
int n,m; char s[N];
int sa[N],rnk[N<<1],h[N];
int cnt[N],tmp[N],rnk1[N];
int lg2[N],st[N][18];
void getSA()
{
memset(cnt,0,sizeof cnt);
for(int i=1;i<=n;i++) cnt[s[i]]=1;
for(int i=1;i<=256;i++) cnt[i]+=cnt[i-1];
for(int i=1;i<=n;i++) rnk[i]=cnt[s[i]];
for(int L=1,k=0;k<n;L<<=1)
{
memset(cnt,0,sizeof cnt);
for(int i=1;i<=n;i++) cnt[rnk[i+L]]++;
for(int i=1;i<=n;i++) cnt[i]+=cnt[i-1];
for(int i=n;i>=1;i--) tmp[cnt[rnk[i+L]]--]=i;
memset(cnt,0,sizeof cnt);
for(int i=1;i<=n;i++) cnt[rnk[tmp[i]]]++;
for(int i=1;i<=n;i++) cnt[i]+=cnt[i-1];
for(int i=n;i>=1;i--) sa[cnt[rnk[tmp[i]]]--]=tmp[i];
k=0;
for(int i=1;i<=n;i++)
{
if(rnk[sa[i]]!=rnk[sa[i-1]]||rnk[sa[i]+L]!=rnk[sa[i-1]+L]) k++;
rnk1[sa[i]]=k;
}
memcpy(rnk,rnk1,sizeof rnk1);
}
for(int i=1,k=0;i<=n;i++)
{
if(rnk[i]==1) {h[1]=k=0; continue;}
if(k) k--;
while(s[i+k]==s[sa[rnk[i]-1]+k]) k++;
h[rnk[i]]=k;
}
lg2[1]=0; for(int i=2;i<=n;i++) lg2[i]=lg2[i>>1]+1;
for(int i=1;i<=n;i++) st[i][0]=h[i];
for(int k=1;(1<<k)<=n;k++)
for(int i=1;i+(1<<k)-1<=n;i++)
st[i][k]=min(st[i][k-1],st[i+(1<<k-1)][k-1]);
}
int lcp(int x,int y)
{
if(x==y) return n-x+1;
int i=rnk[x],j=rnk[y]; if(i>j) swap(i,j);
i++;
int t=lg2[j-i+1];
return min(st[i][t],st[j-(1<<t)+1][t]);
}
int ndCnt,rt[N];
struct node{int chL,chR; int cnt;} nd[N*18];
void update(int p) {nd[p].cnt=nd[nd[p].chL].cnt+nd[nd[p].chR].cnt;}
void ins(int &p,int L0,int R0,int x)
{
nd[++ndCnt]=nd[p]; p=ndCnt;
if(L0==R0) {nd[p].cnt++; return;}
int mid=L0+R0>>1;
if(x<=mid) ins(nd[p].chL,L0,mid,x);
else ins(nd[p].chR,mid+1,R0,x);
update(p);
}
int optL,optR;
int qres;
void query(int p1,int p2,int L0,int R0)
{
if(p1==p2) return;
if(optL<=L0&&R0<=optR) {qres+=nd[p2].cnt-nd[p1].cnt; return;}
int mid=L0+R0>>1;
if(optL<=mid) query(nd[p1].chL,nd[p2].chL,L0,mid);
if(mid<optR) query(nd[p1].chR,nd[p2].chR,mid+1,R0);
}
int x1,x2,y1,y2;
bool check(int len)
{
int fr=1,fr1=rnk[y1];
while(fr<=fr1)
{
int mid=fr+fr1>>1;
if(lcp(sa[mid],y1)<len) fr=mid+1;
else fr1=mid-1;
}
int to=rnk[y1],to1=n;
while(to<=to1)
{
int mid=to+to1>>1;
if(lcp(y1,sa[mid])<len) to1=mid-1;
else to=mid+1;
}
to--;
optL=x1,optR=x2-len+1,qres=0; query(rt[fr-1],rt[to],1,n);
return qres>0;
}
int main()
{
n=read(),m=read(); scanf("%s",s+1);
getSA();
for(int i=1;i<=n;i++) ins(rt[i]=rt[i-1],1,n,sa[i]);
for(int i=1;i<=m;i++)
{
x1=read(),x2=read(),y1=read(),y2=read();
int L=1,R=min(x2-x1+1,y2-y1+1);
while(L<=R)
{
int mid=L+R>>1;
if(check(mid)) L=mid+1;
else R=mid-1;
}
printf("%d\n",R);
}
return 0;
}