您的位置:首页 > 其它

POJ 1743 Musical Theme (后缀数组加二分求不可重叠最长重复子串)

2014-08-27 21:36 543 查看
Musical Theme

Time Limit: 1000MSMemory Limit: 30000K
Total Submissions: 18590Accepted: 6371
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:

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

#include<iostream>
#include<cstdio>
#include<cmath>
#define N 20010
using namespace std;
int sa
,Rank
,wa
,wx
,wy
,num
,height
,wv
;
int cmp(int *r ,int a ,int b ,int j)
{
return r[a]==r[b]&&r[a+j]==r[b+j];
}
void da(int n , int m)
{
int i,j,p,*y=wy,*x=wx,*t;
for(i=0;i<m;i++)
wa[i]=0;
for(i=0;i<n;i++)
wa[x[i]=num[i]]++;
for(i=1;i<m;i++)
wa[i]+=wa[i-1];
for(i=n-1;i>=0;i--)
sa[--wa[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++)
wa[i]=0;
for(i=0;i<n;i++)
wa[wv[i]]++;
for(i=1;i<m;i++)
wa[i]+=wa[i-1];
for(i=n-1;i>=0;i--)
sa[--wa[wv[i]]]=y[i];
for(i=1,p=1,t=x,x=y,y=t,x[sa[0]]=0;i<n;i++)
x[sa[i]]=cmp(y,sa[i-1],sa[i],j)?p-1:p++;
}
}
void calheight(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];num[i+k]==num[j+k];k++);
}
bool ok(int k,int n)//判断是否可行
{
int maxn,minn;
maxn=minn=sa[1];
for(int i=2; i<=n; i++)
{
if(height[i]<k)
minn=maxn=sa[i];
else
{
minn=min(minn,sa[i]);
maxn=max(maxn,sa[i]);
if(maxn-minn>=k)
return 1;
}
}
return 0;
}
int answer(int n)
{
int l=0,r=n,mid,ans=0;
while(l<=r)//二分假定答案
{
mid=(l+r)>>1;
if(ok(mid,n))
{
ans=mid;
l=mid+1;
}
else
r=mid-1;
}
if(ans>=4)
return ans+1;
else
return 0;
}
int main()
{
int n;
while(cin>>n,n)
{
scanf("%d",&num[0]);
for(int i=1;i<n;i++)
{
scanf("%d",&num[i]);
num[i-1]=num[i-1]-num[i]+100;
}
num[n-1]=0;
if(n<10)
{
cout<<'0'<<endl;
continue;
}
da(n,200);
calheight(n-1);
int ans=answer(n-1);
cout<<ans<<endl;
}
return 0;
}


[/code]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: