# [洛谷 2852][USACO06DEC] 牛奶模式 Milk Patterns【后缀数组+二分答案】

• 2018-02-24
• 0
• 0

## Problem:

### 题目描述

Farmer John has noticed that the quality of milk given by his cows varies from day to day. On further investigation, he discovered that although he can't predict the quality of milk from one day to the next, there are some regular patterns in the daily milk quality.

To perform a rigorous study, he has invented a complex classification scheme by which each milk sample is recorded as an integer between 0 and 1,000,000 inclusive, and has recorded data from a single cow over N (1 ≤ N ≤ 20,000) days. He wishes to find the longest pattern of samples which repeats identically at least K (2 ≤ K ≤ N) times. This may include overlapping patterns -- 1 2 3 2 3 2 3 1 repeats 2 3 2 3 twice, for example.

Help Farmer John by finding the longest repeating subsequence in the sequence of samples. It is guaranteed that at least one subsequence is repeated at least K times.

### 输入输出格式

Line 1: Two space-separated integers: N and K

Lines 2..N+1: N integers, one per line, the quality of the milk on day i appears on the ith line.

Line 1: One integer, the length of the longest pattern which occurs at least K times

```8 2
1
2
3
2
3
2
3
1```

`4`

## Code: O(nlogn) [2605K, 4MS]

```#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<cassert>
#include<iostream>
#include<algorithm>
using namespace std;

int N, K, a, disc;
int SA, rnk, ht;

inline void buildSA(int m){
int *x = new int, *y = new int, *cnt = new int;
for(register int i = 0; i < m; i++) cnt[i] = 0;
for(register int i = 0; i < N; i++) cnt[x[i] = a[i]]++;
for(register int i = 1; i < m; i++) cnt[i] += cnt[i - 1];
for(register int i = N - 1; i >= 0; i--) SA[--cnt[x[i]]] = i;
for(register int k = 1; k <= N; k <<= 1){
int p = 0;
for(register int i = N - k; i < N; i++) y[p++] = i;
for(register int i = 0; i < N; i++) if(SA[i] >= k) y[p++] = SA[i] - k;
for(register int i = 0; i < m; i++) cnt[i] = 0;
for(register int i = 0; i < N; i++) cnt[x[y[i]]]++;
for(register int i = 1; i < m; i++) cnt[i] += cnt[i - 1];
for(register int i = N - 1; i >= 0; i--) SA[--cnt[x[y[i]]]] = y[i];
swap(x, y), p = 1, x[SA] = 0;
for(register int i = 1; i < N; i++) x[SA[i]] = (y[SA[i]] == y[SA[i - 1]] && y[SA[i] + k] == y[SA[i - 1] + k]) ? p - 1 : p++;
if(p == N) break;
m = p;
}
for(register int i = 0; i < N; i++) rnk[SA[i]] = i;
int k = 0, j;
for(register int i = 0; i < N; ht[rnk[i++]] = k)
for(k ? k-- : 0, j = SA[rnk[i] - 1]; a[i + k] == a[j + k]; k++);
delete []x, delete []y, delete []cnt;
}

inline bool check(int k){
for(register int i = 1, cnt = 0; i < N; i++){
if(ht[i] < k) cnt = 0;
else if(++cnt == K - 1) return 1;
}
return 0;
}

int main(){
scanf("%d%d", &N, &K);
for(register int i = 0; i < N; i++) scanf("%d", a + i), disc[i + 1] = a[i];
sort(disc + 1, disc + N + 1);
int k = unique(disc + 1, disc + N + 1) - disc - 1;
for(register int i = 0; i < N; i++) a[i] = lower_bound(disc, disc + k, a[i]) - disc;  // Discretization
a[N++] = 0, buildSA(k + 1);
int l = 0, r = N / K;
while(l < r){
int mid = l + r + 1 >> 1;
if(check(mid)) l = mid;
else r = mid - 1;
}
printf("%d\n", l);
return 0;
}
```

#### 评论

49750

https://github.com/Darkleafin

OPEN AT 2017.12.10

Please refresh the page if the code cannot be displayed normally.

https://visualgo.net/en

- Theme by Qzhai