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

poj1743-----Musical Theme

时间:2016-03-30 12:53:19      阅读:178      评论:0      收藏:0      [点我收藏+]

标签:

Musical Theme
Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 24390   Accepted: 8218

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(1 <= N <=20000)个音符的序列来表示一首乐曲,每个音符都是1..88范围内的整数,现在要找一个重复的主题。“主题”是整个音符序列的一个子串,它需要满足如下条件:

    1.长度至少为5个音符。

    2.在乐曲中重复出现。(可能经过转调,“转调”的意思是主题序列中每个音符都被加上或减去了同一个整数值)

    3.重复出现的同一主题不能有公共部分。

 
这种瘠薄题,打错都错模板上了,本题先把数组转换成相邻的差,问题转变为寻找相同段,长度大于等于5,而且无相交部分,我们可以二分答案,将height值大于等于mid的分成同一组,同一组内,若是mx_sa和mn_sa差距大于x,那么这便是没有相交的一组答案。
 1 #include<cstdio>
 2 #include<cstring>
 3 #include<cmath>
 4 #include<iostream>
 5 #include<algorithm>
 6 char s[500005];
 7 int num[500005],wa[500005],ws[500005],wb[500005],h[500005],n;
 8 int wv[500005],rank[500005],sa[500005];
 9 bool cmp(int *r,int a,int b,int l){
10     return r[a]==r[b]&&r[a+l]==r[b+l];
11 }
12 void da(int *r,int *sa,int n,int m){
13     int *t,*x=wa,*y=wb,i,j,k,p;
14     for (i=0;i<m;i++) ws[i]=0;
15     for (i=0;i<n;i++) x[i]=r[i];
16     for (i=0;i<n;i++) ws[x[i]]++;
17     for (i=1;i<m;i++) ws[i]+=ws[i-1];
18     for (i=n-1;i>=0;i--) sa[--ws[x[i]]]=i;
19     for (j=1,p=1;p<n;j*=2,m=p){
20         for (p=0,i=n-j;i<n;i++) y[p++]=i;
21         for (i=0;i<n;i++) if (sa[i]>=j) y[p++]=sa[i]-j;
22         for (i=0;i<m;i++) ws[i]=0;
23         for (i=0;i<n;i++) wv[i]=x[y[i]];
24         for (i=0;i<n;i++) ws[wv[i]]++;
25         for (i=1;i<m;i++) ws[i]+=ws[i-1];
26         for (i=n-1;i>=0;i--) sa[--ws[wv[i]]]=y[i];
27         for (t=x,x=y,y=t,i=1,x[sa[0]]=0,p=1;i<n;i++){
28             x[sa[i]]=cmp(y,sa[i-1],sa[i],j)?p-1:p++;
29         }
30     }
31 }
32 void cal(int *r,int n){
33     int i,j,k=0;
34     for (int i=1;i<=n;i++) rank[sa[i]]=i;
35     for (int i=0;i<n;h[rank[i++]]=k){
36         for (k?k--:0,j=sa[rank[i]-1];r[j+k]==r[i+k];k++);
37     }
38 }
39 bool check(int x){
40     int mn=sa[0],mx=sa[0];
41     for (int i=0;i<=n;i++)
42      if (h[i]>=x){
43         mx=std::max(mx,sa[i]);
44         mn=std::min(mn,sa[i]);
45         if (mx-mn>x) return 1;
46     }else
47     mx=mn=sa[i];
48     return 0;
49 }
50 void work(){
51     int l=0,r=n,ans=0;
52     while (l<=r){
53         int mid=(l+r)/2;
54         if (check(mid)) ans=mid,l=mid+1;
55         else r=mid-1;
56     }
57     if (ans<4) ans=0;
58     else ans++;
59     printf("%d\n",ans);
60 }
61 int main(){
62     while (~scanf("%d",&n)){
63         if (n==0) break;
64         for (int i=0;i<n;i++) scanf("%d",&num[i]);
65         for (int i=0;i<n-1;i++) num[i]=num[i+1]-num[i]+90;
66         n--;num[n]=0;
67         da(num,sa,n+1,200);
68         cal(num,n);
69         work();
70     }
71 }

 

 
 

poj1743-----Musical Theme

标签:

原文地址:http://www.cnblogs.com/qzqzgfy/p/5336572.html

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