Farmer John has noticed, the quality of milk given by he cows varies from day to day. On further investigation, he discovered so although he can ' t predict the quality of milk from one day to the next, there is some regular patterns in the daily milk quality.
To perform a rigorous study, he had invented a complex classification scheme by which each milk sample was recorded as an I Nteger between 0 and 1,000,000 inclusive, and have 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 could 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, at least one subsequence are repeated at least K times.
Old driver FJ has a long sequence of n, please help to find the longest substring that appears at least k times (can overlap between substrings).
The first line consists of two spaces separated by integers n and K, indicating the length of the sequence and the minimum number of occurrences required.
2nd.. A n+1 line that represents each number in the sequence.
Outputs an integer that represents the length of the substring that satisfies the test instructions.
8 2
1
2
3
2
3
2
3
1
4
First of all, needless to say, it is definitely a suffix array to do it again.
Because the substring length has some negative correlation with the number of occurrences, so the longest length of the two points, for a continuous h[i]>=mid, the number of occurrences of the substring is the number of successive suffixes.
Linux is so hard to use, but the third suffix array, finally AC.
1#include <cstdio>2#include <cstring>3#include <algorithm>4 using namespacestd;5 Const intMAXN =20005;6 intS[MAXN], SX[MAXN], SY[MAXN], ssum[20010], SA[MAXN], RK[MAXN], H[MAXN]; Ssum doesn't seem to need to open to 1,000,000, maybe the data is weak.7InlineBOOLcmpint* Y,intIintJintk) {8 returnY[i] = = Y[j] && y[i + K] = = Y[j +K];9 }Ten One voidGetsa (intNintm) { A intI, J, p, *x = SX, *y =Sy; - for(i =0; I < m; ++i) Ssum[i] =0; - for(i =0; I < n; ++i) ++ssum[x[i] =S[i]]; the for(i =1; I < m; ++i) Ssum[i] + = ssum[i-1]; - for(i = n-1; ~i; -i) sa[--ssum[x[i]] =i; - for(p = j =1; P < n; J <<=1, 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 < m; ++i) Ssum[i] =0; + for(i =0; I < n; ++i) + +Ssum[x[y[i]]; A for(i =1; I < m; ++i) Ssum[i] + = ssum[i-1]; at for(i = n-1; ~i; -i) sa[--ssum[x[y[i] []] =Y[i]; -Swap (x, y); x[sa[0]] =0; - for(p = i =1; I < n; ++i) X[sa[i] = cmp (y, Sa[i-1], Sa[i], j)? P-1: p++; - } - } - in voidGeth (intN) { - intJ, k =0; to for(inti =1; I <= N; ++i) Rk[sa[i] =i; + for(inti =0; I < n; h[rk[i++]] =k) - for(K. k--:0, j = Sa[rk[i]-1]; S[i + K] = = S[j + K]; ++k); the } * $ intSolveintXintKintN) {Panax Notoginseng inti =2; - while(1) { the intAns =1; + while(I <= n && h[i] < x) + +i; A if(i > N) Break; the while(I <= n && h[i] >=x) { +++ans; ++i; - } $ if(Ans >= k)return true; $ } - return false; - } the - intMain () {Wuyi intN, K; thescanf"%d%d", &n, &k); - for(inti =0; I < n; ++i) scanf ("%d", S +i); WuGetsa (n +1,20005); Geth (n); - intL =1, r = (n >>1) +1; About while(L < R-1) { $ intMID = L + R >>1; - if(Solve (Mid, K, n)) L =mid; - ElseR =mid; - } Aprintf"%d\n", L); + return 0; the}
I have to go with the string recently ...
[POJ3261] Milk Patterns (suffix array + dichotomy)