zoukankan      html  css  js  c++  java
  • POJ 1743. Musical Theme

    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.
    题意大概就是取差值求一遍不重复的字串,唯一就是至少长度为5,而且重复字串不能连续(因为连续了说明原数组重叠了)。
    #include <iostream>
    #include <cstring>
    #include <cstdio>
    const int N = 20000 + 7;
    using namespace std;
    int n,r[N],wb[N],wss[N],wv[N],sa[N],rank[N],li[N];
    
    int cmp(int *r,int a,int b,int l)
    {
        return r[a] == r[b] && r[a+l] == r[b+l];
    }
    
    void Da(int n,int m)
    {
        int i,j,p,*x = rank, *y = wb, *t;
        for(i = 0; i < m; ++i) wss[i] = 0;
        for(i = 0; i < n; ++i) ++wss[x[i]=r[i]];
        for(i = 1; i < m; ++i) wss[i] += wss[i-1];
        for(i = n-1; i >= 0; --i ) sa[--wss[x[i]]] = i;
        for(j = p = 1; p < n; j <<= 1, m = p)
        {
            for(p = 0,i = n-j; i < n; ++i) y[p++] = i;
            for(i = 0; i < n; ++i) if(sa[i] >= j) y[p++] = sa[i] - j;
            for(i = 0; i < n; ++i) wv[i] = x[y[i]];
            for(i = 0; i < m; ++i) wss[i] = 0;
            for(i = 0; i < n; ++i) ++wss[wv[i]];
            for(i = 1; i < m; ++i) wss[i] += wss[i-1];
            for(i = n-1; i >= 0; --i) sa[--wss[wv[i]]] = y[i];
            for(t = x, x = y, y = t, p = i = 1, x[sa[0]] = 0; i < n; ++i)
            x[sa[i]] = cmp(y,sa[i-1],sa[i],j)?p-1:p++;
        }
    }
    
    int f[N], hi[N];
    void cal_height( )
    {
        int i,j,k = 0;
        for(i = 1; i <= n; ++i) f[sa[i]] = i;
        for(i = 0; i < n; hi[f[i++]] = k)
        for(k?k--:0,j = sa[f[i]-1]; r[i+k] == r[j+k]; ++k);
    }
    
    bool check(int len)
    {
        int minx = n + 1 , maxx = -1,i;
        for(i = 2; i <= n; ++i)
        {
            if(hi[i] >= len)
            {
                minx = min(minx,min(sa[i],sa[i-1]));
                maxx = max(maxx,max(sa[i],sa[i-1]));
            }
            else
            {
                if(minx + len < maxx) return true;
                minx = n + 1, maxx = -1;
             }
        }
        if(minx + len < maxx) return true; return false;
    }
    
    int  binary_search( )
    {
        int L = 1, R =  ( n >> 1 )+ 1, mid, ret = -1;
        while(L <= R)
        {
            mid = ( L + R ) >> 1;
            if(check(mid)) L = mid + 1, ret = max(ret , mid);
            else R = mid - 1;
        }
        return ret;
    }
    
    int main()
    {
        while(~scanf("%d",&n) && n)
        {
            int i; memset(r,0,sizeof(r));
            for(i = 0; i < n; ++i) scanf("%d",r+i);
            for(i = 0; i < n; ++i) r[i] = r[i+1] - r[i] + 100; ; n--; r[n] = 0;
            Da(n+1,201);
            cal_height( );
            int ans = binary_search()+1;
            if( ans < 5) puts("0");
            else printf("%d
    ",ans);
        }
        return 0;
    }
  • 相关阅读:
    S02_CH15_ AXI_OLED 实验
    S02_CH14_ EMIO_OLED 实验
    S02_CH13_ AXI_PWM 实验
    S02_CH12_ AXI_Lite 总线详解
    【第二季】CH11_ ZYNQ软硬调试高级技巧
    bzoj3028 食物
    Hdu3579 Hello Kiki
    bzoj4872 [Shoi2017]分手是祝愿
    bzoj4873 [Shoi2017]寿司餐厅
    省选模拟赛 LYK loves rabbits(rabbits)
  • 原文地址:https://www.cnblogs.com/Ateisti/p/6500353.html
Copyright © 2011-2022 走看看