POJ3261 Milk Patterns(后缀数组,二分)

来源:互联网 发布:java效率最高的排序 编辑:程序博客网 时间:2024/06/05 10:19

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


求重复次数不小于k的可重叠的最长重复子串。

二分答案后根据高度数组分组,只要存在一个分组内的子串数量大于等于k即可。


#include<cstdio>#include<cstring>#include <string>#include <iostream>#include <algorithm>using namespace std;const int MAXN=200+10;const int INF=1e9+7;int n,m,k;int rnk[MAXN+1];int tmp[MAXN+1];//比较(rnk[i],rnk[i+k])和(rnk[j],rnk[j+k])bool compare_sa(int i,int j){    if(rnk[i]!=rnk[j])        return rnk[i]<rnk[j];    else{        int ri=i+k<=n?rnk[i+k]:-1;        int rj=j+k<=n?rnk[j+k]:-1;        return ri<rj;    }}//rank用来记录字符串的排序,sa用来记录开头字符的位置,S用来记录字符串//第一个通常是空字符串void construct_sa(int *S,int *sa){    //初始长度为1,rank直接取字符的编码.    for(int i=0;i<=n;i++){        sa[i]=i;        rnk[i]=i<n?S[i]:-1;    }        //利用对长度为k的排序的结果对长度为2k的排序    for(k=1;k<=n;k*=2){        sort(sa,sa+n+1,compare_sa);                //先在tmp中临时储存新计算的rank,再转存回rank中        tmp[sa[0]]=0;        for(int i=1;i<=n;i++){            tmp[sa[i]]=tmp[sa[i-1]]+(compare_sa(sa[i-1],sa[i])?1:0);        }        for(int i=0;i<=n;i++){            rnk[i]=tmp[i];        }    }}//高度数组lcp的计算void construct_lcp(int *S,int *sa,int *lcp){    for(int i=0;i<=n;i++) rnk[sa[i]]=i;    int h=0;    lcp[sa[0]]=0;    for(int i=0;i<n;i++){        //计算字符串中从位置i开始的后缀及其在后缀数组中的前一个后缀的lcp        int j=sa[rnk[i]-1];                //将h先减去首字母的1长度,在保持前缀相同的前提下不断地增加        if(h>0) h--;        for(;j+h<n&&i+h<n;h++){            if(S[j+h]!=S[i+h]) break;        }        lcp[rnk[i]-1]=h;    }}int sa[MAXN],lcp[MAXN];int a[MAXN];void init(){    memset(sa,0,sizeof sa);    memset(lcp,0,sizeof lcp);    memset(rnk,0,sizeof rnk);    memset(tmp,0,sizeof tmp);}bool C(int x){    int pre=1;    for(int i=1;i<=n;i++){        if(lcp[i]<x||i==n){            if(i-pre+1>=m){                return true;            }            pre=i+1;        }    }    return false;}int main(){    while(scanf("%d%d",&n,&m)!=EOF){        for(int i=0;i<n;i++){            scanf("%d",a+i);        }        init();        construct_sa(a, sa);        construct_lcp(a, sa, lcp);        int l=0,r=n+1;        while(r>l+1){            int m=(l+r)>>1;            if(C(m)){                l=m;            }else{                r=m;            }        }        printf("%d\n",l);    }}