标签:
后缀数组。求可重叠的至少出现k次的最长子串。
#pragma comment(linker, "/STACK:1024000000,1024000000") #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> #include<vector> #include<map> #include<set> #include<queue> #include<stack> #include<iostream> using namespace std; typedef long long LL; const double pi=acos(-1.0),eps=1e-8; void File() { freopen("D:\\in.txt","r",stdin); freopen("D:\\out.txt","w",stdout); } inline int read() { char c = getchar(); while(!isdigit(c)) c = getchar(); int x = 0; while(isdigit(c)) { x = x * 10 + c - ‘0‘; c = getchar(); } return x; } const int maxn=2000000+10; int wa[maxn],wb[maxn],wv[maxn],WS[maxn]; int cmp(int *r,int a,int b,int l) { return r[a]==r[b]&&r[a+l]==r[b+l]; } void da(int *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*=2,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; } int rank[maxn],height[maxn]; void calheight(int *r,int *sa,int n) { int i,j,k=0; for(i=1; i<=n; i++) rank[sa[i]]=i; for(i=0; i<n; height[rank[i++]]=k) for(k?k--:0,j=sa[rank[i]-1]; r[i+k]==r[j+k]; k++); return; } int n,k,a[maxn],SA[maxn]; bool check(int x) { int Min=0,Max=0; height[n+1]=0; for(int i=1;i<=n+1;i++) { if(height[i]>=x) { Min=min(Min,i), Max=max(Max,i); continue; } if(Max-Min+1>=k) return 1; Min=i; Max=i; } return 0; } int main() { while(~scanf("%d%d",&n,&k)) { for(int i=0;i<n;i++) scanf("%d",&a[i]); a[n]=0; da(a,SA,n+1,1000001); calheight(a,SA,n); int L=0,R=n+1,ans=0; while(L<=R) { int mid=(L+R)/2; if(check(mid)) ans=mid,L=mid+1; else R=mid-1; } printf("%d\n",ans); } return 0; }
标签:
原文地址:http://www.cnblogs.com/zufezzt/p/5720700.html