Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
ozy#include<cstdio> #include<cstring> #define maxn 20000 int n,m; int wr[maxn],rank[maxn],a[maxn],height[maxn],sa[maxn],sort[maxn],y[maxn]; void init(int n) { memset(rank,0,sizeof(rank)); memset(a,0,sizeof(a)); memset(sa,0,sizeof(sa)); int i,k,p; scanf("%d",&p); for(i=1;i<n;i++) { scanf("%d",&k); a[i]=k-p+100; p=k; } } bool cmp(int x,int y,int z) { if(wr[x+z]==wr[y+z] && wr[x]==wr[y]) return true; return false; } void get_sa(int n,int m) { int i; for(i=1;i<=n;i++) rank[i]=a[i]; for(i=0;i<=m;i++) sort[i]=0; for(i=1;i<=n;i++) sort[a[i]]++; for(i=1;i<=m;i++) sort[i]+=sort[i-1]; for(i=n;i>=1;i--) sa[sort[a[i]]--]=i; int ln=1,p=0; while(ln<n) { int k=0; for(i=n-ln+1;i<=n;i++) y[++k]=i; for(i=1;i<=n;i++) if(sa[i]>ln) y[++k]=sa[i]-ln; for(i=1;i<=n;i++) wr[i]=rank[y[i]]; for(i=0;i<=m;i++) sort[i]=0; for(i=1;i<=n;i++) sort[wr[i]]++; for(i=1;i<=m;i++) sort[i]+=sort[i-1]; for(i=n;i>=1;i--) sa[sort[wr[i]]--]=y[i]; for(i=1;i<=n;i++) wr[i]=rank[i]; rank[sa[1]]=1;p=1; for(int i=2;i<=n;i++) { if(cmp(sa[i-1],sa[i],ln)==false) p++; rank[sa[i]]=p; } m=p;ln*=2; } } void get_height(int n) { int i,j,k=0; for(i=1;i<=n;i++) { j=sa[rank[i]-1]; if(k>0) k--; while(a[i+k]==a[j+k]) k++; height[rank[i]]=k; } } bool check(int n,int k) { int i,maxx=sa[1],minn=sa[1]; for(i=2;i<=n;i++) { if (height[i]<k) maxx=minn=sa[i]; else { if (sa[i]<minn) minn=sa[i]; if (sa[i]>maxx) maxx=sa[i]; if (maxx-minn>k) return true; } } return false; } void work(int n) { int l,r,mid,ans; l=1;r=n; while(l<=r) { mid=(l+r)/2; if(check(n,mid)==true) { ans=mid; l=mid+1; } else r=mid-1; } if(ans>=4) printf("%d\n",ans+1); else printf("0\n"); } int main() { while(1) { scanf("%d",&n); if(n==0) break; init(n); get_sa(n-1,200); get_height(n-1); work(n-1); } } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator