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

    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

     
     
    正解:后缀数组
    解题报告:
      首先声明,这道题是一道模板题(水题)。
      罗穗骞的论文的例题。中午的时候,我满心欢喜的打算下午要切掉至少5道后缀数组题,然后被这道题卡了一个下午加一个晚上。
      思想确实简单,但我就是无限wa,迷之wa。。。几个小时之后,各种努力之后,最后发现,特判的时候没有读数。。。
      多么痛的领悟。半天时间就这么报废了。。。我很不开心,不讲题解了,直接看代码吧。
     
      1 //It is made by jump~
      2 #include <iostream>
      3 #include <cstdlib>
      4 #include <cstring>
      5 #include <cstdio>
      6 #include <cmath>
      7 #include <algorithm>
      8 #include <ctime>
      9 #include <vector>
     10 #include <queue>
     11 #include <map>
     12 #include <set>
     13 #ifdef WIN32   
     14 #define OT "%I64d"
     15 #else
     16 #define OT "%lld"
     17 #endif
     18 using namespace std;
     19 typedef long long LL;
     20 const int MAXN = 20011;
     21 int ch[MAXN];
     22 int n,m;
     23 int wa[MAXN],wb[MAXN],c[MAXN];
     24 int rank[MAXN],height[MAXN];
     25 int sa[MAXN];
     26 int ans;
     27 
     28 inline int getint()
     29 {
     30     int w=0,q=0;
     31     char c=getchar();
     32     while((c<'0' || c>'9') && c!='-') c=getchar();
     33     if (c=='-')  q=1, c=getchar();
     34     while (c>='0' && c<='9') w=w*10+c-'0', c=getchar();
     35     return q ? -w : w;
     36 }
     37 
     38 inline void da(int m,int n){
     39     int i,*x=wa,*y=wb;
     40     for(i=1;i<=m;i++) c[i]=0;
     41     for(i=1;i<=n;i++) c[x[i]=ch[i]]++;
     42     for(i=1;i<=m;i++) c[i]+=c[i-1];
     43     for(i=n;i>=1;i--) sa[c[x[i]]--]=i;
     44     for(int k=1,p;k<=n;k=k*2) {
     45     p=0;
     46     for(i=n-k+1;i<=n;i++) y[++p]=i;
     47     for(i=1;i<=n;i++) if(sa[i]>k) y[++p]=sa[i]-k;
     48     for(i=1;i<=m;i++) c[i]=0;
     49     for(i=1;i<=n;i++) c[x[y[i]]]++;     
     50     for(i=1;i<=m;i++) c[i]+=c[i-1];
     51     for(i=n;i>=1;i--) sa[c[x[y[i]]]--]=y[i];
     52     swap(x,y);  x[sa[1]]=1; p=1;
     53     for(i=2;i<=n;i++) x[sa[i]]=(y[sa[i-1]]==y[sa[i]] && y[sa[i-1]+k]==y[sa[i]+k])?p:++p;
     54     if(p==n) break; m=p;
     55     }
     56 }
     57 
     58 inline void calheight(){
     59     int i,j,k=0;
     60     for(i=1;i<=n;i++) {
     61     if(k) k--;
     62     j=sa[rank[i]-1];
     63     while(ch[i+k]==ch[j+k]) k++;
     64     height[rank[i]]=k;//是rank[i]!!!
     65     }
     66 }
     67 
     68 inline bool check(int x){
     69     int i=2,minl,maxl;
     70     while(1) {
     71     while(i<=n && height[i]<x) i++;
     72     if(i>n) break;
     73     minl=maxl=sa[i-1];
     74     while(i<=n && height[i]>=x) minl=min(minl,sa[i]),maxl=max(maxl,sa[i]),i++;
     75     if(maxl-minl>x) return true;//不能等号!!!记录的是差
     76     }
     77     return false;
     78 }
     79 
     80 inline void work(){
     81     while(1) {
     82     n=getint();  if(n==0) break;
     83     memset(ch,0,sizeof(ch)); memset(sa,0,sizeof(sa));
     84     memset(wa,0,sizeof(wa)); memset(wb,0,sizeof(wb));
     85     memset(height,0,sizeof(height)); memset(rank,0,sizeof(rank));
     86     for(int i=1;i<=n;i++) ch[i]=getint();
     87     if(n<10) { printf("0
    "); continue; }
     88     for(int i=1;i<n;i++) ch[i]=ch[i+1]-ch[i],ch[i]+=89;
     89     ch[n]=0;
     90     da(300,n);
     91     for(int i=1;i<=n;i++) rank[sa[i]]=i;
     92     calheight();
     93     int l=1,r=n,mid; 
     94     while(l<r) {
     95         mid=(l+r)/2;
     96         if(check(mid)) l=mid+1;
     97         else r=mid;
     98     }
     99     if(l<=4) printf("0
    ");
    100     else printf("%d
    ",l);
    101     }
    102 }
    103 
    104 int main()
    105 {
    106     work();
    107     return 0;
    108 }
  • 相关阅读:
    mysql5.7 linux安装参考
    谈谈微服务中的 API 网关(API Gateway)
    十大Intellij IDEA快捷键
    SqoopFlume、Flume、HDFS之间比较
    PostgreSQL-存储过程(一)基础篇
    spark调优篇-oom 优化(汇总)
    spark调优篇-数据倾斜(汇总)
    spark调优篇-Spark ON Yarn 内存管理(汇总)
    spark异常篇-OutOfMemory:GC overhead limit exceeded
    spark异常篇-Removing executor 5 with no recent heartbeats: 120504 ms exceeds timeout 120000 ms 可能的解决方案
  • 原文地址:https://www.cnblogs.com/ljh2000-jump/p/5659920.html
Copyright © 2011-2022 走看看