zoukankan      html  css  js  c++  java
  • poj 1743 Musical Theme

    Musical Theme
    Time Limit: 1000MS   Memory Limit: 30000K
         

    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

    题目大意:
    从所给字符串里找最长的2个子串,这2个子串满足以下3个条件:
    ① 每个长度>=5
    ② 2个子串的第i个数到第i+1个数的加减变化情况相等
    ③ 2子串不重叠
    输出子串最长长度
    多组数据
     
    解题思路:
    对于条件② ,差分,在差分序列上做后缀数组
    求最值,可以二分答案mid,
    满足条件①,二分起点l=4,因为差分数组长度为len,对应的序列长度为len+1
    后缀数组得到的height数组,根据mid分组,保证 相邻的 height值>=mid的 分在同一组
    这样分出的组满足:同一组内任意两后缀的公共前缀>=mid
    每一组内,找到后缀起始位置的min,max,判断是否满足条件③
    输出ans+1,原因同l=4
    #include<cstdio>
    #include<cmath>
    #include<cstring>
    #include<algorithm>
    #define N 20010  
    using namespace std;
    int n,p,q=1,k,a[N],rk[2][N],sa[2][N],v[N],h[N],ans=-1,f[N][16];
    char s[N]; 
    void mul(int *sa,int *rk,int *SA,int *RK)
    {
        for(int i=1;i<=n;i++) v[rk[sa[i]]]=i;
        for(int i=n;i;i--) if(sa[i]>k) SA[v[rk[sa[i]-k]]--]=sa[i]-k;
        for(int i=n-k+1;i<=n;i++) SA[v[rk[i]]--]=i;
        for(int i=1;i<=n;i++) RK[SA[i]]=RK[SA[i-1]]+(rk[SA[i]]!=rk[SA[i-1]]||rk[SA[i]+k]!=rk[SA[i-1]+k]); 
    } 
    void presa()
    {
        for(int i=1;i<=n;i++) v[a[i]]++;
        for(int i=1;i<=180;i++) v[i]+=v[i-1];
        for(int i=1;i<=n;i++) sa[p][v[a[i]]--]=i;
        for(int i=1;i<=n;i++) rk[p][sa[p][i]]=rk[p][sa[p][i-1]]+(a[sa[p][i]]!=a[sa[p][i-1]]);
        for(k=1;k<n;k<<=1,swap(p,q)) 
        {
            mul(sa[p],rk[p],sa[q],rk[q]);
            if(rk[q][sa[q][n]]==n) 
            {
                swap(p,q);
                return;
            }
        }
    }
    void getheight()
    {
        int j,g=0;
        for(int i=1;i<=n;i++)
        {
            j=sa[p][rk[p][i]-1];
            while(a[i+g]==a[j+g]) g++;
            h[rk[p][i]]=g;if(g) g--;
        }
    }
    void pre()
    {
        p=0;q=1;k=0;
        memset(a,0,sizeof(a));
        memset(rk,0,sizeof(rk));
        memset(sa,0,sizeof(sa));
        memset(v,0,sizeof(v));
        memset(h,0,sizeof(h));
        ans=-1;
    }
    bool check(int mid)
    {
        int minn=n+1,maxn=-1;
        for(int i=2;i<=n;i++)
        {
            if(h[i]<mid)
            {
                if(maxn-minn>=mid) return true;
                minn=n+1;maxn=-1;
            }
            minn=min(minn,sa[p][i]);
            maxn=max(maxn,sa[p][i]);
        }
        if(maxn-minn>=mid) return true;
        return false;
    }
    void solve()
    {
        int l=4,r=n,mid;
        while(l<=r)
        {
            mid=l+r>>1;
            if(check(mid)) ans=mid,l=mid+1;
            else r=mid-1;
        }
        printf("%d
    ",ans+1);
    }
    int main()
    {
        while(scanf("%d",&n))
        {
            if(!n) return 0;
            pre();
            int last,x;
            scanf("%d",&last);
            for(int i=2;i<=n;i++)
            {
                scanf("%d",&x);
                a[i-1]=x-last;
                a[i-1]+=88;
                last=x;
            }
            n--;
            presa();
            getheight();
            solve();
        }
    }
     
  • 相关阅读:
    Document
    Document
    Document
    Document
    Document
    Document
    Document
    Document
    export和import 输出/接收模块变量的接口
    webpack:(模块打包机)
  • 原文地址:https://www.cnblogs.com/TheRoadToTheGold/p/6594317.html
Copyright © 2011-2022 走看看