POJ1743 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.

 

 

 

 

 

 

题解

٩(๑>◡<๑)۶人生第道后缀数组٩(๑>◡<๑)۶(重新开始省选的第一道)

差分一下

就变成选两个长度大于等于5的不重叠子串

建一个SA(后缀数组)

二分答案一下

就只需要检查height值满足条件的最远的两个端点了

代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define N 20005
int *sa,*rk,*nsa,*nrk,arr[4][N],b[N],h[N];
int a[N],n;
void SA()
{
	memset(arr,0,sizeof(arr));
	memset(b,0,sizeof(b));
	memset(h,0,sizeof(h));
	sa=arr[0];rk=arr[1];nsa=arr[2];nrk=arr[3];
	int i,j,k;
	for(i=1;i<=n;i++)b[a[i]]++;
	for(i=1;i<=20000;i++)b[i]+=b[i-1];
	for(i=1;i<=n;i++)sa[b[a[i]]--]=i;
	for(i=1;i<=n;i++)rk[sa[i]]=rk[sa[i-1]]+(a[sa[i]]!=a[sa[i-1]]);
	for(k=1;k<=n&&rk[sa[n]]<n;k<<=1){
		for(i=1;i<=n;i++)b[rk[sa[i]]]=i;
		for(i=n;i>=1;i--)if(sa[i]>k)nsa[b[rk[sa[i]-k]]--]=sa[i]-k;
		for(i=n-k+1;i<=n;i++)nsa[b[rk[i]]--]=i;
		for(i=1;i<=n;i++)nrk[nsa[i]]=nrk[nsa[i-1]]+(rk[nsa[i]]!=rk[nsa[i-1]]||rk[nsa[i]+k]!=rk[nsa[i-1]+k]);
		swap(sa,nsa);swap(rk,nrk);
	}
	for(i=1,k=0;i<=n;i++){
		if(rk[i]==1)h[1]=0;
		else{
			if(k)k--;
			for(j=sa[rk[i]-1];a[i+k]==a[j+k];k++);
			h[rk[i]]=k;
		}
	}
}
bool check(int mid)
{
	int mx,mi,i;
	for(i=2;i<=n;i++){
		if(h[i]<mid)
			mx=mi=sa[i];
		else{
			mx=max(mx,sa[i]);
			mi=min(mi,sa[i]);
			if(mx-mi>mid)return 1;
		}
	}
	if(mx-mi>mid)return 1;
	return 0;
}
int main()
{
	int i,l,r,mid;
	while(1){
		memset(a,0,sizeof(a));
		scanf("%d",&n);
		if(!n)return 0;
		n--;
		for(i=0;i<=n;i++)scanf("%d",&a[i]);
		for(i=n;i>=1;i--)a[i]=a[i]-a[i-1]+88;
		SA();
		l=1;r=n;
		while(l<r){
			mid=(l+r)>>1;
			if(check(mid))
				l=mid+1;
			else
				r=mid;
		}
		if(l>=5) printf("%d\n",l);
		else printf("0\n");
	}
}

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值