zoukankan      html  css  js  c++  java
  • 后缀数组---Musical Theme

    POJ   1743

    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

    Hint

    Use scanf instead of cin to reduce the read time.

    题意:有N(1 <= N <=20000)个音符的序列来表示一首乐曲,每个音符都是1..88范围内的整数,现在要找一个重复的主题。“主题”是整个音符序列的一个子串,它需要满足如下条件:

        1.长度至少为5个音符。

        2.在乐曲中重复出现。(可能经过转调,“转调”的意思是主题序列中每个音符都被加上或减去了同一个整数值)

        3.重复出现的同一主题不能有公共部分。

    思路:后缀数组。求出任意相邻音符的差值,最后一个填充0,然后把问题转化为 不可重叠最长重复子串,用后缀数组来做。先二分答案,把题目变成判定性问题:判断是否存在两个长度为k的子串是相同的,且不重叠。解决这个问题的关键还是利用 height数组。把排序后的后缀分成若干组,其中每组的后缀之间的height值都不小于k。
    #include <iostream>
    #include <algorithm>
    #include <cstdio>
    #include <cstring>
    #define rep(i,n) for(int i = 0;i < n; i++)
    using namespace std;
    const int size=20205,INF=1<<30;
    int rk[size],sa[size],height[size],w[size],wa[size],res[size];
    int N;
    void getSa (int len,int up) {
        int *k = rk,*id = height,*r = res, *cnt = wa;
        rep(i,up) cnt[i] = 0;
        rep(i,len) cnt[k[i] = w[i]]++;
        rep(i,up) cnt[i+1] += cnt[i];
        for(int i = len - 1; i >= 0; i--) {
            sa[--cnt[k[i]]] = i;
        }
        int d = 1,p = 0;
        while(p < len){
            for(int i = len - d; i < len; i++) id[p++] = i;
            rep(i,len)  if(sa[i] >= d) id[p++] = sa[i] - d;
            rep(i,len) r[i] = k[id[i]];
            rep(i,up) cnt[i] = 0;
            rep(i,len) cnt[r[i]]++;
            rep(i,up) cnt[i+1] += cnt[i];
            for(int i = len - 1; i >= 0; i--) {
                sa[--cnt[r[i]]] = id[i];
            }
            swap(k,r);
            p = 0;
            k[sa[0]] = p++;
            rep(i,len-1) {
                if(sa[i]+d < len && sa[i+1]+d <len &&r[sa[i]] == r[sa[i+1]]&& r[sa[i]+d] == r[sa[i+1]+d])
                    k[sa[i+1]] = p - 1;
                else k[sa[i+1]] = p++;
            }
            if(p >= len) return ;
            d *= 2,up = p, p = 0;
        }
    }
    
    void getHeight(int len) {
        rep(i,len) rk[sa[i]] = i;
        height[0] =  0;
        for(int i = 0,p = 0; i < len - 1; i++) {
            int j = sa[rk[i]-1];
            while(i+p < len&& j+p < len&& w[i+p] == w[j+p]) {
                p++;
            }
            height[rk[i]] = p;
            p = max(0,p - 1);
        }
    }
    
    int getSuffix(int s[]) {
        int len =N,up = 0;
        for(int i = 0; i < len; i++) {
            w[i] = s[i];
            up = max(up,w[i]);
        }
        w[len++] = 0;
        getSa(len,up+1);
        getHeight(len);
        return len;
    }
    
    bool valid(int len)
    {
        int i = 2, ma, mi;
        while(1)
        {
            while(i <= N && height[i] < len) i ++;
            if(i > N) break;
            ma = sa[i-1];
            mi = sa[i-1];
            while(i <= N && height[i] >= len)
            {
                ma = max(ma, sa[i]);
                mi = min(mi, sa[i]);
                i ++;
            }
            if(ma - mi >= len) return true;
        }
        return false;
    }
    int main()
    {
        int s[size];
        while(scanf("%d",&N)!=EOF)
        {
            if(!N) return 0;
            for(int i=0;i<N;i++)
            {
                scanf("%d",&s[i]);
            }
            for(int i=0;i<N-1;i++)
            {
                s[i]=s[i+1]-s[i]+88;
            }
            s[N-1]=0;
            getSuffix(s);
            int low = 4, high = (N-1)/2, mid;
            while(low < high)
            {
                mid = (low + high + 1) / 2;
                if(valid(mid)) {
                    low = mid;
                }else {
                    high = mid - 1;
                }
            }
            int ans = low < 4 ? 0 : low + 1;
            if(N<10) ans=0;
            printf("%d
    ", ans);
        }
    }
     
  • 相关阅读:
    追寻缺失的大学精神 一个民族需要关注天空的人
    图论简介
    18个分形图形的GIF动画演示
    平行宇宙
    eclipse经常出现——未响应!!!
    单例模式
    Java内存区域
    编译与解释(java)
    正则表达式判断QQ号格式是否正确
    正则表达式判断手机号格式是否正确
  • 原文地址:https://www.cnblogs.com/chen9510/p/5487060.html
Copyright © 2011-2022 走看看