zoukankan      html  css  js  c++  java
  • poj1743 Musical Theme

    Musical Theme
    Time Limit: 1000MS   Memory Limit: 30000K
    Total Submissions: 28409   Accepted: 9591

    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.

    Source

    题意:
    求两个最长相似子串(差分序列相同)的长度
    /*
    得出height数组后,二分答案x,将连续的>=x的段分组
    如果一组内sa的最大值与最小值的差>=x,则x成立
    */
    #include<cstdio>
    #include<cstring>
    #include<iostream>
    using namespace std;
    const int N=2e4+5;
    int n,sa[N],tsa[N],rank[N],trank[N],c[N],h[N];
    int s[N];
    void DA(int maxx=256){
        memset(c,0,sizeof c);int p;
        for(int i=1;i<=n;i++) c[rank[i]=s[i]]++;
        for(int i=2;i<=maxx;i++) c[i]+=c[i-1];
        for(int i=n;i;i--) sa[c[rank[i]]--]=i;
        trank[sa[1]]=p=1;
        for(int i=2;i<=n;i++){
            if(rank[sa[i]]!=rank[sa[i-1]]) p++;
            trank[sa[i]]=p;
        }
        for(int i=1;i<=n;i++) rank[i]=trank[i];
        for(int k=1;p<n;k<<=1,maxx=p){
            p=0;
            for(int i=n-k+1;i<=n;i++) tsa[++p]=i;
            for(int i=1;i<=n;i++) if(sa[i]>k) tsa[++p]=sa[i]-k;
            memset(c,0,sizeof c);
            for(int i=1;i<=n;i++) trank[i]=rank[tsa[i]];
            for(int i=1;i<=n;i++) c[trank[i]]++;
            for(int i=2;i<=maxx;i++) c[i]+=c[i-1];
            for(int i=n;i;i--) sa[c[trank[i]]--]=tsa[i];
            trank[sa[1]]=p=1;
            for(int i=2;i<=n;i++){
                if(rank[sa[i]]!=rank[sa[i-1]]||rank[sa[i]+k]!=rank[sa[i-1]+k]) p++;
                trank[sa[i]]=p;
            }
            for(int i=1;i<=n;i++) rank[i]=trank[i];
        }
        for(int i=1,k=0;i<=n;i++){
            int j=sa[rank[i]-1];
            while(s[i+k]==s[j+k]) k++;
            h[rank[i]]=k;if(k>0) k--;
        }
    }
    bool judge(int k){
        int mn=sa[1],mx=sa[1];
        for(int i=2;i<=n;i++){
            if(h[i]<k){
                mn=mx=sa[i];
            }
            else{
                mx=max(mx,sa[i]);
                mn=min(mn,sa[i]);
                if(mx-mn>=k) return 1;
            } 
        }
        return 0;
    }
    void Clear(){
        memset(h,0,sizeof h);
        memset(sa,0,sizeof sa);
        memset(tsa,0,sizeof tsa);
        memset(rank,0,sizeof rank);
        memset(trank,0,sizeof trank);
    }
    int main(){
        while(scanf("%d",&n)==1){
            if(!n) break;
            Clear();
            for(int i=1;i<=n;i++) scanf("%d",&s[i]);n--;
            for(int i=1;i<=n;i++) s[i]=s[i+1]-s[i]+100;
            DA();
            int l=0,r=n/2,mid,ans=0;
            while(l<=r){
                mid=l+r>>1;
                if(judge(mid)) l=mid+1,ans=mid;
                else r=mid-1;
            }
            ans++;
            if(ans<5) ans=0;
            printf("%d
    ",ans);
        }
        return 0;
    } 
  • 相关阅读:
    HTML_常用标签
    介绍Html_前端
    Mysql(2)数据类型与约束
    Mysql命令
    python_高级进阶(5)协程_事件
    python_高级进阶(4)同步_异步_回调函数_阻塞
    数据类型的补充以及各类型之间的相互转换
    二次编码
    坑坑坑坑坑坑----列表
    字典
  • 原文地址:https://www.cnblogs.com/shenben/p/6589900.html
Copyright © 2011-2022 走看看