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次的最长子串
 
POJ 3261 Milk Patterns
 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 }
View Code

 

你可能感兴趣的:(Pattern)