首页 > 编程语言 > 详细

后缀数组练习题

时间:2018-02-10 13:34:29      阅读:379      评论:0      收藏:0      [点我收藏+]
Milk Patterns
Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 17079   Accepted: 7553
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 ≤ KN) 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

求最长可重复至少出现k次的子串长度

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=1e6+88;
const int M=2e4+88;
int wa[N],wv[N],ws[N];
int sa[M],rank[M],wb[M],height[M],num[M];
bool cmp(int *r,int a,int b,int l){
    return r[a]==r[b]&&r[a+l]==r[b+l];
}
void da(int *r,int n,int m){
    int *x=wa,*y=wb;
    for(int i=0;i<m;++i) ws[i]=0;
    for(int i=0;i<n;++i) ++ws[x[i]=r[i]];
    for(int i=1;i<m;++i) ws[i]+=ws[i-1];
    for(int i=0;i<n;++i) sa[--ws[x[i]]]=i;
    int p=1;
    for(int j=1;p<n;j<<=1,m=p) {
        p=0;
        for(int i=n-j;i<n;++i) y[p++]=i;
        for(int i=0;i<n;++i) if(sa[i]>=j) y[p++]=sa[i]-j;
        for(int i=0;i<n;++i) wv[i]=x[y[i]];
        for(int i=0;i<m;++i) ws[i]=0;
        for(int i=0;i<n;++i) ++ws[wv[i]];
        for(int i=1;i<m;++i) ws[i]+=ws[i-1];
        for(int i=n-1;i>=0;--i) sa[--ws[wv[i]]]=y[i];
        swap(x,y),x[sa[0]]=0,p=1;
        for(int i=1;i<n;++i) x[sa[i]]=cmp(y,sa[i-1],sa[i],j)?p-1:p++;
    }
    for(int i=1;i<n;++i) rank[sa[i]]=i;
    int k=0;
    for(int i=0;i<n-1;height[rank[i++]]=k){
        if(k) --k;
        for(int j=sa[rank[i]-1];r[i+k]==r[j+k];++k);
    }
}
bool Ju(int k,int lim,int ci){
    int cnt=0;
    for(int i=2;i<=lim;++i) 
    {
    if(height[i]>=k) ++cnt;
    else cnt=0;
    if(cnt>=ci) return true;
    }
    return false;
}
int main(){
    int ans,l,r,n,k,x,maxx=0;
    scanf("%d%d",&n,&k);
    for(int i=0;i<n;++i) {
        scanf("%d",num+i);
        ++num[i];
        maxx=max(maxx,num[i]);
    }
    num[n]=0;
    da(num,n+1,maxx+1);
    l=1,r=n;
    while(l<=r) {
        int mid=(l+r)>>1;
        if(Ju(mid,n,k-1)) ans=mid,l=mid+1;
        else r=mid-1;
    }
    printf("%d\n",ans);
}

 

后缀数组练习题

原文:https://www.cnblogs.com/mfys/p/8438460.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!