码迷,mamicode.com
首页 > 其他好文 > 详细

POJ1743 Musical Theme

时间:2016-07-11 14:04:59      阅读:244      评论:0      收藏:0      [点我收藏+]

标签:

 

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\n"); 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\n");
100     else printf("%d\n",l);
101     }
102 }
103 
104 int main()
105 {
106     work();
107     return 0;
108 }

 

POJ1743 Musical Theme

标签:

原文地址:http://www.cnblogs.com/ljh2000-jump/p/5659920.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!