zoukankan      html  css  js  c++  java
  • poj 1743 Musical Theme (后缀数组+二分法)

    Musical Theme
    Time Limit: 1000MS   Memory Limit: 30000K
    Total Submissions: 16162   Accepted: 5577

    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

      1 //800K    204MS    C++    3271B    2013-12-14 11:09:52
      2 /*
      3 
      4     题意:
      5         给出一串音律,要求:
      6         1、最少5个数字长;
      7 
      8         2、重复出现的时候,必须相对差值一样,比如{ 1 ,2, 3, 22, 10, 11, 12  },1,2,3与10 11 12是一样的韵律。
      9 
     10         3、韵律不可重叠。
     11         
     12         求最长的一串
     13     
     14     后缀数组+二分法:
     15         WA了n次,最后反复对照..原来是DA算法写错了一步...
     16         假设最长的串长度为 mid ,然后求证mid是否存在,这一步要求对height数组有较好的理解 
     17         mid用二分法求解 
     18 
     19     写完后在看别人代码找bug时才发现这是 男人八题 中的一题,默默的开心- 
     20      
     21 */
     22 #include<stdio.h>
     23 #include<string.h>
     24 #define N 20005
     25 int wa[N],wb[N],ws[N],wv[N],wd[N];
     26 int rank[N],height[N];
     27 int Max(int a,int b)
     28 {
     29     return a>b?a:b;
     30 }
     31 int Min(int a,int b)
     32 {
     33     return a<b?a:b;
     34 }
     35 int Abs(int a)
     36 {
     37     return a<0?-a:a;
     38 }
     39 void get_height(int *r,int *sa,int n)
     40 {
     41     int i,j,k=0;
     42     for(i=1;i<=n;i++) rank[sa[i]]=i;
     43     for(i=0;i<n;height[rank[i++]]=k)
     44         for(k?k--:0,j=sa[rank[i]-1];r[i+k]==r[j+k];k++);
     45     return;
     46 }
     47 int cmp(int *r,int a,int b,int l)
     48 {
     49     return r[a]==r[b]&&r[a+l]==r[b+l];
     50 }
     51 void DA(int *r,int *sa,int n,int m)
     52 {
     53     int i,j,p,*x=wa,*y=wb,*t;
     54     for(i=0;i<m;i++) ws[i]=0;
     55     for(i=0;i<n;i++) ws[x[i]=r[i]]++;
     56     for(i=1;i<m;i++) ws[i]+=ws[i-1];
     57     for(i=n-1;i>=0;i--) sa[--ws[x[i]]]=i;
     58     
     59     for(p=1,j=1;p<n;j*=2,m=p){
     60         for(p=0,i=n-j;i<n;i++) y[p++]=i;
     61         for(i=0;i<n;i++) if(sa[i]>=j) y[p++]=sa[i]-j;
     62         
     63         for(i=0;i<n;i++) wv[i]=x[y[i]];
     64         for(i=0;i<m;i++) ws[i]=0;
     65         for(i=0;i<n;i++) ws[wv[i]]++;
     66         for(i=1;i<m;i++) ws[i]+=ws[i-1];
     67         for(i=n-1;i>=0;i--) sa[--ws[wv[i]]]=y[i];
     68         
     69         for(t=x,x=y,y=t,p=1,x[sa[0]]=0,i=1;i<n;i++)
     70             x[sa[i]]=cmp(y,sa[i-1],sa[i],j)?p-1:p++;
     71     }
     72     return;
     73 }
     74 int main(void)
     75 {
     76     int n,r[N];
     77     int sa[N]; 
     78     while(scanf("%d",&n),n)
     79     {
     80         n--;
     81         for(int i=0;i<=n;i++) scanf("%d",&r[i]);
     82         if(n<10){
     83             puts("0");continue;
     84         }
     85         for(int i=0;i<n;i++) r[i]=r[i+1]-r[i]+90;
     86         r[n]=0;
     87         DA(r,sa,n+1,200);
     88         get_height(r,sa,n);
     89         int up=n;
     90         int down=3;
     91         int mid;
     92         int ans;
     93         while(up>=down){
     94             int flag=0;
     95             mid=(up+down)>>1;
     96             for(int i=2;i<=n;i++){
     97                 if(height[i]>=mid){ 
     98                     for(int j=i-1;j>=2;j--){
     99                         if(Abs(sa[i]-sa[j])>=mid) flag=1;
    100                         if(height[j]<mid) break;
    101                     }
    102                 }
    103                 if(flag) break;
    104             } 
    105             if(flag){
    106                 down=mid+1;
    107                 ans=mid;
    108             }
    109             else up=mid-1;
    110             //printf("%d
    ",mid);
    111         }
    112         if(ans<4) puts("0");
    113         else printf("%d
    ",ans+1);
    114     }
    115     return 0;
    116 }
  • 相关阅读:
    动画 + 设置contentoffset,然后就 蛋疼了,
    xmpp这一段蛋疼的 坑,
    项目,
    一段测试代码,哦哦哦,
    libresolv,
    mutating method sent to immutable object'
    解析json,是还是不是,
    济南学习 Day 4 T1 am
    济南学习 Day 3 T3 pm
    济南学习 Day 3 T2 pm
  • 原文地址:https://www.cnblogs.com/GO-NO-1/p/3474168.html
Copyright © 2011-2022 走看看