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 |
后缀数组407msQ^Q#include<cstdio> #include<cstdlib> #include<cstring> #include<cmath> #include<algorithm> using namespace std; int n,m,k,ans; int s[20100],c[20100],sa[20100],t[21000],t2[21000]; struct R{int no,r;}r[20100]; bool cmp(R a,R b){return a.r<b.r;} void build_sa(int m) { int i,*x=t,*y=t2; for(i=0;i<m;i++)c[i]=0; for(i=0;i<n;i++)c[x[i]=s[i]]++; for(i=1;i<m;i++)c[i]+=c[i-1]; for(i=n-1;i>=0;i--)sa[--c[x[i]]]=i; for(int k=1;k<=n;k<<=1) { int p=0; for(i=n-k;i<n;i++)y[p++]=i; for(i=0;i<n;i++)if(sa[i]>=k)y[p++]=sa[i]-k; for(i=0;i<m;i++)c[i]=0; for(i=0;i<n;i++)c[x[y[i]]]++; for(i=0;i<m;i++)c[i]+=c[i-1]; for(i=n-1;i>=0;i--)sa[--c[x[y[i]]]]=y[i]; swap(x,y); p=1;x[sa[0]]=0; for(i=1;i<n;i++) x[sa[i]]=(y[sa[i-1]]==y[sa[i]]&&y[sa[i-1]+k]==y[sa[i]+k])?p-1:p++; if(p>=n)break; m=p; } } void dfs(int l,int r,int len) { ans=max(ans,len+1); int tmp=-1,c=0; for(int i=l;i<=r;i++) { if(sa[i]+len+1>=n)continue; if(s[sa[i]+len+1]==tmp)c++; else { if(c>=k)dfs(i-c,i-1,len+1); c=1; tmp=s[sa[i]+len+1]; } } if(c>=k)dfs(r-c+1,r,len+1); } int main() { scanf("%d%d",&n,&k); for(int i=1;i<=n;i++) {scanf("%d",&r[i].r);r[i].no=i;} sort(r+1,r+n+1,cmp); int m=-1,tmp=-1; for(int i=1;i<=n;i++) { if(r[i].r!=tmp) { tmp=r[i].r; m++; } s[r[i].no-1]=m; } build_sa(m+1); memset(c,0,sizeof(c)); for(int i=0;i<n;i++) c[s[i]]++; int mx=0; for(int i=0;i<m;i++) mx=max(mx,c[i]); if(mx<k){ printf("0");return 0; } dfs(0,n-1,0); printf("%d",ans); return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator