[USACO06DEC] 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 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

就当是后缀数组模板题好了,真心写的心酸

二分答案,看在RMQ中所有长度为k的区间是否满足LCP大于等于mid(即二分的长度),有一个区间满足则该长度可行,然而继续二分即可。

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 typedef long long ll;
 4 template <typename T> void aout(T *a,int i,int j){
 5     for(int k=i,k_end=j;k<k_end;++k)
 6         cout<<a[k]<<" ";puts("");
 7 }
 8 const int N=100010;
 9 int s[N];
10 int sa[N],t[N],t2[N],c[N],n,k;
11 int Rank[N],height[N],dmin[N][20];
12 void build_sa(int m){
13     int i,*x=t,*y=t2;
14     for(i=0;i<m;++i)c[i]=0;
15     for(i=0;i<n;++i)++c[x[i]=s[i]];
16     for(i=1;i<m;++i)c[i]+=c[i-1];
17     for(i=n-1;i>=0;--i)sa[--c[x[i]]]=i;
18     for(int k=1;k<=n;k<<=1){
19         int p=0;
20         for(i=n-k;i<n;++i)y[p++]=i;
21         for(i=0;i<n;++i)if(sa[i]>=k)y[p++]=sa[i]-k;
22         for(i=0;i<m;++i)c[i]=0;
23         for(i=0;i<n;++i)++c[x[y[i]]];
24         for(i=1;i<m;++i)c[i]+=c[i-1];
25         for(i=n-1;i>=0;--i)sa[--c[x[y[i]]]]=y[i];
26         swap(x,y);
27         p=1;x[sa[0]]=0;y[n]=-1;
28         for(i=1;i<n;++i)
29             x[sa[i]]=(y[sa[i-1]]==y[sa[i]]&&y[sa[i-1]+k]==y[sa[i]+k])?p-1:p++;
30         if(p>=n)break;
31         m=p;
32     }
33 }
34 void build_height(){
35     int i,j,k=0;
36     for(i=0;i<n;i++)Rank[sa[i]]=i;
37     for(i=0;i<n;i++) {
38         if(k)k--;
39         j=sa[Rank[i]-1];
40         while(s[i+k]==s[j+k])k++;
41         height[Rank[i]]=k;
42     }
43 }
44 void initMin(){
45     for(int i=1;i<=n;++i)dmin[i][0]=height[i];
46         for(int j=1;(1<<j)<=n;++j)
47             for(int i=1;i+(1<<j)-1<=n;++i)
48                 dmin[i][j]=min(dmin[i][j-1],dmin[i+(1<<(j-1))][j-1]);
49 }
50 int RMQ(int L,int R){
51     int k=0;
52     while((1<<(k+1))<=R-L+1)k++;
53     return min(dmin[L][k],dmin[R-(1<<k)+1][k]);
54 }
55 bool check(int len){
56     int l=0,r=l+k-2;
57     while(r<=n-1){
58         int cnt=RMQ(l,r);
59         if(cnt>=len)return true;
60         ++l,++r;
61     }
62     return false;
63 }
64 int a[N],num[N];
65 inline bool cmp(int __x,int __y){ return a[__x]<a[__y]; }
66 int main(){
67     scanf("%d%d",&n,&k);
68     for(int i=0;i<n;++i)scanf("%d",&a[i]),num[i]=i;
69     sort(num,num+n,cmp);
70     s[num[0]]=1;
71     int cnt=1;
72     for(int i=0;i<n-1;++i)
73         if(a[num[i]]!=a[num[i+1]])s[num[i+1]]=++cnt;
74         else s[num[i+1]]=cnt;
75     build_sa(cnt+1);
76     build_height();
77     initMin();
78     int l=1,r=n,ans=0;
79     while(l<=r){
80         int mid=l+r>>1;
81         if(check(mid))ans=mid,l=mid+1;
82         else r=mid-1;
83     }
84     printf("%d\n",ans);
85 }

据说hash可以过...过两天试一试

时间: 2024-08-30 09:42:04

[USACO06DEC] Milk Patterns的相关文章

P2852 [USACO06DEC]Milk Patterns

题意 显然如果有一个子串出现过\(k\)次,那么它必定是一个至少长为k的后缀序的\(LCP\),求出所有相邻的长为\(k-1\)的\(height\)数组的最小值,在其中取最大值即可 code: #include<bits/stdc++.h> using namespace std; const int maxn=20010; const int maxm=1000010; int n,m,num,ans; int a[maxn],sa[maxn],rk[maxn],oldrk[maxn],i

[BZOJ1717][Usaco2006 Dec]Milk Patterns 产奶的模式

1717: [Usaco2006 Dec]Milk Patterns 产奶的模式 Time Limit: 5 Sec  Memory Limit: 64 MB Submit: 1297  Solved: 705 [Submit][Status][Discuss] Description 农夫John发现他的奶牛产奶的质量一直在变动.经过细致的调查,他发现:虽然他不能预见明天产奶的质量,但连续的若干天的质量有很多重叠.我们称之为一个"模式". John的牛奶按质量可以被赋予一个0到100

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

Milk Patterns Time Limit: 5000ms Memory Limit: 65536KB This problem will be judged on PKU. Original ID: 326164-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 t

BZOJ 1717: [Usaco2006 Dec]Milk Patterns 产奶的模式 [后缀数组]

1717: [Usaco2006 Dec]Milk Patterns 产奶的模式 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 1017  Solved: 561[Submit][Status][Discuss] Description 农夫John发现他的奶牛产奶的质量一直在变动.经过细致的调查,他发现:虽然他不能预见明天产奶的质量,但连续的若干天的质量有很多重叠.我们称之为一个“模式”. John的牛奶按质量可以被赋予一个0到1000000之间的

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 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,

Milk Patterns

poj3261:http://poj.org/problem?id=3261 题意: 题解: 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 using namespace std; 5 const int maxn=20010; 6 char str[maxn]; 7 int wa[maxn],wb[maxn],wv[maxn],wn[maxn],a[maxn],sa[maxn]; 8 int

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,