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

寻找重复奶质模式
针对奶质变化规律,本篇介绍了一种算法来找出奶质数据中最长的重复子序列,该子序列至少重复K次,包括可能的重叠情况。通过二分查找和后缀数组等技术手段实现。

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

Sample Input

8 2
1
2
3
2
3
2
3
1

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);
    }
}





评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值