zoukankan      html  css  js  c++  java
  • poj3261 后缀数组求重复k次可重叠的子串的最长长度

    Milk Patterns
    Time Limit: 5000MS   Memory Limit: 65536K
    Total Submissions: 13669   Accepted: 6041
    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
     
    题意:
    n个数,求重复k次的,可重叠子串的最长长度。
    思路:
    求出height数组,然后二分答案,再判断连续的height[i] >= mid的个数是否大于k。
     
    /*
     * Author:  sweat123
     * Created Time:  2016/6/28 16:24:57
     * File Name: main.cpp
     */
    #include<set>
    #include<map>
    #include<queue>
    #include<stack>
    #include<cmath>
    #include<string>
    #include<vector>
    #include<cstdio>
    #include<time.h>
    #include<cstring>
    #include<iostream>
    #include<algorithm>
    #define INF 1<<30
    #define MOD 1000000007
    #define ll long long
    #define lson l,m,rt<<1
    #define rson m+1,r,rt<<1|1
    #define pi acos(-1.0)
    using namespace std;
    const int MAXN = 20010;
    int wa[MAXN],wb[MAXN],wc[1000010],n,height[MAXN],sa[MAXN],k,r[MAXN],Rank[MAXN];
    void da(int *r,int *sa,int n,int m){
        int *x = wa,*y = wb;
        for(int i = 0; i < m; i++)wc[i] = 0;
        for(int i = 0; i < n; i++)wc[x[i] = r[i]] ++;
        for(int i = 0; i < m; i++)wc[i] += wc[i-1];
        for(int i = n - 1; i >= 0; i--)sa[--wc[x[i]]] = i;
        for(int p = 1,k = 1; p < n; m = p,k <<= 1){
            p = 0;
            for(int i = n - k; i < n; i++)y[p++] = i;
            for(int i = 0; i < n; i++)if(sa[i] >= k)y[p++] = sa[i] - k;
            for(int i = 0; i < m; i++)wc[i] = 0;
            for(int i = 0; i < n; i++)wc[x[y[i]]] ++;
            for(int i = 0; i < m; i++)wc[i] += wc[i-1];
            for(int i = n - 1; i >= 0; i--)sa[--wc[x[y[i]]]] = y[i];
            swap(x,y);
            p = 1;
            x[sa[0]] = 0;
            for(int i = 1; i < n; i++)
                x[sa[i]] = (y[sa[i-1]] == y[sa[i]] && y[sa[i-1]+k] == y[sa[i]+k])?p-1:p++;
        }   
    }
    void calheight(int *r,int *sa,int n){
        for(int i = 1; i <= n; i++)Rank[sa[i]] = i;
        int k = 0,j;
        for(int i = 0; i < n; height[Rank[i++]] = k){
            for(k?k--:0,j = sa[Rank[i]-1]; r[j+k] == r[i+k]; k++);
        }   
    }
    int ok(int m,int n){
        int num = 0;
        for(int i = 1; i <= n; i++){
            if(height[i] >= m){
                num ++;   
                if(num + 1 >= k)return 1;
            } else{
                num = 0;   
            }
        }
        return 0;
    }
    void solve(){
        int l,r,m,ans = 0;
        l = 0,r = n;
        while(l <= r){
            m = (l + r) >> 1;
            if(ok(m,n)){
                ans = max(m,ans);
                l = m + 1;
            } else {
                r = m - 1;   
            }
        }
        printf("%d
    ",ans);
    }
    int main(){
        while(~scanf("%d%d",&n,&k)){
            for(int i = 0; i < n; i++){
                scanf("%d",&r[i]);
            }
            r[n] = 0;
            da(r,sa,n+1,1000002);
            calheight(r,sa,n);
            solve();
        }
        return 0;
    }
  • 相关阅读:
    Tomcat部署web项目,虚拟目录,上下文(Context),WEB-INF,web.xml,servlet,404
    Android异常:唤醒锁未授权。(Caused by: java.lang.SecurityException: Neither user 10044 nor current process has android.permission.WAKE_LOCK.)
    .hiverc
    Hive安装
    搭建Kafka开发环境
    java实现Kafka的消费者示例
    java实现Kafka生产者示例
    Kafka集群部署
    kafka介绍
    Pig UDF 用户自定义函数
  • 原文地址:https://www.cnblogs.com/sweat123/p/4795267.html
Copyright © 2011-2022 走看看