Milk Patterns poj3261(后缀数组)

奶农约翰发现奶制品质量存在规律性波动,通过发明一种复杂分类方案,记录了奶牛每天的奶制品质量,并找到了至少出现两次的最长重复模式。本文详细介绍了研究过程和解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Milk Patterns
Time Limit: 5000MS Memory Limit: 65536K
Total Submissions: 9274 Accepted: 4173
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 ≤ 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

 1 #include <iostream>
 2 #include <stdio.h>
 3 #include <string.h>
 4 using namespace std;
 5 #define N 20001
 6 #define M 1000002
 7 int a[N],c[N],d[N],e[N],sa[N],height[N],n,b[M],m,k;
 8 int cmp(int *r,int a,int b,int l)
 9 {
10     return r[a]==r[b]&&r[a+l]==r[b+l];
11 }
12 void da()
13 {
14     int i,j,p,*x=c,*y=d,*t;
15     memset(b,0,sizeof(b));
16     for(i=0; i<n; i++)b[x[i]=a[i]]++;
17     for(i=1; i<m; i++)b[i]+=b[i-1];
18     for(i=n-1; i>=0; i--)sa[--b[x[i]]]=i;
19     for(j=1,p=1; p<n; j*=2,m=p)
20     {
21         for(p=0,i=n-j; i<n; i++)y[p++]=i;
22         for(i=0; i<n; i++)if(sa[i]>=j)y[p++]=sa[i]-j;
23         for(i=0; i<n; i++)e[i]=x[y[i]];
24         for(i=0; i<m; i++)b[i]=0;
25         for(i=0; i<n; i++)b[e[i]]++;
26         for(i=1; i<m; i++)b[i]+=b[i-1];
27         for(i=n-1; i>=0; i--)sa[--b[e[i]]]=y[i];
28         for(t=x,x=y,y=t,p=1,x[sa[0]]=0,i=1; i<n; i++)
29             x[sa[i]]=cmp(y,sa[i-1],sa[i],j)?p-1:p++;
30     }
31 }
32 void callheight()
33 {
34     int i,j,k=0;
35     b[0]=0;
36     for(i=1; i<n; i++)b[sa[i]]=i;
37     for(i=0; i<n-1; height[b[i++]]=k)
38         for(k?k--:0,j=sa[b[i]-1]; a[i+k]==a[j+k]; k++);
39 }
40 bool check(int mid)
41 {
42     int sum,i=1;
43     while(1)
44     {
45         while(i<n&&height[i]<mid)i++;
46         if(i>=n)return 0;
47         sum=1;
48         while(i<n&&height[i]>=mid)
49         {
50             sum++;
51             i++;
52         }
53         if(sum>=k)return 1;
54     }
55 }
56 int main()
57 {
58     int i,l,r;
59     scanf("%d%d",&n,&k);
60     for(i=0; i<n; i++)scanf("%d",&a[i]),a[i]++,m=m<a[i]?a[i]:m;
61     m=m<n?n:m;
62     m++;
63     a[n++]=0;
64     da();
65     callheight();
66     l=0,r=n;
67     while(l<=r)
68     {
69         int mid=(l+r)>>1;
70         if(check(mid))
71             l=mid+1;
72         else r=mid-1;
73     }
74     printf("%d\n",r);
75 }
View Code

 

转载于:https://www.cnblogs.com/ERKE/p/3593785.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值