题目题目:http://poj.org/problem?id=3261
Milk Patterns
Time Limit: 5000MS | | Memory Limit: 65536K |
Total Submissions: 18880 | | Accepted: 8336 |
Case Time Limit: 2000MS |
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 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
题意概括:
给出一串长度为 N 的字符串
要求找最长可重叠子串,要求至少出现K次;
解题思路:
二分答案长度,按照height分组(按照排序后的后缀,很明显这样才是最优的)
判断条件就是判断是否有一组里面的元素数量 >= K;
AC code:
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 #include <set> 2 #include <map> 3 #include <cmath> 4 #include <vector> 5 #include <cstdio> 6 #include <cstring> 7 #include <iostream> 8 #include <algorithm> 9 #define INF 0x3f3f3f3f 10 #define LL long long 11 using namespace std; 12 const int MAXN = 2e5+10; 13 //const int M = 1e6+10; 14 int M; 15 int r[MAXN]; 16 int wa[MAXN], wb[MAXN], wv[MAXN], tmp[MAXN]; 17 int sa[MAXN]; //index range 1~n value range 0~n-1 18 int cmp(int *r, int a, int b, int l) 19 { 20 return r[a] == r[b] && r[a + l] == r[b + l]; 21 } 22 23 void da(int *r, int *sa, int n, int m) 24 { 25 int i, j, p, *x = wa, *y = wb, *ws = tmp; 26 for (i = 0; i < m; i++) ws[i] = 0; 27 for (i = 0; i < n; i++) ws[x[i] = r[i]]++; 28 for (i = 1; i < m; i++) ws[i] += ws[i - 1]; 29 for (i = n - 1; i >= 0; i--) sa[--ws[x[i]]] = i; 30 for (j = 1, p = 1; p < n; j *= 2, m = p) 31 { 32 for (p = 0, i = n - j; i < n; i++) y[p++] = i; 33 for (i = 0; i < n; i++) 34 if (sa[i] >= j) y[p++] = sa[i] - j; 35 for (i = 0; i < n; i++) wv[i] = x[y[i]]; 36 for (i = 0; i < m; i++) ws[i] = 0; 37 for (i = 0; i < n; i++) ws[wv[i]]++; 38 for (i = 1; i < m; i++) ws[i] += ws[i - 1]; 39 for (i = n - 1; i >= 0; i--) sa[--ws[wv[i]]] = y[i]; 40 for (swap(x, y), p = 1, x[sa[0]] = 0, i = 1; i < n; i++) 41 x[sa[i]] = cmp(y, sa[i - 1], sa[i], j) ? p - 1 : p++; 42 } 43 } 44 45 int Rank[MAXN]; //index range 0~n-1 value range 1~n 46 int height[MAXN]; //index from 1 (height[1] = 0) 47 void calheight(int *r, int *sa, int n) 48 { 49 int i, j, k = 0; 50 for (i = 1; i <= n; ++i) Rank[sa[i]] = i; 51 for (i = 0; i < n; height[Rank[i++]] = k) 52 for (k ? k-- : 0, j = sa[Rank[i] - 1]; r[i + k] == r[j + k]; ++k); 53 return; 54 } 55 56 int N, K; 57 bool check(int len) 58 { 59 int cnt = 1; 60 for(int i = 2; i <= N; i++){ 61 if(height[i] < len) cnt = 1; 62 else cnt++; 63 if(cnt >= K) return true; 64 } 65 return false; 66 } 67 68 int main() 69 { 70 scanf("%d %d", &N, &K); 71 for(int i = 0; i < N; i++){ 72 scanf("%d", &r[i]); 73 r[i]++; 74 M = max(M, r[i]); 75 } 76 r[N] = 0; 77 da(r, sa, N+1, M+1); 78 calheight(r, sa, N); 79 int ans = 0; 80 int L = 1, R = N, mid; 81 while(L <= R){ 82 mid = (L+R)>>1; 83 if(check(mid)){ 84 L = mid+1; 85 ans = max(mid, ans); 86 } 87 else R = mid-1; 88 } 89 printf("%d\n", ans); 90 return 0; 91 }