POJ 3261 Milk Patterns 后缀数组+二分

来源:互联网 发布:pop3 smtp 端口 编辑:程序博客网 时间:2024/05/03 20:00

Milk Patterns
Time Limit: 5000MS Memory Limit: 65536KTotal Submissions: 16231 Accepted: 7159Case 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 212323231

Sample Output

4

Source

USACO 2006 December Gold


求在一个序列当中重复出现至少k次的最长子序列的长度。


把数字当做字符串,建立后缀数组。

之后二分答案len。同样地把后缀按照长度len分为若干组,若某组的字符串个数大于等于k则存在满足条件的长度为len的字符串。


入门级别的题目。


#include <cstdio>#include <string.h>#include <string> #include <map>#include <queue>#include <vector>#include <set>#include <algorithm>#include <math.h>#include <cmath>#include <stack>#define mem0(a) memset(a,0,sizeof(a))#define meminf(a) memset(a,0x3f,sizeof(a))using namespace std;typedef long long ll;typedef long double ld;typedef double db;const int maxn=20005,inf=0x3f3f3f3f;  const ll llinf=0x3f3f3f3f3f3f3f3f;   const ld pi=acos(-1.0L);int wa[maxn],wb[maxn],wv[maxn],ws[maxn],sa[maxn],ranki[maxn],height[maxn];int s[maxn];int cmp(int *r,int a,int b,int l) {return r[a]==r[b]&&r[a+l]==r[b+l];}void build(int *r,int *sa,int n,int m) {int i,j,k,p,*x=wa,*y=wb,*t;for (i=0;i<m;i++) ws[i]=0;for (i=0;i<n;i++) ws[x[i]=r[i]]++;for (i=0;i<m;i++) ws[i]+=ws[i-1];for (i=n-1;i>=0;i--)     sa[--ws[x[i]]]=i;for (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];t=x;x=y;y=t;p=1;x[sa[0]]=0;for (i=1;i<n;i++)     x[sa[i]]=cmp(y,sa[i-1],sa[i],j)?p-1:p++;}for (i=1;i<n;i++) ranki[sa[i]]=i;k=0; for (i=0;i<n-1;height[ranki[i++]]=k) {if (k) k--;for (j=sa[ranki[i]-1];r[i+k]==r[j+k];k++);}}bool check(int l,int n,int k) {int i,mi,ma,cnt=1;for (i=2;i<=n;i++) {if (height[i]>=l) cnt++; else {if (cnt>=k) return true;cnt=1;}}if (cnt>=k) return true;return false;}int main() {int n,k;while (scanf("%d%d",&n,&k)!=EOF) {int i,m=-1;for (i=0;i<n;i++) {scanf("%d",&s[i]);m=max(m,s[i]); }s[n]=0;build(s,sa,n+1,m+1);int l=1,r=n,mid,ans=-1;    while (l<=r) {    mid=(l+r)/2;    if (check(mid,n,k)) ans=mid,l=mid+1; else r=mid-1;    }    printf("%d\n",ans); }return 0;}