|
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:
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 |
[Submit] [Go Back] [Status] [Discuss]
因为要不重复,所以只用二分长度,把排名连续的>mid的分到一起,看同一组隔得最远的数列上的位置相差是否也>mid,若有则为真.有公长且不重叠的长度mid,mid以下的长度自然也可以,所以具有二分性.
用后一个减前一个就能判断题目中所说的变调相同的序列,这是个小技巧,可以自己模拟一下.
#include<stdio.h>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=20010;
int s[maxn],wa[maxn],wb[maxn],sa[maxn],r[maxn],ws[maxn],wv[maxn],rank[maxn],height[maxn],ans;
inline const int read(){
register int f=1,x=0;
register char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=(x<<3)+(x<<1)+ch-'0';ch=getchar();}
return f*x;
}
inline bool cmp(int *r,int a,int b,int l){
return r[a]==r[b]&&r[a+l]==r[b+l];
}
void da(int *r,int *sa,int m,int n)
{
register int i,j,p;
int * x=wa,* y=wb,*t;
for(i=0;i<m;i++) ws[i]=0;
for(i=0;i<n;i++) ws[x[i]=r[i]]++;
for(i=1;i<m;i++) ws[i]+=ws[i-1];
for(i=n-1;i>=0;i--) sa[--ws[x[i]]]=i;
for(j=1,p=1;p<n;j=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++) ws[i]=0;
for(i=0;i<n;i++) ws[wv[i]]++;
for(i=1;i<m;i++) ws[i]+=ws[i-1];
for(i=n-1;i>=0;i--) sa[--ws[wv[i]]]=y[i];
int i;
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;
}
inline void calc_height(int *r,int n){
register 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++);
}
inline bool check(int count,int n){
int minn=sa[0],maxx=sa[0];
for(register int i=0;i<=n;i++){
if(height[i]>=count){
minn=min(minn,sa[i]);
maxx=max(maxx,sa[i]);
if(maxx-minn>count) return true;
}
else minn=maxx=sa[i];
}
return false;
}
int main(){
int n=0;
while(n=read()){
for(register int i=0;i<n;i++) s[i]=read();
n--;ans=0;
for(register int i=0;i<n;i++) s[i]=s[i+1]-s[i]+90;s[n]=0;
da(s,sa,200,n+1);
calc_height(s,n);
int lf=0,rg=n;
while(lf<=rg){
int mid=(lf+rg)>>1;
if(check(mid,n)) ans=mid,lf=mid+1;
else rg=mid-1;
}
if(check(ans+1,n)) ans++;
if(ans>=4) printf("%d\n",ans+1);
else puts("0");
}
}

本文介绍了一种算法,用于从音乐旋律中找出最长的主题序列。通过对比音符序列并考虑音调变化,该算法能够识别出至少五音符长、在乐曲中至少出现两次且不重叠的主题。
387

被折叠的 条评论
为什么被折叠?



