Milk Patterns
Time Limit: 5000MS
Memory Limit: 65536K
Total Submissions: 7497
Accepted: 3424
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 timesSample Input
8 2
1
2
3
2
3
2
3
1
Sample Output
4Source
USACO 2006 December Gold
解析:
题目是给你一个字符串,叫你求至少重复k次的子串的最长长度。。。
后缀数组入门题。。。
维护好height数组后,每次二分子串的长度,利用height数组的性质按rank排名依次判断是否存在重复次数>k的子串
代码:
#include<cstdio>
#include<algorithm>
using namespace std;
#define maxn 20001
#define maxm 1000010
int wa[maxn],wb[maxn],wv[maxn],ws[maxm];
int cmp(int *r,int a,int b,int l)
{
return (r[a]==r[b] && r[a+l]==r[b+l]);
}
void da(int *s,int *sa,int n,int m)
{
int i,j,p,*x=wa,*y=wb,*t;
for(int i=0;i<m;i++)ws[i]=0; //基数排序
for(int i=0;i<n;i++)ws[x[i]=s[i]]++; //
for(int i=1;i<m;i++)ws[i]+=ws[i-1]; // (长度为一)
for(int i=n-1;i>=0;i--)sa[--ws[x[i]]]=i; //
for(int j=1,p=1;p<n;j*=2,m=p)
{
for(p=0,i=n-j;i<n;i++)y[p++]=i;
for(i=0;i<n;i++)if(sa[i]>=j)y[p++]=sa[i]-j;
for(i=0;i<n;i++)wv[i]=x[y[i]];
for(i=0;i<m;i++)ws[i]=0; //基数排序
for(i=0;i<n;i++)ws[wv[i]]++; //
for(i=1;i<m;i++)ws[i]+=ws[i-1]; //
for(i=n-1;i>=0;i--)sa[--ws[wv[i]]]=y[i]; //
for(t=x,x=y,y=t,p=1,x[sa[0]]=0,i=1;i<n;i++)
x[sa[i]]=cmp(y,sa[i-1],sa[i],j)?p-1:p++;
}
return ;
}
int rank[maxn],height[maxn];
void calheight(int *s,int *sa,int n)
{
int i,j,k=0;
for(i=1;i<=n;i++)rank[sa[i]]=i;
for(i=0;i<n;height[rank[i++]]=k)
for(k?k--:0,j=sa[rank[i]-1];s[i+k]==s[j+k];k++);
return ;
}
bool check(int n,int k,int mid)
{
int s=1;
for(int i=1;i<=n;i++)
{
if(height[i]>=mid)
{
s++;
if(s>=k)return 1;
}else s=1;
}
return 0;
}
int s[maxn],sa[maxn];
void read()
{
freopen("da.in","r",stdin);
freopen("da.out","w",stdout);
int n,k;
scanf("%d%d",&n,&k);
for(int i=0;i<n;i++)
{
scanf("%d",&s[i]); s[i]++;
}
s[n]=0;
da(s,sa,n+1,maxm);
calheight(s,sa,n);
int l=1,r=n;
while(l<=r)
{
int mid=(l+r)/2;
if(check(n,k,mid))l=mid+1;
else r=mid-1;
}
printf("%d\n",r);
//for(int i=0;i<=n;i++)printf("%d\n",sa[i]);
}
int main()
{
read();
return 0;
}