题目链接:点击打开链接
Musical Theme
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 24047 | Accepted: 8130 |
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.
Source
题意:有N(1 <= N <=20000)个音符的序列来表示一首乐曲,每个音符都是1..88范围内的整数,现在要找一个重复的主题。“主题”是整个音符序列的一个子串,它需要满足如下条件:
思路:这个题是求重复出现的可重叠子串的最大长度。明显要用二分枚举长度,再用后缀数组分组求。要注意的是,只要相邻整数的差值相同,就可以视为相同主题,我们可以把原来的数组转换为差值数组,由于差值可能为负,我们把他们都加上89。即r[i]=r[i]-是r[i-1]+89;这样就和POJ3261牛奶那道题非常相似了,看代码:
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cstdlib>
using namespace std;
int wa[200005],wb[200005],wv[200005],ws[200005];
int r[200005],height[200005],rank1[200005],sa[200005];char s[200005];
int n;
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++) 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*=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];
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;
}
void calheight(int *r,int *sa,int n){
int i,j,k=0;
for(i=1;i<=n;i++) rank1[sa[i]]=i;
for(i=0;i<n;height[rank1[i++]]=k)
for(k?k--:0,j=sa[rank1[i]-1];r[i+k]==r[j+k];k++);
return;
}
int check(int x)
{
int i;
int Min=sa[1],Max=sa[1];
for(i=2;i<=n;i++)
{
if(height[i]>=x)
{
Min=min(Min,min(sa[i-1],sa[i]));//注意这里的比较要有sa[i-1],因为height数组本来就是相邻两个子串相同的字符个数。
Max=max(Max,max(sa[i-1],sa[i]));
if(Max-Min>=x) return 1;//如果差值大于x,说明这个长度存在不重叠的子串,返回1
}
else
{
Min=Max=sa[i];
}
}
return 0;
}
int main()
{
int i;
while(scanf("%d",&n)!=EOF&&n!=0)
{
for(i=0;i<n;i++)
scanf("%d",&r[i]);
n--;
for(i=0;i<n;i++)
{
r[i]=r[i+1]-r[i]+89;
}
r[n]=0;//这里不能忘了
//将后一个数和前一个数的差值构造为一个新的数组
da(r,sa,n+1,300);
calheight(r,sa,n);
int l=1,r=n,mid,ans=-1;
while(l<=r)
{
mid=(l+r)/2;
if(check(mid))
{
ans=mid;
l=mid+1;
}
else r=mid-1;
}//二分找最大长度
ans++;//注意这里ans要加1
if(ans>=5) printf("%d\n",ans);
else printf("0\n");
}
}