给n(n<=20000),k和一个长度为n的由0..1,000,000 组成的字符串,问这个串中最长的重复k次的子串的长度。依然是后缀数组的模板,看到每个字符范围挺大,还是离散化一下比较安全。用离散化后的串构造出height数组后,因为构造好后缀数组后,重复出现的子串所在的后缀串一定是相邻的,所以直接枚举起点i终点j,长度即重复的次数k,用RMQ求一个i..j的最小值,记录一下这些最小值中的最大值输出即可。 不过这道题好像用字符串哈希更快一点,倍增的sa跑了100ms+...下面贴一个sa的代码。
#include <iostream> #include <algorithm> #include <cmath> #include <cstdio> #include <algorithm> #include <stack> #include <queue> #include <map> #include <string> #include <cstring> #include <string> using namespace std; typedef long long ll; const int maxn=40000+40; int s[maxn],rs[maxn]; int sa[maxn],t[maxn],t2[maxn],c[maxn]; int n,m,k; char s1[maxn],s2[maxn]; int rank[maxn],height[maxn]; int l1,l2; void getheight(int n) { int i,j,k=0; for (i=0; i<=n; i++) rank[sa[i]]=i; for (i=0; i<n; i++) { if (k) k--; int j=sa[rank[i]-1]; while(s[i+k]==s[j+k]) k++; height[rank[i]]=k; } } void build_ss(int m,int n) { n++; int i,*x=t,*y=t2; for (int i=0; i<m; i++) c[i]=0; for (int i=0; i<n; i++) c[x[i]=s[i]]++; for (int i=1; i<m; i++) c[i]+=c[i-1]; for (int 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=1; 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; } } int d[maxn][20]; void RMQ_init() { for (int i=1; i<=n; i++) d[i][0]=height[i]; for (int j=1; (1<<j)<=n; j++) for (int i=1; i+(1<<(j-1))<=n; i++) d[i][j]=min(d[i][j-1],d[i+(1<<(j-1))][j-1]); } int RMQ(int L,int R) { int k=0; while ((1<<(k+1))<=R-L+1) k++; return min(d[L][k],d[R-(1<<k)+1][k]); } void print(int k) { for (int i=k; i<n; i++) printf("%d ",s[i]); printf("\n"); } struct node { int dt,id,neu; }a[20020]; bool cmpid(node p,node q) { return p.id<q.id; } bool cmpdt(node p,node q) { if (p.dt!=q.dt) return p.dt<q.dt; return p.id<q.id; } int main() { // freopen("in.txt","r",stdin); while(~scanf("%d%d",&n,&k)) { for (int i=0; i<n; i++) scanf("%d",&a[i].dt),a[i].id=i; sort(a,a+n,cmpdt); int rank=1; a[0].neu=1; for (int i=1; i<n; i++) { if (a[i].dt!=a[i-1].dt) rank++; a[i].neu=rank; } sort(a,a+n,cmpid); for (int i=0;i<n; i++) s[i]=a[i].neu; s[n]=0; build_ss(rank+1,n); getheight(n); int i=1,j=i+k-1; int ans=0; RMQ_init(); while(j<=n) { ans=max(ans,RMQ(i+1,j)); i++; j++; } printf("%d\n",ans); } return 0; }