您的位置:首页 > 其它

Musical Theme POJ - 1743 后缀数组

2017-09-15 18:14 344 查看
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.

在学习过后缀数组的前提下仍让想了这个题目好几天,一直在想加上同一个数怎么处理。

想不出来看了一下题解,原来都后一项与前一项的差- -

例如,如果求出来最长的是4,那么原来串中最长的一定是5,以此类推。

这样的话二分出来的答案+1就可以了。

#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<cstdio>
#include<cstring>
#include<vector>
#include<cmath>
#include<complex>
#include<queue>
using namespace std;
const int MAXN=1e5+10;
int s[MAXN],s1[MAXN];
int t1[MAXN],t2[MAXN],cc[MAXN],x[MAXN],sa[MAXN],Rank[MAXN],height[MAXN];
int len;
bool cmp(int *y,int a,int b,int k)
{
int a1=y[a];
int b1=y[b];
int a2=a+k>=len ? -1:y[a+k];
int b2=b+k>=len ? -1:y[b+k];
return a1==b1 && a2==b2;
}
void make_sa()
{
int *x=t1,*y=t2;
int m=200;
for(int i=0; i<m; i++) cc[i]=0;
for(int i=0; i<len; i++) ++cc[x[i]=s[i]];
for(int i=1; i<m; i++) cc[i]+=cc[i-1];
for(int i=len-1; i>=0; i--) sa[--cc[x[i]]]=i;
for(int k=1; k<=len; k<<=1)
{
int p=0;
for(int i=len-k; i<len; i++) y[p++]=i;
for(int i=0; i<len; i++)
if( sa[i]>=k ) y[p++]=sa[i]-k;
for(int i=0; i<m; i++) cc[i]=0;
for(int i=0; i<len; i++) ++cc[x[y[i]]];
for(int i=1; i<m; i++) cc[i]+=cc[i-1];
for(int i=len-1; i>=0; i--) sa[--cc[x[y[i]]]]=y[i];
swap(x,y);
m=1;
x[sa[0]]=0;
for(int i=1; i<len; i++)
x[sa[i]]=cmp(y,sa[i],sa[i-1],k) ? m-1:m++;

if( m>=len ) break;
}
}
void make_height()
{
for(int i=0; i<len; i++) Rank[sa[i]]=i;
height[0]=0;
int k=0;
for(int i=0; i<len; i++)
{
if(!Rank[i]) continue;
int j=sa[Rank[i]-1];
if(k) k--;
while(s[i+k]==s[j+k]) k++;
height[Rank[i]]=k;
}
}
bool judge(int lim)
{
int up=0,down=0;
for(int i=0; i<len; i++)
{
if(height[i]<lim)
up=down=sa[i];
else
{
up=min(up,sa[i]);
down=max(down,sa[i]);
if(down-up>=lim)
return 1;
}
}
return 0;
}
int main()
{
while(scanf("%d",&len)!=EOF&&len)
{

int l=4,r=len/2;
for(int i=0; i<len; i++)
scanf("%d",&s1[i]);
if(len<10)
{
printf("0\n");
continue;
}
for(int i=0; i<len-1; i++)
s[i]=s1[i+1]-s1[i]+90;
len--;
make_sa();
make_height();
int ans=0;
while(l<=r)
{
int mid=(l+r)/2;
if(judge(mid))
{
l=mid+1;
ans=mid;
}
else
{
r=mid-1;
}

}
if(ans)
printf("%d\n",ans+1);
else
printf("0\n");
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: