POJ 3261 Milk Patterns

Milk Patterns

Time Limit: 5000ms

Memory Limit: 65536KB

This problem will be judged on PKU. Original ID: 3261
64-bit integer IO format: %lld      Java class name: Main

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

Line 1: Two space-separated integers: N and K
Lines 2..N+1: N integers, one per line, the quality of the milk on day i appears on the ith line.

Output

Line 1: One integer, the length of the longest pattern which occurs at least K times

Sample Input

8 2
1
2
3
2
3
2
3
1

Sample Output

4

Source

USACO 2006 December Gold

解题 :可重叠的重复k次的最长子串

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 using namespace std;
 5 const int maxn = 100010;
 6 int rk[maxn],sa[maxn],height[maxn],rp;
 7 int c[maxn],t[maxn],t2[maxn],s[maxn],n;
 8 void build_sa(int m) {
 9     int i,j,*x = t,*y = t2;
10     for(i = 0; i < m; ++i) c[i] = 0;
11     for(i = 0; i < n; ++i) c[x[i] = s[i]]++;
12     for(i = 1; i < m; ++i) c[i] += c[i-1];
13     for(i = n-1; i >= 0; --i) sa[--c[x[i]]] = i;
14
15     for(int k = 1; k <= n; k <<= 1) {
16         int p = 0;
17         for(i = n-k; i < n; ++i) y[p++] = i;
18         for(i = 0; i < n; ++i)
19             if(sa[i] >= k) y[p++] = sa[i] - k;
20         for(i = 0; i < m; ++i) c[i] = 0;
21         for(i = 0; i < n; ++i) c[x[y[i]]]++;
22         for(i = 1; i < m; ++i) c[i] += c[i-1];
23         for(i = n-1; i >= 0; --i) sa[--c[x[y[i]]]] = y[i];
24
25         swap(x,y);
26         p = 1;
27         x[sa[0]] = 0;
28         for(i = 1; i < n; ++i)
29             if(y[sa[i]] == y[sa[i-1]] && y[sa[i]+k] == y[sa[i-1]+k])
30                 x[sa[i]] = p-1;
31             else x[sa[i]] = p++;
32         if(p >= n) break;
33         m = p;
34     }
35 }
36 void getHeight(){
37     int i,j,k = 0;
38     for(i = 0; i < n; ++i) rk[sa[i]] = i;
39     for(i = 0; i < n; ++i){
40         if(k) --k;
41         j = sa[rk[i]-1];
42         while(i + k < n && j + k < n && s[i+k] == s[j+k]) ++k;
43         height[rk[i]] = k;
44     }
45 }
46 bool check(int m){
47     int sum = 1;
48     for(int i = 1; i < n; ++i){
49         if(height[i] < m) sum = 1;
50         else if(++sum >= rp) return true;
51     }
52     return false;
53 }
54 int main() {
55     scanf("%d%d",&n,&rp);
56     for(int i = 0; i < n; ++i){
57         scanf("%d",s+i);
58         ++s[i];
59     }
60     s[n++] = 0;
61     build_sa(256);
62     getHeight();
63     int low = 1,high = n,ret;
64     while(low <= high){
65         int mid = (low + high)>>1;
66         if(check(mid)){
67             ret = mid;
68             low = mid + 1;
69         }else high = mid - 1;
70     }
71     printf("%d\n",ret);
72     return 0;
73 }

时间: 2024-10-21 17:29:41

POJ 3261 Milk Patterns的相关文章

POJ 3261 Milk Patterns (求可重叠的k次最长重复子串)

Milk Patterns Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 14094   Accepted: 6244 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,

poj 3261 Milk Patterns 后缀数组+二分

1 /*********************************************************** 2 题目: Milk Patterns(poj 3261) 3 链接: http://poj.org/problem?id=3261 4 题意: 给一串数字,求这些数字中公共子串个数大于k的 5 最长串. 6 算法: 后缀数组+二分 7 ***********************************************************/ 8 #incl

POJ 3261 Milk Patterns 可重复k次的最长重复子串

Milk PatternsTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://poj.org/problem?id=3261 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 c

POJ 3261 Milk Patterns 后缀数组求 一个串种 最长可重复子串重复至少k次

Milk Patterns 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 r

POJ 3261 Milk Patterns sa+二分

Milk Patterns Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 11944   Accepted: 5302 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,

POJ - 3261 Milk Patterns (后缀数组求可重叠的 k 次最长重复子串)

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 pattern

POJ 3261 Milk Patterns(后缀数组+二分答案)

[题目链接] http://poj.org/problem?id=3261 [题目大意] 求最长可允许重叠的出现次数不小于k的子串. [题解] 对原串做一遍后缀数组,二分子串长度x,将前缀相同长度超过x的后缀分组, 如果存在一个大小不小于k的分组,则说明答案可行,分治得到最大可行解就是答案. [代码] #include <cstdio> #include <cstring> #include <vector> using namespace std; const int

后缀数组 POJ 3261 Milk Patterns

题目链接 题意:可重叠的 k 次最长重复子串.给定一个字符串,求至少出现 k 次的最长重复子串,这 k 个子串可以重叠. 分析:与POJ 1743做法类似,先二分答案,height数组分段后统计 LCP>=m 的子串的个数. #include <cstdio> #include <cstring> #include <algorithm> const int N = 2e4 + 5; int sa[N], rank[N], height[N]; int t[N],

POJ 3261 Milk Patterns 后缀数组

用后缀数组求重复出现至少k次的可重叠最长子串的长度, 当然是可以用hash搞的,用后缀数组的话,只要在分组之后看看个数是不是大于等于k #include <cstdio> #include <cstring> #include <algorithm> #include <queue> #include <stack> #include <map> #include <set> #include <climits>