题解 P4094 [HEOI2016/TJOI2016]字符串
P4094 [HEOI2016/TJOI2016]字符串 题面传送门
题意简述:给出字符串
s ,多次询问a,b,c,d 求s[a:b] 的所有子串与s[c:d] 的最长公共前缀的最大值。
我的 SAM 学习笔记 例题 X.
首先,SAM 不太方便处理前缀,所以将整个串翻转(询问不要忘记翻转),这样就转化为了最长公共后缀。接下来求
直接不管
根据 SAM 的性质,
-
先说说为什么要
len(q) 最小:假设存在q' 满足上述条件,但len(q')>len(q) ,即q 是q' 的祖先(同时q' 是p 的祖先)。记\max_{x\in endpos(q),x\leq b}x 为maxp(q,b) ,那么根据endpos 和link 的性质,即endpos(q')\subsetneq endpos(q) ,因此,maxp(q',b)\leq maxp(q,b) ,即q' 点所表示字符串在b 或b 之前出现的最大结束位置,一定不大于q 点所表示的字符串在b 或b 之前出现的最大结束位置。因此maxp(q',b)-a+1\leq maxp(q,b)-a+1 。又因为len(q)\ (len(q'))\geq maxp(q,b)\ (maxp(q',b)) -a+1 ,即q 和q' 所表示的的最长字符串超出了a 的限制,所以我们是用maxp 值-a+1 求出在a 的限制下该状态对答案的贡献。故q 一定比q' 更优。 -
再说说为什么要算上
link(q) : 一 目 了 然,不 言 而 喻。 -
同时,因为
link(q) 的贡献已经是len(q) 了,如果再往上跳maxp 递增,贡献也一定是该点的len 值,这是递减的,所以不需要再往上考虑。
说完了思路,接下来讲讲怎么实现:用线段树合并维护
时间复杂度
/*
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;
}