题意:求可重叠k次的最长重复子串.
思路:和这题类似,http://blog.csdn.net/u013008291/article/details/48105673
先二分答案,判断是否有长度为s的可重叠k次的重复子串,之后将后缀分成若干组。要判断有没有一组的后缀个数大与等于k,有则抬高二分答案的上界。
/***********************************************
* Author: fisty
* Created Time: 2015-08-24 20:55:24
* File Name : B.cpp
*********************************************** */
#include <iostream>
#include <cstring>
#include <deque>
#include <cmath>
#include <queue>
#include <stack>
#include <list>
#include <map>
#include <set>
#include <string>
#include <vector>
#include <cstdio>
#include <bitset>
#include <algorithm>
using namespace std;
#define Debug(x) cout << #x << " " << x <<endl
#define Memset(x, a) memset(x, a, sizeof(x))
const int INF = 0x3f3f3f3f;
typedef long long LL;
typedef pair<int, int> P;
#define FOR(i, a, b) for(int i = a;i < b; i++)
#define lson l, m, k<<1
#define rson m+1, r, k<<1|1
#define MAX_N 200100
#define MAX_M 1000010
int sa[MAX_N], rank[MAX_N], tmp[MAX_N+1];
int lcp[MAX_N];
int a[MAX_N];
int wa[MAX_M], wb[MAX_M], wv[MAX_M], wd[MAX_M];
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, p = 0;
for(i = 1; i <= n; i ++) rank[sa[i]] = i;
for(i = 0; i < n; lcp[rank[i ++]] = p){
for(p ? p -- : 0, j = sa[rank[i]-1]; r[i+p] == r[j+p]; p ++);
}
}
int n, k;
bool ok(int x){
int _max = 1;
for(int i = 2;i <= n; i++){
if(lcp[i] >= x) {
_max++;
}else{
_max = 1;
}
if(_max >= k) return true;
}
return false;
}
int solve(){
da(a, n+1, MAX_M-1);
calHeight(a, n);
int l = 1, r = n;
int ans = 0;
while(r - l >= 0){
int mid = (l + r) / 2;
if(ok(mid)){
ans = mid;
l = mid + 1;
}else r = mid - 1;
}
return ans;
}
int main(){
//freopen("in.cpp", "r", stdin);
//cin.tie(0);
//ios::sync_with_stdio(false);
while(~scanf("%d%d", &n , &k)){
for(int i = 0;i < n; i++){
scanf("%d", &a[i]);
}
a[n] = 0;
int ans = solve();
printf("%d\n", ans);
}
return 0;
}