Musical Theme
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.
题目链接:http://poj.org/problem?id=1743
题目大意:给长度为n的序列, 问最长的不重叠相似子序列是多少(长度不小于5)。相似不要求完全一样,可以是子序列中所有数+x或-x与另一个不重叠的子序列完全相同。
思路:子序列可以加上或减去一个数使相同,那么可以直接转化为差相同,所以先预处理出数列的差 ai - ai-1,然后问题就变成了不重叠的重复最长子序列。二分不重叠的重复最长子序列,在一段连续的区间 [ l , r ] 满足 hi >=x 且区间中 maxsai -minsai >=x,说明有长度为x的不重叠的重复子序列
代码:
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define ll long long
const int N=200005;
int sa[N],rak[N],h[N],tax[N],tp[N],a[N],n,m;
int s[N];
bool cmp(int *f,int x,int y,int w){return f[x]==f[y]&&f[x+w]==f[y+w]; }
void Rsort()
{
for(int i=0;i<=m;i++) tax[i]=0;
for(int i=1;i<=n;i++) tax[rak[i]]++;
for(int i=1;i<=m;i++) tax[i]+=tax[i-1];
for(int i=n;i>=1;i--) sa[tax[rak[tp[i]]]--]=tp[i];
}
void suffix()
{
for(int i=1;i<=n;i++) rak[i]=a[i],tp[i]=i;
m=200,Rsort();
for(int w=1,p=0,i;p<n;w+=w,m=p)
{
for(p=0,i=n-w+1;i<=n;i++) tp[++p]=i;
for(i=1;i<=n;i++) if(sa[i]>w)tp[++p]=sa[i]-w;
Rsort();
swap(rak,tp); rak[sa[1]]=p=1;
for(i=2;i<=n;i++) rak[sa[i]]=cmp(tp,sa[i],sa[i-1],w)?p:++p;
}
int j,k=0;
for(int i=1;i<=n;h[rak[i++]]=k)
for(k=k?k-1:k,j=sa[rak[i]-1];a[i+k]==a[j+k];++k);
}
int fun(int x)
{
int l=sa[1],r=sa[1];
for(int i=2;i<=n;i++)
{
if(h[i]>=x&&i<n)
{
l=min(l,sa[i]);
r=max(r,sa[i]);
continue;
}
if(r-l>=x) return 1;
l=r=sa[i];
}
return 0;
}
int main()
{
while(~scanf("%d",&n)&&n)
{
for(int i=0;i<n;i++)
{
scanf("%d",&s[i]);
if(i) a[i]=s[i]-s[i-1]+100;
}
--n;
suffix();
int l=4,r=n,ans=0;
while(l<=r)
{
int mid=(l+r)>>1;
if(fun(mid))
{
ans=mid;
l=mid+1;
}
else r=mid-1;
}
printf("%d\n",ans>=4?ans+1:0);
}
return 0;
}