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.
Input
Lines 2.. N+1: N integers, one per line, the quality of the milk on day i appears on the ith line.
Output
Sample Input
8 2 1 2 3 2 3 2 3 1
Sample Output
4
Title effect: a given number of columns, the number of columns determined in the longest contiguous subsequence, and satisfies the sequence occurs more than k times the number of columns.
Thinking: enumeration half length L, then L is recorded as the hash value string length from each of the start position, the final hash value to traverse these want to wait to see whether the number is greater than equal to k
1 #include <stdio.h> 2 #include <algorithm> 3 #include <iostream> 4 #include <stdlib.h> 5 #include <string> 6 #include <string.h> 7 #include <math.h> 8 #include <vector> 9 #include <queue> 10 #include <stack> 11 #include <map> 12 #include <set> 13 14 15 #define INF 0x3f3f3f3f 16 #define LL long long 17 18typedef unsigned long long eye; 19 const int maxn 1E5 + = 10 ; 20 21 int a [maxn]; 22 int s [maxn]; 23 out base = 131 ; 24 out mod = 1e9 + 7 ; 25 out p [maxn]; 26 out h1 [maxn] H2 [maxn]; 27 eye q [maxn]; 28 int n, k; 29 30 eye get_hash (h eye [], int s, int r) { 31 return (h [r] - h [l-1]*p[r-l+1]); 32 } 33 34 bool check(int len) { 35 int cnt = 0; 36 for (int i=1;i+len-1<=n;i++) { 37 q[cnt++] = get_hash(h1,i,i+len-1); 38 } 39 std::sort(q,q+cnt); 40 for (int i=0;i<cnt;i++) { 41 int ans = 0; 42 is int J; 43 is for (J = I; J <CNT; J ++ ) { 44 is IF (Q [I] == Q [J]) 45 ANS ++ ; 46 is the else 47 BREAK ; 48 } 49 I = J- . 1 ; / / may be slightly cut at 50 IF (ANS> = K) 51 is return to true ; 52 is } 53 is return to false ; 54 is } 55 56 is 57 is 58 59 int main(){ 60 std::cin >> n >> k; 61 p[0] = 1; 62 for (int i=1;i<maxn;i++) { 63 p[i] = p[i-1] * base; 64 } 65 for (int i=1;i<=n;i++) { 66 std::cin >> s[i]; 67 } 68 for (int i=1;i<=n;i++) { 69 h1[i] = h1[i-1] * base + s[i]; 70 } 71 int l = 0,r = n; 72 int ans = 0; 73 while (l <= r) { 74 int mid = (l + r) >> 1; 75 if (check(mid)) { 76 ans = mid; 77 l = mid + 1; 78 } 79 else 80 r = mid - 1; 81 } 82 printf("%d\n",ans); 83 return 0; 84 }