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 4 Source |
来自论文后缀数组 罗穗骞:
给定一个字符串,求不相同的子串的个数。
算法分析:
每个子串一定是某个后缀的前缀, 那么原问题等价于求所有后缀之间的不相
同的前缀的个数。如果所有的后缀按照 suffix(sa[1]), suffix(sa[2]),suffix(sa[3]), ...... ,suffix(sa[n])的顺序计算,不难发现,对于每一次新加进来的后缀 suffix(sa[k]),它将产生 n-sa[k]+1 个新的前缀。但是其中有height[k]个是和前面的字符串的前缀是相同的。 所以 suffix(sa[k])将“贡献”出 n-sa[k]+1- height[k]个不同的子串。累加后便是原问题的答案。这个做法的时间复杂度为 O(n)。
#include <iostream>
#include <string.h>
#include <stdio.h>
#include <algorithm>
#include <queue>
#include <math.h>
#include <vector>
using namespace std;
const int maxn = 20100;
int w[maxn],sa[maxn],rk[maxn],height[maxn],res[maxn],cnt[maxn];
void getSa(int len,int up)
{
int *k=rk,*r=res,*id=height;
for(int i=0;i<up;i++) cnt[i]=0;
for(int i=0;i<len;i++) cnt[k[i]=w[i]]++;
for(int i=0;i<up;i++) cnt[i+1]+=cnt[i];
for(int i=len-1;i>=0;i--){
sa[--cnt[k[i]]]=i;
}
int d=1,p=0;
while(p<len){
for(int i=len-d;i<len;i++) id[p++]=i;
for(int i=0;i<len;i++) if(sa[i]>=d) id[p++]=sa[i]-d;
for(int i=0;i<len;i++) r[i]=k[id[i]];
for(int i=0;i<up;i++) cnt[i]=0;
for(int i=0;i<len;i++) cnt[r[i]]++;
for(int i=0;i<up;i++) cnt[i+1]+=cnt[i];
for(int i=len-1;i>=0;i--){
sa[--cnt[r[i]]]=id[i];
}
p=0;
swap(r,k);
k[sa[0]]=p++;
for(int i=0;i<len-1;i++){
if(sa[i]+d<len&&sa[i+1]+d<len&&r[sa[i]]==r[sa[i+1]]&&r[sa[i+1]+d]==r[sa[i]+d])
k[sa[i+1]]=p-1;
else
k[sa[i+1]]=p++;
}
if(p>=len) return;
d*=2,up=p,p=0;
}
}
void getHeight(int len)
{
for(int i=0;i<len;i++) rk[sa[i]]=i;
int p=0;
for(int i=0;i<len-1;i++){
int j=sa[rk[i]-1];
while(i+p<len&&j+p<len&&w[i+p]==w[j+p])
p++;
height[rk[i]]=p;
p=max(0,p-1);
}
}
void getSuffix(int len,int up)
{
w[len++]=0;
getSa(len,up+1);
getHeight(len);
}
int n;
int check(int k,int cnt)
{
int num=1;
for(int i=2;i<=n;i++){
if(height[i]<k) num=1;
else{
num++;
if(num>=cnt) return 1;
}
}
return 0;
}
int main()
{
int k;
scanf("%d %d",&n,&k);
int up=0;
for(int i=0;i<n;i++){
scanf("%d",&w[i]);
w[i]++;
if(up<w[i]) up=w[i];
}
getSuffix(n,up);
int l=1,r=maxn;
int mid;
int ans=0;
while(l<=r){
mid=(l+r)>>1;
if(check(mid,k)){
ans=mid;
l=mid+1;
}
else
r=mid-1;
}
printf("%d\n",ans);
return 0;
}