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

 
解题 :可重叠的重复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
",ret);
72     return 0;
73 }
View Code
 1 #include <cstdio>
 2 #include <iostream>
 3 using namespace std;
 4 const int maxn = 100010;
 5 int sa[maxn],height[maxn],rk[maxn],n;
 6 int tmp[2][maxn],c[maxn],s[maxn],k;
 7 void build_sa(int m) {
 8     int i,*x = tmp[0],*y = tmp[1];
 9     for(i = 0; i < m; ++i) c[i] = 0;
10     for(i = 0; i < n; ++i) c[x[i] = s[i]]++;
11     for(i = 1; i < m; ++i) c[i] += c[i-1];
12     for(i = n-1; i >= 0; --i) sa[--c[x[i]]] = i;
13 
14     for(int k = 1; k <= n; k <<= 1) {
15         int p = 0;
16         for(i = n-k; i < n; ++i) y[p++] = i;
17         for(i = 0; i < n; ++i) if(sa[i] >= k) y[p++] = sa[i] - k;
18         for(i = 0; i < m; ++i) c[i] = 0;
19         for(i = 0; i < n; ++i) c[x[y[i]]]++;
20         for(i = 1; i < m; ++i) c[i] += c[i-1];
21         for(i = n-1; i >= 0; --i) sa[--c[x[y[i]]]] = y[i];
22         swap(x,y);
23         x[sa[0]] = 0;
24         for(p = i = 1; i < n; ++i)
25             if(y[sa[i]] == y[sa[i-1]] && y[sa[i] + k] == y[sa[i-1] + k])
26                 x[sa[i]] = p-1;
27             else x[sa[i]] = p++;
28         if(p >= n) break;
29         m = p;
30     }
31 }
32 bool check(int m,int len = 0) {
33     for(int i = 1; i < n; ++i)
34         if(height[i] < m) len = 1;
35         else if(++len >= k) return true;
36     return false;
37 }
38 void getHeight() {
39     int i,j,k = 0;
40     for(i = 0; i < n; ++i) rk[sa[i]] = i;
41     for(i = 0; i < n; ++i) {
42         if(k) --k;
43         j = sa[rk[i]-1];
44         while(i + k < n && j + k < n&&s[i+k] == s[j+k]) ++k;
45         height[rk[i]] = k;
46     }
47 }
48 int main() {
49     while(~scanf("%d%d",&n,&k)) {
50         for(int i = 0; i < n; ++i){
51             scanf("%d",s+i);
52             ++s[i];
53         }
54         s[n++] = 0;
55         build_sa(256);
56         getHeight();
57         int ret = 0,low = 0,high = n;
58         while(low <= high) {
59             int mid = (low + high)>>1;
60             if(check(mid)) {
61                 ret = mid;
62                 low = mid + 1;
63             } else high = mid-1;
64         }
65         printf("%d
",ret);
66     }
67     return 0;
68 }
View Code
原文地址:https://www.cnblogs.com/crackpotisback/p/4634193.html