大致题意:
给出一个长度为n的字符串,再给出一个数字k。求出现至少k次的子串中长度最大是多少,注:可覆盖。
大致思路:
后缀数组+二分判定……水水。
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int nMax =1000012;
int num[nMax];
int sa[nMax], rank[nMax], height[nMax];
int wa[nMax], wb[nMax], wv[nMax], wd[nMax];
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 n, int m){ // 倍增算法 r为待匹配数组 n为总长度 m为字符范围
int i, j, p, *x = wa, *y = wb, *t;
for(i = 0; i < m; i ++) wd[i] = 0;
for(i = 0; i < n; i ++) wd[x[i]=r[i]] ++;
for(i = 1; i < m; i ++) wd[i] += wd[i-1];
for(i = n-1; i >= 0; i --) sa[-- wd[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 ++) wd[i] = 0;
for(i = 0; i < n; i ++) wd[wv[i]] ++;
for(i = 1; i < m; i ++) wd[i] += wd[i-1];
for(i = n-1; i >= 0; i --) sa[-- wd[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 ++;
}
}
}
void calHeight(int *r, int n){ // 求height数组。
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 ++);
}
}
int loc[nMax],m;
char str[nMax],res[nMax];
bool vis[1004];
int abs(int a){if(a>0)return a;return -a;}
bool check(int mid,int len,int k){ //长度为mid的子串是否出现了k次
int i,j,tot=0;
for(i=2;i<=len;i++){
if(height[i]<mid)tot=0;
else{
tot++;
if(tot==k-1)return 1;
}
}
return 0;
}
int main(){
int i,j,n,ans,k;
while(scanf("%d%d",&n,&k)!=EOF){
ans=0;
for(i=0;i<n;i++){
scanf("%d",&num[i]);
num[i]++;
}
num[n]=0;
da(num,n+1,1000002);
calHeight(num,n);
int left=0,right=n,mid;//开始二分
while(right>=left){
mid=(right+left)/2;
if(check(mid,n,k)){
left=mid+1;
ans=mid;
}
else{
right=mid-1;
}
}
printf("%d\n",ans);
}
return 0;
}