View Code
#include <iostream> #include <cstdio> #include <queue> #include <cstring> #include <cmath> #include <algorithm> using namespace std; const int MM = 111111; //处理字符串的最大长度 #define debug puts("wrong") int Wa[MM], Wb[MM], Wv[MM], Ws[MM]; int ran[MM], height[MM]; int minh[1000][20]; int min(int x,int y) {return x>y?y:x;} int max(int x,int y) {return x>y?x:y;} void calheight(char*r,int*sa,int n) { int i,j,k=0; for(i=1;i<=n;i++) printf("%d ",sa[i]); printf("\n"); for(i=1;i<=n;i++) ran[sa[i]]=i; debug; for(i=0;i<n;height[ran[i++]]=k) for(k?k--:0,j=sa[ran[i]-1];r[i+k]==r[j+k];k++); return; } int cmp(int*r,int a,int b,int l) { return r[a]==r[b]&&r[a+l]==r[b+l]; } void da(char*r,int*sa,int n,int m) { int i,j,p,*x=Wa,*y=Wb,*t; for(i=0;i<m;i++) Ws[i]=0; for(i=0;i<n;i++) Ws[x[i]=r[i]]++; for(i=1;i<m;i++) Ws[i]+=Ws[i-1]; for(i=n-1;i>=0;i--) sa[--Ws[x[i]]]=i; for(j=1,p=1;p<n;j<<=1,m=p) { for(p=0,i=n-j;i<n;i++) y[p++]=i; for(i=0;i<n;i++) if(sa[i]>=j) y[p++]=sa[i]-j; for(i=0;i<n;i++) Wv[i]=x[y[i]]; for(i=0;i<m;i++) Ws[i]=0; for(i=0;i<n;i++) Ws[Wv[i]]++; for(i=1;i<m;i++) Ws[i]+=Ws[i-1]; for(i=n-1;i>=0;i--) sa[--Ws[Wv[i]]]=y[i]; for(t=x,x=y,y=t,p=1,x[sa[0]]=0,i=1;i<n;i++) x[sa[i]]=cmp(y,sa[i-1],sa[i],j)?p-1:p++; } return; } void rmqinit(int n) { int i,j,m; for(i=2;i<=n;i++) minh[i][0]=height[i]; m=floor(log((double)(n-1))/log(2.0)); for(i=1;i<=m;i++) { for(j=n;j>=2;j--) { minh[j][i]=minh[j][i-1]; if(j+(1<<(i-1))<=n) minh[j][i]=min(minh[j][i],minh[j+(1<<(i-1))][i-1]); } } return; } int rmqmin(int l,int r) { int m=floor(log((double)(r-l+1))/log(2.0)); return min(minh[l][m],minh[r-(1<<m)+1][m]); } int main() { char ch[MM]; while(scanf("%s", ch)!=EOF) { int len=strlen(ch), sa[MM], i; da(ch,sa,len+1,256); calheight(ch,sa,len); rmqinit(len); printf("%d\n", rmqmin(2,4)); } return 0; }