Description
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 assigned Ted a complex classification scheme by which each milk sample is recorded as an integer between 0 and 1,000,000 random Sive, and has recorded data from a single cow overN(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 leastKTimes.
Input
Line 1: two space-separated integers:
NAnd
K
Lines 2 ..
N+ 1:
NIntegers, one per line, the quality of the milk on day
IAppears on
ITh line.
Output
Line 1: One integer, the length of the longest pattern which occurs at least
KTimes
Sample Input
8 212323231
Sample output
4
Given a string, the longest repeated substring that appears at least K times can overlap.
Idea: Like the previous question poj-1743 musical theme, it is also a binary answer, and then the suffix is divided into several groups. The difference is that, here, we need to determine whether there is a group with the suffix heweigh [] not less than mid. If yes, then K consecutive existence conditions are met.
# Include <iostream> # include <cstdio> # include <cstring> # include <algorithm> # include <queue> using namespace STD; const int maxn = 20010; int SA [maxn]; // SA array, it means to sort the N suffixes of S from small to large and put the starting positions of the suffixes of sorted // into SA in sequence int T1 [maxn], T2 [maxn], c [maxn]; int rank [maxn], height [maxn]; int s [maxn]; void build_sa (INT s [], int N, int m) {int I, j, P, * x = T1, * Y = t2; for (I = 0; I <m; I ++) C [I] = 0; for (I = 0; I <n; I ++) C [x [I] = S [I] ++; for (I = 1; I <m; I ++) C [I] + = C [I-1]; for (I = n-1; i> = 0; I --) SA [-- C [x [I] = I; for (j = 1; j <= N; j <= 1) {P = 0; for (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 <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] + J] = Y [SA [I] + J]? P-1: P ++; If (P> = N) break; M = P ;}} void getheight (INT s [], 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 --; j = sa [rank [I]-1]; while (s [I + k] = s [J + k]) K ++; height [rank [I] = K ;}} int check (INT N, int K, int mid) {int num = 1; for (INT I = 2; I <= N; I ++) {If (height [I]> = mid) {num ++; If (Num> = k) return 1;} else num = 1;} return 0 ;} Int main () {int N, K; while (scanf ("% d", & N, & K )! = EOF) {int max = 0; For (INT I = 0; I <n; I ++) {scanf ("% d", & S [I]); max = max (max, s [I]);} s [N] = 0; // notice build_sa (S, N + 1, Max + 1); getheight (S, n); int L = 0, r = N; int ans = 0; while (L <= r) {int mid = L + r> 1; if (check (n, k, mid) {ans = mid; L = Mid + 1;} else r = mid-1;} printf ("% d \ n ", ans);} return 0 ;}
Poj-3261 milk patterns)