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]
题目大意:
1.长度至少为5个音符
2.在乐曲中重复出现(可能经过转调,“转调”的意思是主题序列中每个音符都被加上或减去了同一个整数值。)
3.重复出现的同一主题不能有公共部分。
题解:后缀数组
相邻两项作差得到新的的序列,然后对新的序列建立后缀数组。我们要求的是重复不重叠最长子串,先二分答案x,然后将height数组分组,保证每个组内的height都大于等于x,然后看组中sa的最大值与最小值之差是否大于等于x。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define N 20003
using namespace std;
int rank[N],sa[N],a[N],b[N],xx[N],yy[N],*x,*y,height[N];
int n,len,m,p;
int cmp(int i,int j,int l)
{
return y[i]==y[j]&&(i+l>len?-1:y[i+l])==(j+l>len?-1:y[j+l]);
}
void get_SA()
{
x=xx; y=yy; m=180;
for (int i=1;i<=len;i++) ++b[x[i]=a[i]];
for (int i=1;i<=m;i++) b[i]+=b[i-1];
for (int i=len;i>=1;i--) sa[b[x[i]]--]=i;
for (int k=1;k<=len;k<<=1) {
p=0;
for (int i=len-k+1;i<=len;i++) y[++p]=i;
for (int i=1;i<=len;i++)
if (sa[i]>k) y[++p]=sa[i]-k;
for (int i=1;i<=m;i++) b[i]=0;
for (int i=1;i<=len;i++) b[x[y[i]]]++;
for (int i=1;i<=m;i++) b[i]+=b[i-1];
for (int i=len;i>=1;i--) sa[b[x[y[i]]]--]=y[i];
swap(x,y); p=2; x[sa[1]]=1;
for (int i=2;i<=len;i++)
x[sa[i]]=cmp(sa[i-1],sa[i],k)?p-1:p++;
if (p>len) break;
m=p+1;
}
p=0;
for (int i=1;i<=len;i++) rank[sa[i]]=i;
for (int i=1;i<=len;i++) {
if (rank[i]==1) continue;
int j=sa[rank[i]-1];
while (i+p<=len&&j+p<=len&&a[i+p]==a[j+p]) p++;
height[rank[i]]=p;
p=max(0,p-1);
}
}
int pd(int x)
{
int mn=len+1; int mx=0;
mn=sa[1]; mx=sa[1];
for (int i=2;i<=len;i++)
if (height[i]>=x)
mn=min(mn,sa[i]),mx=max(mx,sa[i]);
else {
if (mx-mn>=x) return 1;
mn=sa[i]; mx=sa[i];
}
if (mx-mn>=x) return 1;
return 0;
}
int main()
{
freopen("a.in","r",stdin);
freopen("my.out","w",stdout);
while (true){
scanf("%d",&len);
if (len==0) break;
memset(rank,0,sizeof(rank));
memset(sa,0,sizeof(sa));
memset(b,0,sizeof(b));
memset(height,0,sizeof(height));
int last=0; scanf("%d",&last);
for (int i=2;i<=len;i++){
scanf("%d",&a[i-1]); int x=a[i-1];
a[i-1]=a[i-1]-last+90;
last=x;
}
len--;
get_SA();
int ans=0; int l=1; int r=len;
while (l<=r) {
int mid=(l+r)/2;
if (pd(mid)) ans=max(ans,mid),l=mid+1;
else r=mid-1;
}
printf("%d\n",ans>=4?ans+1:0);
}
}