题面
Sol
二分答案+分组,判断有没有一个组的后缀个数不小于 k
做法
# include <bits/stdc++.h>
# define IL inline
# define RG register
# define Fill(a, b) memset(a, b, sizeof(a))
using namespace std;
typedef long long ll;
const int _(20010);
IL ll Read(){
RG char c = getchar(); RG ll x = 0, z = 1;
for(; c < '0' || c > '9'; c = getchar()) z = c == '-' ? -1 : 1;
for(; c >= '0' && c <= '9'; c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48);
return x * z;
}
int n, a[_], sa[_], rk[_], y[_], h[_], height[_], t[1000010], ans, K, mx;
IL bool Cmp(RG int i, RG int j, RG int k){ return y[i] == y[j] && y[i + k] == y[j + k]; }
IL void Sort(){
RG int m = mx;
for(RG int i = 1; i <= n; ++i) ++t[rk[i] = a[i]];
for(RG int i = 1; i <= m; ++i) t[i] += t[i - 1];
for(RG int i = n; i; --i) sa[t[rk[i]]--] = i;
for(RG int k = 1; k <= n; k <<= 1){
RG int l = 0;
for(RG int i = n - k + 1; i <= n; ++i) y[++l] = i;
for(RG int i = 1; i <= n; ++i) if(sa[i] > k) y[++l] = sa[i] - k;
for(RG int i = 0; i <= m; ++i) t[i] = 0;
for(RG int i = 1; i <= n; ++i) ++t[rk[y[i]]];
for(RG int i = 1; i <= m; ++i) t[i] += t[i - 1];
for(RG int i = n; i; --i) sa[t[rk[y[i]]]--] = y[i];
swap(rk, y); rk[sa[1]] = l = 1;
for(RG int i = 2; i <= n; ++i) rk[sa[i]] = Cmp(sa[i - 1], sa[i], k) ? l : ++l;
if(l >= n) break; m = l;
}
for(RG int i = 1; i <= n; ++i){
h[i] = max(0, h[i - 1] - 1);
if(rk[i] == 1) continue;
while(a[i + h[i]] == a[sa[rk[i] - 1] + h[i]]) ++h[i];
}
for(RG int i = 1; i <= n; ++i) height[i] = h[sa[i]];
}
IL bool Check(RG int x){
RG int cnt = 0;
for(RG int i = 2; i <= n; ++i){
if(height[i] < x) cnt = 0;
else cnt++;
if(cnt >= K - 1) return 1;
}
return 0;
}
int main(RG int argc, RG char* argv[]){
n = Read(); K = Read();
for(RG int i = 1; i <= n; ++i) a[i] = Read(), mx = max(a[i], mx);
Sort();
RG int l = 0, r = n;
while(l <= r){
RG int mid = (l + r) >> 1;
if(Check(mid)) ans = mid, l = mid + 1;
else r = mid - 1;
}
printf("%d
", ans);
return 0;
}