poj3261 Milk Patterns(后缀数组)

                                       Milk Patterns


Time Limit: 5000MS


Memory Limit: 65536K


Total Submissions: 12571


Accepted: 5572


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 ≤ KN) 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次的可重叠最长子串。

二分长度+划分height,然后判断是否存在一组的数目不小于k即可。

需要注意的是:用末尾添0的方法解决n==1时的RE问题,但是在can中需要忽略height[0]且考虑height[1..n]。

【代码】

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<iostream>
 4 #define FOR(a,b,c) for(int a=(b);a<=(c);a++)
 5 using namespace std;
 6
 7 const int maxn = 20000+10;
 8 const int maxm = 1000000+10;
 9
10 int s[maxn];
11 int sa[maxn],c[maxm],t[maxn],t2[maxn];
12
13 void build_sa(int m,int n) {
14     int i,*x=t,*y=t2;
15     for(i=0;i<m;i++) c[i]=0;
16     for(i=0;i<n;i++) c[x[i]=s[i]]++;
17     for(i=1;i<m;i++) c[i]+=c[i-1];
18     for(i=n-1;i>=0;i--) sa[--c[x[i]]]=i;
19
20     for(int k=1;k<=n;k<<=1) {
21         int p=0;
22         for(i=n-k;i<n;i++) y[p++]=i;
23         for(i=0;i<n;i++) if(sa[i]>=k) y[p++]=sa[i]-k;
24
25         for(i=0;i<m;i++) c[i]=0;
26         for(i=0;i<n;i++) c[x[y[i]]]++;
27         for(i=0;i<m;i++) c[i]+=c[i-1];
28         for(i=n-1;i>=0;i--) sa[--c[x[y[i]]]]=y[i];
29
30         swap(x,y);
31         p=1; x[sa[0]]=0;
32         for(i=1;i<n;i++)
33             x[sa[i]]=y[sa[i]]==y[sa[i-1]] && y[sa[i]+k]==y[sa[i-1]+k]?p-1:p++;
34         if(p>=n) break;
35         m=p;
36     }
37 }
38 int rank[maxn],height[maxn];
39 void getHeight(int n) {
40     int i,j,k=0;
41     for(i=0;i<=n;i++) rank[sa[i]]=i;
42     for(i=0;i<n;i++) {
43         if(k) k--;
44         j=sa[rank[i]-1];
45         while(s[j+k]==s[i+k]) k++;
46         height[rank[i]]=k;
47     }
48 }
49
50 int n,k;
51
52 bool can(int limit) {
53     int cnt=1;                //忽略height[0] 截至height[n]
54     for(int i=2;i<=n;i++) {
55         if(height[i]>=limit) {
56             if(++cnt>=k) return true;
57         }
58         else cnt=1;
59     }
60     return false;
61 }
62
63 int main() {
64     scanf("%d%d",&n,&k);
65     int up=0;
66     FOR(i,0,n-1) {
67          scanf("%d",&s[i]);
68          s[i]++;
69          up=max(up,s[i]);
70     }
71     s[n]=0;
72     build_sa(up+1,n);
73     getHeight(n);
74     int L=0,R=n;
75     while(L<R) {
76         int M=L+(R-L+1)/2;
77         if(can(M)) L=M;
78         else R=M-1;
79     }
80     printf("%d\n",L);
81     return 0;
82 }
时间: 2024-11-06 06:30:53

poj3261 Milk Patterns(后缀数组)的相关文章

[POJ3261] Milk Patterns (后缀数组+二分)

题目概述: 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 t

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 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 后缀数组

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

POJ 3261 Milk Patterns ( 后缀数组 &amp;&amp; 出现k次最长可重叠子串长度 )

题意 : 给出一个长度为 N 的序列,再给出一个 K 要求求出出现了至少 K 次的最长可重叠子串的长度 分析 : 后缀数组套路题,思路是二分长度再对于每一个长度进行判断,判断过程就是对于 Height 数组进行限定长度的分组策略,如果有哪一组的个数 ≥  k 则说明可行! 分组要考虑到一个事实,对于每一个后缀,与其相匹配能够产生最长的LCP长度的串肯定是在后缀数组中排名与其相邻. 一开始对分组的理解有误,所以想了一个错误做法 ==> 遍历一下 Height 将值 ≥ (当前二分长度) 的做一次贡

[Poj3261] [Bzoj1717] [后缀数组论文例题,USACO 2006 December Gold] Milk Patterns [后缀数组可重叠的k次最长重复子串]

和上一题(POJ1743,上一篇博客)相似,只是二分的判断条件是:是否存在一段后缀的个数不小于k 1 #include <iostream> 2 #include <algorithm> 3 #include <cstdio> 4 #include <cstdlib> 5 #include <cstring> 6 #include <cmath> 7 #include <ctime> 8 #include <map&

【POJ3261】Milk Patterns 后缀数组

水题不好意思说题解. 说说题意吧: 给一个字符串(数字串),然后求最长k次重复子串. 即某串在字符串中重复了至少k次,求这种串的最长长度. 代码: #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #define N 21000 using namespace std; struct LSH { int x,id; bool operator <

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

poj3261Milk Patterns 后缀数组

题目地址: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 can't predict the quality of milk from one day to