zoukankan      html  css  js  c++  java
  • Poj 1743 Musical Theme(后缀数组+二分答案)

    Musical Theme
    Time Limit: 1000MS Memory Limit: 30000K
    Total Submissions: 28435 Accepted: 9604
    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
    LouTiancheng@POJ

    /*
    后缀数组+二分答案.
    先将原数组差分.
    求出height数组后二分答案判定.
    */
    #include<iostream>
    #include<cstring>
    #include<cstdio>
    #define MAXN 20001
    using namespace std;
    int n,m=188,ans,s[MAXN],sa[MAXN],ht[MAXN],rank[MAXN],t1[MAXN],t2[MAXN],c[190],ch[MAXN];
    bool cmp(int *y,int a,int b,int k)
    {
        int a1=y[a],b1=y[b];
        int a2=a+k>=n?-1:y[a+k];
        int b2=b+k>=n?-1:y[b+k];
        return a1==b1&&a2==b2;
    }
    void slovesa()
    {
        int *x=t1,*y=t2;
        for(int i=0;i<n;i++) c[x[i]=s[i]]++;
        for(int i=1;i<m;i++) c[i]+=c[i-1];
        for(int i=n-1;i>=0;i--) sa[--c[x[i]]]=i;
        for(int k=1,p=0;k<=n;k<<=1,m=p,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++) c[i]=0;
            for(int i=0;i<n;i++) c[x[y[i]]]++;
            for(int i=1;i<m;i++) c[i]+=c[i-1];
            for(int i=n-1;i>=0;i--) sa[--c[x[y[i]]]]=y[i];
            swap(x,y),p=1,x[sa[0]]=0;
            for(int i=1;i<n;i++)
            {
                if(cmp(y,sa[i-1],sa[i],k)) x[sa[i]]=p-1;
                else x[sa[i]]=p++;
            }
            if(p>=n) break;
        }
    }
    void sloveheight()
    {
        int k=0;
        for(int i=0;i<n;i++) rank[sa[i]]=i;
        for(int i=0;i<n;ht[rank[i++]]=k)
        {
            if(!rank[i]) continue;
            int j=sa[rank[i]-1];
            if(k) k--;
            while(j+k<n&&i+k<n&&s[j+k]==s[i+k]) k++;
        }
        ht[0]=0;
    }
    bool check(int x)
    {
        int lans=1e9,rans=0;
        for(int i=1;i<n;i++)
        {
            if(ht[i]>=x)
            {
                lans=min(lans,min(sa[i],sa[i-1]));
                rans=max(rans,max(sa[i],sa[i-1]));
            }
            else {
                if(rans-lans>x) return true;
                rans=0;lans=n+1;
            }
        }
        if(rans-lans>x) return true;
        return false;
    }
    void erfen(int l,int r)
    {
        int mid;
        while(l<=r)
        {
            mid=(l+r)>>1;
            if(check(mid)) ans=mid,l=mid+1;
            else r=mid-1;
        }
    }
    void Clear()
    {
        ans=0,m=188;
        memset(t1,0,sizeof t1);
        memset(t2,0,sizeof t2);
        memset(c,0,sizeof c);
        memset(sa,0,sizeof sa);
        memset(ht,0,sizeof ht);
        memset(rank,0,sizeof rank);
        memset(ch,0,sizeof ch);
    }
    int main()
    {
        while(~scanf("%d",&n)!=EOF)
        {
            if(!n) break;Clear();
            for(int i=0;i<n;i++) scanf("%d",&ch[i]);
            for(int i=0;i<n-1;i++) s[i]=ch[i+1]-ch[i]+89;
            slovesa(),
            sloveheight(),
            erfen(0,n);
            if(ans+1>=5) printf("%d
    ",ans+1);
            else printf("0
    ");
        }
        return 0;
    }
    
  • 相关阅读:
    大数据用户画像方法与实践(干货 转帖)
    前端学习之路
    MySQL学习记录
    Linux 运维之路
    Python学习之路
    NgRx/Store 4 + Angular 5使用教程
    CSS实现各种形状
    CSS3实现背景透明,文字不透明
    CSS实现网页背景图片自适应全屏
    使用Restify+superagent做数据转发
  • 原文地址:https://www.cnblogs.com/nancheng58/p/10068011.html
Copyright © 2011-2022 走看看