zoukankan      html  css  js  c++  java
  • poj1743 后缀数组求不可重叠的重复出现的子串最长长度

    Musical Theme
    Time Limit: 1000MS   Memory Limit: 30000K
    Total Submissions: 25348   Accepted: 8546

    Description

    A musical melody is represented as a sequence of N (1<=N<=20000)notes that are integers in the range 1..88, each representing a key on the piano. It is unfortunate but true that this representation of melodies ignores the notion of musical timing; but, this programming task is about notes and not timings. 
    Many composers structure their music around a repeating &qout;theme&qout;, which, being a subsequence of an entire melody, is a sequence of integers in our representation. A subsequence of a melody is a theme if it: 
    • is at least five notes long 
    • appears (potentially transposed -- see below) again somewhere else in the piece of music 
    • is disjoint from (i.e., non-overlapping with) at least one of its other appearance(s)

    Transposed means that a constant positive or negative value is added to every note value in the theme subsequence. 
    Given a melody, compute the length (number of notes) of the longest theme. 
    One second time limit for this problem's solutions! 

    Input

    The input contains several test cases. The first line of each test case contains the integer N. The following n integers represent the sequence of notes. 
    The last test case is followed by one zero. 

    Output

    For each test case, the output file should contain a single line with a single integer that represents the length of the longest theme. If there are no themes, output 0.

    Sample Input

    30
    25 27 30 34 39 45 52 60 69 79 69 60 52 45 39 34 30 26 22 18
    82 78 74 70 66 67 64 60 65 80
    0
    

    Sample Output

    5
     
    题意: 求长度不小于5的,不重叠重复出现的子串。(重复出现不一定值一样,但是可以一个子串加减一些值后相同)。
     
    思路:
    由于2个串不一定相同,先求出差得数组,因为差一定是一样的。
    然后求height[ ],由于答案不能直接确定,我们可以二分答案。
    对于每次二分的答案m,我们可以在height数组中查询,如果height[i] >= m,并且该组内的左端点和
    右端点的差值 >= m,说明满足要求。
     
     
    /*
     * Author:  sweat123
     * Created Time:  2016/6/28 13:57:31
     * 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 = 40010;
    int wa[MAXN],wb[MAXN],wc[MAXN],n,r[MAXN],Rank[MAXN],sa[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 k = 1,p = 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++;
        }
    }
    int height[MAXN];
    void calheight(int *r,int *sa,int n){
        int k,j;
        k = 0;
        for(int i = 1; i <= n; i++)Rank[sa[i]] = i;
        for(int i = 0; i < n; height[Rank[i++]] = k)
            for(k?k--:0,j = sa[Rank[i]-1]; r[i+k] == r[j+k]; k++);
    }
    int ok(int m,int n){
        int x,y;
        x = INF;
        y = -INF;
        for(int i = 1; i <= n; i++){
            if(height[i] >= m){
                x = min(x,sa[i]);
                y = max(y,sa[i]);
                if(y - x >= m)return 1;      
            } else{
                x = sa[i];
                y = sa[i]; 
            }  
        }   
        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 = m;
                l = m + 1; 
             } else{
                r = m - 1;   
             }
        }
        if(ans < 4)printf("0
    ");
        else printf("%d
    ",ans + 1);
    }
    int main(){
        while(~scanf("%d",&n)){
            if(!n)break;
            for(int i = 0; i < n; i++){
                scanf("%d",&r[i]); 
            }  
            for(int i = 0; i < n - 1; i++){
                r[i] = r[i+1] - r[i];
            }
            n -= 1;
            int maxval,minval;
            maxval = -INF;
            minval = INF;
            for(int i = 0; i < n; i++){
                maxval = max(maxval,r[i]);
                minval = min(minval,r[i]);
            }
            if(minval <= 0){
                minval *= -1;
                minval += 1;
                for(int i = 0; i < n; i++){
                      r[i] += minval;
                }
                maxval += minval;
            }
            r[n] = 0;
            da(r,sa,n+1,maxval+1);
            calheight(r,sa,n);
            solve();
        }
        return 0;
    }
  • 相关阅读:
    Linux切换到su超级用户
    使用Qt ARM交叉编译提示“此qt版本具有一个未知的工具链”
    使用qwt提示error LNK2001 问题的解决方法
    JavaScript中的 in
    JavaScript中的this
    autocad2010指令全集
    Proj.NETProj.NET简介和示例(转载)
    高性能地图优化策略(转载)
    Emgu.CV,OpenCV for C#
    C# 转载网络爬虫
  • 原文地址:https://www.cnblogs.com/sweat123/p/5623856.html
Copyright © 2011-2022 走看看