Musical Theme
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 18455 | Accepted: 6310 |
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!
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.
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.
刚上手模板,来测试一下罗大牛模板的正确性!
这道题问的是一个序列,出现最长的相同的没有重叠的序列,这里所谓的相同,是指给前一个序列加上一个整数,能够变成后一个序列。
只要将原序列相邻两项相减,判断新序列最长的相同的没有重叠的序列即可,不过答案要加1,。因为n个数相减保存差值少了一个。
有重叠的话直接取下height数组的最大值即可。没有重叠将height数组的按照height>=k划分成若干组,这样就保证跨组是不能选的,因为LCP(i,j)=min{LCP(k-1,k)} i+1<=k<=j
此时跨组的那个height[t]<k,因此LCP(i,j)<k,不合题意。
接下来只需记录每组里后缀的起始位置最小最大值,只要保证 最大值-最小值<=k,即没有重叠。
因为这题不知道k具体是指多少?只有一个范围[1,n/2],因此我们尝试二分求出答案。
代码:
#include<cstdio>
#include<iostream>
#define Maxn 20010
using namespace std;
int r[Maxn],sa[Maxn];
int wa[Maxn],wb[Maxn],wv[Maxn],rs[Maxn];
int 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 n,int m)
{
int i,j,p,*x=wa,*y=wb,*t;
for(i=0;i<m;i++) rs[i]=0;
for(i=0;i<n;i++) rs[x[i]=r[i]]++;
for(i=1;i<m;i++) rs[i]+=rs[i-1];
for(i=n-1;i>=0;i--) sa[--rs[x[i]]]=i;
for(j=1,p=1;p<n;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<m;i++) rs[i]=0;
for(i=0;i<n;i++) rs[wv[i]=x[y[i]]]++;
for(i=1;i<m;i++) rs[i]+=rs[i-1];
for(i=n-1;i>=0;i--) sa[--rs[wv[i]]]=y[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++;
}
}
int rank[Maxn],height[Maxn];
void calheight(int *r,int *sa,int n)
{
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++);
}
bool check(int n,int mid){
int minx=sa[1],maxx=sa[1];
for(int i=2;i<n;i++){
if(height[i]<mid) minx=sa[i],maxx=sa[i];
else{
minx=min(minx,sa[i]);
maxx=max(maxx,sa[i]);
if(maxx-minx>=mid) return true;
}
}
return false;
}
int main()
{
int n;
while(scanf("%d",&n),n){
for(int i=0;i<n;i++)
scanf("%d",r+i);
for(int i=0;i<n-1;i++)
r[i]=r[i]-r[i+1]+90;
r[n-1]=0;
da(r,sa,n,180);
calheight(r,sa,n-1);
int l=1,r=n>>1;
while(l<r){
int mid=l+r+1>>1;
if(check(n,mid)) l=mid;
else r=mid-1;
}
if(l>=4) printf("%d\n",l+1);
else puts("0");
}
return 0;
}