• POJ 1743 Musical Theme 二分+后缀数组


    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.
     
    来源:http://blog.sina.com.cn/s/blog_6635898a0102e0me.html
     
    #include <iostream>
    #include <cstdio>
    #include <cmath>
    #include <cstring>
    #include <algorithm>
    using namespace std;
    #pragma comment(linker, "/STACK:102400000,102400000")
    #define ls i<<1
    #define rs ls | 1
    #define mid ((ll+rr)>>1)
    #define pii pair<int,int>
    #define MP make_pair
    typedef long long LL;
    const long long INF = 1e18+1LL;
    const double Pi = acos(-1.0);
    const int N = 2e5+10, M = 2e5+20, mod = 1e9+7, inf = 2e9;
    
    ///heght[i] 表示 Suffix(sa[i-1])和Suffix(sa[i]) 的最长公共前缀:
    ///rank[i] 表示 开头为i的后缀的等级:
    ///sa[i] 表示 排名为i的后缀 的开头位置:
    
    int *rank,r[N],sa[N],height[N],wa[N],wb[N],wm[N];
    bool cmp(int *r,int a,int b,int l) {
        return r[a] == r[b] && r[a+l] == r[b+l];
    }
    
    void SA(int *r,int *sa,int n,int m) {
        int *x=wa,*y=wb,*t;
        for(int i=0;i<m;++i)wm[i]=0;
        for(int i=0;i<n;++i)wm[x[i]=r[i]]++;
        for(int i=1;i<m;++i)wm[i]+=wm[i-1];
        for(int i=n-1;i>=0;--i)sa[--wm[x[i]]]=i;
        for(int i=0,j=1,p=0;p<n;j=j*2,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<m;++i)wm[i]=0;
            for(i=0;i<n;++i)wm[x[y[i]]]++;
            for(i=1;i<m;++i)wm[i]+=wm[i-1];
            for(i=n-1;i>=0;--i)sa[--wm[x[y[i]]]]=y[i];
            for(t=x,x=y,y=t,i=p=1,x[sa[0]]=0;i<n;++i) {
                x[sa[i]]=cmp(y,sa[i],sa[i-1],j)?p-1:p++;
            }
        }
        rank=x;
    }
    void Height(int *r,int *sa,int n) {
        for(int i=0,j=0,k=0;i<n;height[rank[i++]]=k)
        for(k?--k:0,j=sa[rank[i]-1];r[i+k] == r[j+k];++k);
    }
    
    int n,a[N];
    int check(int len) {
            int i = 2, mx, mi;
            while(1) {
                while(i <= n && height[i] < len) i++;
                if(i > n) break;
                mx = sa[i-1];
                mi = sa[i-1];
                while(i <= n && height[i] >= len) {
                    mx = max(mx,sa[i]);
                    mi = min(mi,sa[i]);
                    i++;
                }
                if(mx - mi >= len) return 1;
            }
            return 0;
    }
    int main() {
            while(~scanf("%d",&n)) {
                if(!n) break; 
                a[0] = 0;
                for(int i = 0; i < n; ++i) scanf("%d",&a[i]);
                n--;
                for(int i = 0; i < n; ++i) r[i] = a[i+1]-a[i] + 100;
                r[n] = 0;
                SA(r,sa,n+1,300);
                Height(r,sa,n);
                int ll = 4, rr = n,ans = 1;
                while(ll <= rr) {
                    int md = (ll + rr) >> 1;
                    int bo = check(md);
                    if(bo)  ans = md,ll = md + 1;
                    else rr = md - 1;
                }
                if(ans >= 4) {
                    printf("%d
    ",ans+1);
                } else puts("0");
            }
            return 0;
    }
      
  • 相关阅读:
    欧拉图
    hdu2544 迪杰斯特拉题目优化
    迪杰斯特拉--数组模拟邻接表优化
    快速幂
    四叉树 bnuoj
    逆康拓展开展开
    全排列 STL
    魔板拼图
    「luogu4366」最短路
    「国家集训队」稳定婚姻
  • 原文地址:https://www.cnblogs.com/zxhl/p/5997960.html
Copyright © 2020-2023  润新知