• POJ 1743 Musical Theme (后缀数组)


    Musical Theme
    Time Limit: 1000MS   Memory Limit: 30000K
    Total Submissions: 14584   Accepted: 5026

    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

     
     

    【题意】

    一篇n个音符的乐谱,求他最长的音乐主题

    乐谱由n个小于88的正整数表示

    所谓的音乐主题就是两端变化相同的长度大于五的不重合乐谱

    比如1 2 3 4 5 6 7 8 9 10

    1 2 3 4 5的变化与6 7 8 9 10就相同,且两端不重合

    详细介绍:http://tieba.baidu.com/f?kz=754580296

      http://www.cnblogs.com/staginner/archive/2012/02/02/2335600.html

     解题报告: http://www.cnblogs.com/staginner/archive/2012/02/03/2337299.html

    /*
    height数组是从1到n的
    接着sa数组是从1到n的,接着sa【0】肯定是我们在后面加的最小的字母
    接着的话,由于我们算是差值,所以的话一定要注意
    rmax-rmin>mid,而不是>=mid,因为可能出现用同一个元素的情况
    */
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    using namespace std;
    const int maxn=30000;
    char str[maxn];
    int wa[maxn],wb[maxn],wv[maxn],wn[maxn],a[maxn],sa[maxn];
    int cmp(int* r,int a,int b,int l)
    {
        return r[a]==r[b]&&r[a+l]==r[b+l];
    }
    //n为字符串长度,m为字符的取值范围,r为字符串。后面的j为每次排序时子串的长度
    void DA(int* r,int* sa,int n,int m)
    {
        int i,j,p,*x=wa,*y=wb,*t;
        ///对R中长度为1的子串进行基数排序
        for(i=0; i<m; i++)wn[i]=0;
        for(i=0; i<n; i++)wn[x[i]=r[i]]++;
        for(i=1; i<m; i++)wn[i]+=wn[i-1];
        for(i=n-1; i>=0; i--)sa[--wn[x[i]]]=i;
        for(j=1,p=1; p<n; 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<n; i++)wv[i]=x[y[i]];
            for(i=0; i<m; i++)wn[i]=0;
            for(i=0; i<n; i++)wn[wv[i]]++;
            for(i=1; i<m; i++)wn[i]+=wn[i-1];
            for(i=n-1; i>=0; i--)sa[--wn[wv[i]]]=y[i];
            ///当p=n的时候,说明所有串都已经排好序了
            ///在第一次排序以后,rank数组中的最大值小于p,所以让m=p
            for(t=x,x=y,y=t,p=1,x[sa[0]]=0,i=1; i<n; i++)
                x[sa[i]]=cmp(y,sa[i-1],sa[i],j)?p-1:p++;
        }
        return;
    }
    ///后缀数组  计算height数组
    /**
    height数组的值应该是从height[1]开始的,而且height[1]应该是等于0的。
    原因是,+因为我们在字符串后面添加了一个0号字符,所以它必然是最小的
    一个后缀。而字符串中的其他字符都应该是大于0的(前面有提到,使用倍
    增算法前需要确保这点),所以排名第二的字符串和0号字符的公共前缀
    (即height[1])应当为0.在调用calheight函数时,要注意height数组的范
    围应该是[1..n]。所以调用时应该是calheight(r,sa,n)
    而不是calheight(r,sa,n+1)。*/
    int rank[maxn],height[maxn];
    void calheight(int* r,int* sa,int n)
    {
        int i,j,k=0;
        for(i=1; i<=n; i++)rank[sa[i]]=i;
        for(i=0; i<n; height[rank[i++]]=k)
            for(k?k--:0,j=sa[rank[i]-1]; r[i+k]==r[j+k]; k++);
        return;
    }
    int main()
    {
        int n;
        while(scanf("%d",&n)!=EOF)
        {
            if(n==0) break;
            int x,y;
            scanf("%d",&x);
            n--;
            for(int i=0; i<n; i++)
            {
                scanf("%d",&y);
                a[i]=y-x+90;
                x=y;
            }
            a[n]=0;
            //for(int i=0; i<n; i++)  printf("%d  ",a[i]);
            //printf("%d/n/n",a[n]);
            //printf("1111111111/n");
            DA(a,sa,n+1,189);
            // printf("111/n");
            calheight(a,sa,n);//height  1-->n
            //printf("111111111/n");
           // for(int i=1; i<n; i++) printf("%d ",height[i]);
           // printf("%d/n",height[n]);
            int fir=0,sec=n;
            int ans=-1;
            while(fir<=sec)
            {
                int mid=(sec+fir)>>1;
               // printf("mid=%d/n",mid);
                int rmin=sa[0],rmax=sa[0];
                bool flag=false;
                for(int i=1; i<=n; i++)
                    if(height[i]>=mid)
                    {
                        if(sa[i]>rmax) rmax=sa[i];
                        if(sa[i]<rmin) rmin=sa[i];
                    }
                    else
                    {
                       // printf("rmin1=%d  rmax1=%d/n",rmin,rmax);
                        if(rmax-rmin>mid)
                        {
                            ans=mid;
                           // printf("ans=%d/n",ans);
                           // printf("rmin=%d  rmax=%d/n",rmin,rmax);
                            flag=true;
                        }
                        rmin=sa[i],rmax=sa[i];
                        //printf("rmin=%d  rmax=%d/n",rmin,rmax);
                    }
                if(rmax-rmin>mid)
                {
                    ans=mid;
                    //printf("ans=%d/n",ans);
                   // printf("rmin=%d  rmax=%d/n",rmin,rmax);
                    flag=true;
                }
                if(flag) fir=mid+1;
                else sec=mid-1;
            }
            //printf("%d/n",ans);
            if(ans>=4)  printf("%d/n",ans+1);
            else printf("0/n");
        }
        return 0;
    }
  • 相关阅读:
    php中文乱码处理方法
    Zend 官方框架增加 Swoole 协程支持 !
    矩阵行列式的向量表示
    ArduinoYun教程之ArduinoYun硬件介绍
    MIT 操作系统实验 MIT JOS lab1
    java File_encoding属性
    Java入门 第一季第五章 编程练习解析
    android 三种定位方式
    6.30
    OpenJudge百炼习题解答(C++)--题3142:球弹跳高度的计算
  • 原文地址:https://www.cnblogs.com/jackge/p/3092059.html
Copyright © 2020-2023  润新知