后缀字典序中二分找查询串区间
bool cmp1(int pos,char *p) { pos=sa[pos]; int len1=n-pos+1,len2=now,l=min(len1,len2); int flag=strncmp(a+pos,p,l); return flag>0||(flag==0&&len1>=len2); } bool cmp2(int pos,char *p) { pos=sa[pos]; int len1=n-pos+1,len2=now,l=min(len1,len2); int flag=strncmp(a+pos,p,l); return flag<=0; }
bool ok(int pos,char *p)
{
pos=sa[pos];
int len=n-pos+1;
return len>=now&&strncmp(a+pos,p,now)==0;
} //main函数 int ql=1,qr=n,ans1=n+1,ans2=0; while(ql<=qr) { int mid=ql+qr>>1; if(cmp1(mid,b))ans1=mid,qr=mid-1; else ql=mid+1; } ql=1,qr=n; while(ql<=qr) { int mid=ql+qr>>1; if(cmp2(mid,b))ans2=mid,ql=mid+1; else qr=mid-1; } if(ans1<=ans2&&ok(ans1,b)&&ok(ans2,b)) { qu[l-1].pb(node{ans1,ans2,-i}); qu[r-now+1].pb(node{ans1,ans2,i}); }