BZOJ3796 : Mushroom追妹紙

Claris發表於2014-12-12

將S1與S2用#號拼接在一起形成S串

將S3與S串跑KMP求出S3在S串中每次出現的位置l[i]

對於S串每個字尾i,求出f[i]表示該串不包含S3串的最長字首

然後求出S串的字尾陣列

先從小到大掃描字尾陣列,

同時維護一個tmp表示S2中的串與現在的串的最長公共字首,且沒有出現S3,

如果碰到一個S1的字尾,那麼更新ans=max(ans,min(f[sa[i]],tmp))

如果碰到一個S2的字尾,那麼更新tmp=max(tmp,f[sa[i]])

然後將tmp與height取一個最小值

最後再從後往前掃描一次即可

時間複雜度$O(n)$

 

#include<cstdio>
#include<cstring>
using std::strlen;
inline bool leq(int a1,int a2,int b1,int b2){return a1<b1||a1==b1&&a2<=b2;}
inline bool leq(int a1,int a2,int a3,int b1,int b2,int b3){return a1<b1||a1==b1&&leq(a2,a3,b2,b3);}
inline void radixPass(int*a,int*b,int*r,int n,int K){
  int*c=new int[K+1];
  int i,sum,t;
  for(i=0;i<=K;i++)c[i]=0;
  for(i=0;i<n;i++)c[r[a[i]]]++;
  for(i=sum=0;i<=K;i++)t=c[i],c[i]=sum,sum+=t;
  for(i=0;i<n;i++)b[c[r[a[i]]]++]=a[i];
  delete[]c;
}
void suffixArray(int*T,int*SA,int n,int K){
  int n0=(n+2)/3,n1=(n+1)/3,n2=n/3,n02=n0+n2;
  int*R=new int[n02+3];R[n02]=R[n02+1]=R[n02+2]=0;
  int*SA12=new int[n02+3];SA12[n02]=SA12[n02+1]=SA12[n02+2]=0;
  int*R0=new int[n0];
  int*SA0=new int[n0];
  int i,j,name=0,c0=-1,c1=-1,c2=-1,p=0,t=n0-n1,k=0;
  for(i=j=0;i<n+n0-n1;i++)if(i%3)R[j++]=i;
  radixPass(R,SA12,T+2,n02,K),radixPass(SA12,R,T+1,n02,K),radixPass(R,SA12,T,n02,K);
  for(i=0;i<n02;i++){
    if(T[SA12[i]]!=c0||T[SA12[i]+1]!=c1||T[SA12[i]+2]!=c2)name++,c0=T[SA12[i]],c1=T[SA12[i]+1],c2=T[SA12[i]+2];
    if(SA12[i]%3==1)R[SA12[i]/3]=name;else R[SA12[i]/3+n0]=name;
  }
  if(name<n02)for(suffixArray(R,SA12,n02,name),i=0;i<n02;i++)R[SA12[i]]=i+1;else for(i=0;i<n02;i++)SA12[R[i]-1]=i;
  for(i=j=0;i<n02;i++)if(SA12[i]<n0)R0[j++]=3*SA12[i];
  for(radixPass(R0,SA0,T,n0,K);k<n;k++){
    #define GetI() (SA12[t]<n0?SA12[t]*3+1:(SA12[t]-n0)*3+2)
    i=GetI(),j=SA0[p];
    if(SA12[t]<n0?leq(T[i],R[SA12[t]+n0],T[j],R[j/3]):leq(T[i],T[i+1],R[SA12[t]-n0+1],T[j],T[j+1],R[j/3+n0])){
      SA[k]=i;
      if(++t==n02)for(k++;p<n0;p++,k++)SA[k]=SA0[p];
    }else{
      SA[k]=j;
      if(++p==n0)for(k++;t<n02;t++,k++)SA[k]=GetI();
    }
  }
  delete[]R;delete[]SA12;delete[]SA0;delete[]R0;
}
#define N 100010
char s1[N],s2[N],s3[N],s[N],ch;
int l1,l2,l3,n,nxt[N],S[N],SA[N],rank[N],height[N],f[N],i,j,k,cnt,loc[N],ans,tmp;
inline int min(int a,int b){return a<b?a:b;}
inline void Min(int&a,int b){if(a>b)a=b;}
inline void Max(int&a,int b){if(a<b)a=b;}
int main(){
  scanf("%s%s%s",s1,s2,s3);
  l1=strlen(s1),l2=strlen(s2),l3=strlen(s3);
  for(i=0;i<l1;i++)s[i]=s1[i];
  for(s[n=l1]='#',i=0;i<l2;i++)s[++n]=s2[i];++n;
  for(nxt[0]=j=-1,i=1;i<l3;nxt[i++]=j){
    while(~j&&s3[j+1]!=s3[i])j=nxt[j];
    if(s3[j+1]==s3[i])j++;
  }
  for(j=-1,i=0;i<n;i++){
    while(~j&&s3[j+1]!=s[i])j=nxt[j];
    if(s3[j+1]==s[i])j++;
    if(j==l3-1)loc[++cnt]=i-l3+1,j=nxt[j];
  }
  for(i=0,j=1;i<n;i++){
    while(j<=cnt&&loc[j]<i)j++;
    if(j>cnt)f[i]=n-i;else f[i]=loc[j]+l3-i-1;
  }
  for(i=0;i<=l1;i++)Min(f[i],l1-i);
  for(i=0;i<n;i++)if(s[i]=='#')S[i]=27;else S[i]=s[i]-'a'+1;
  suffixArray(S,SA,n,27);
  for(i=0;i<n;i++)rank[SA[i]]=i;
  for(k=i=0;i<n;i++)if(rank[i]==n-1)k=0;
  else{
    if(k)k--;
    for(j=SA[rank[i]+1];S[i+k]==S[j+k];k++);
    height[rank[i]]=k;
  }
  for(i=tmp=0;i<n;i++){
    if(SA[i]<l1)Max(ans,min(tmp,f[SA[i]]));
    if(SA[i]>l1)Max(tmp,f[SA[i]]);
    Min(tmp,height[i]);
  }
  for(i=n-1,tmp=0;~i;i--){
    if(SA[i]<l1)Max(ans,min(tmp,f[SA[i]]));
    if(SA[i]>l1)Max(tmp,f[SA[i]]);
    if(i)Min(tmp,height[i-1]);
  }
  return printf("%d",ans),0;
}

  

 

相關文章