Time Limit: 5000MS | Memory Limit: 65536K | |
Total Submissions: 12146 | Accepted: 5401 | |
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
Output
Sample Input
8 2 1 2 3 2 3 2 3 1
Sample Output
4
题意:给你一个字符串, 让你求出 出现次数至少k次的最长可重叠子串并输出长度。
思路:同1743,二分查找答案。
对每次查找值mid,把后缀按height值不小于mid来分组。下面只需要判断每一组中的后缀个数是否不小于k,若存在一组则满足,否则不满足。
AC代码:
#include <cstdio> #include <cstring> #include <algorithm> #define MAXN 20000+10 using namespace std; int cmp(int *r, int a, int b, int l) { return (r[a] == r[b]) && (r[a+l] == r[b+l]); } int wa[MAXN], wb[MAXN], ws[MAXN], wv[MAXN]; int rank[MAXN], height[MAXN]; void DA(int *r, int *sa, int n, int m) { int i, j, p, *x = wa, *y = wb, *t; for(i = 0; i < m; i++) ws[i] = 0; for(i = 0; i < n; i++) ws[x[i]=r[i]]++; for(i = 1; i < m; i++) ws[i] += ws[i-1]; for(i = n-1; i >= 0; i--) sa[--ws[x[i]]] = i; for(j = 1, p = 1; p < n; j *= 2, 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 < n; i++) wv[i] = x[y[i]]; for(i = 0; i < m; i++) ws[i] = 0; for(i = 0; i < n; i++) ws[wv[i]]++; for(i = 1; i < m; i++) ws[i] += ws[i-1]; for(i = n-1; i >= 0; i--) sa[--ws[wv[i]]] = y[i]; for(t = x, x = y, y = t, p = 1, x[sa[0]] = 0, i = 1; i < n; i++) x[sa[i]] = cmp(y, sa[i-1], sa[i], j) ? p-1 : p++; } } void calheight(int *r, int *sa, int n) { int i, j, k = 0; for(i = 1; i <= n; i++) rank[sa[i]] = i; for(i = 0; i < n; height[rank[i++]] = k) for(k ? k-- : 0, j = sa[rank[i]-1]; r[i+k] == r[j+k]; k++); } int sa[MAXN]; bool judge(int mid, int n, int k) { int sum;//记录次数 for(int i = 1; i <= n; i++) { sum = 1; while(height[i] >= mid)//累加 向下查找 i++, sum++; if(sum >= k)//大于或者等于k 有一个满足即可 return true; } return false; } int a[MAXN]; int main() { int N, K; while(scanf("%d%d", &N, &K) != EOF) { int rec = 0; for(int i = 0; i < N; i++) scanf("%d", &a[i]), rec = max(rec, a[i]); a[N] = 0; DA(a, sa, N+1, rec + 1);//注意N的值 calheight(a, sa, N);//注意N的值 //二分 int left = 0, right = N; int ans = 0; while(right >= left) { int mid = (left + right) >> 1; if(judge(mid, N, K)) { ans = max(ans, mid); left = mid + 1; } else right = mid - 1; } printf("%d\n", ans); } return 0; }