PKU1743 Musical Theme 楼教主男人八题之一

本文介绍了一种使用后缀数组和二分查找技术来检测音乐旋律中最长重复主题的高效算法。该算法首先将旋律转换为整数序列,然后通过构建后缀数组来查找可能的主题候选。通过二分查找优化搜索过程,确保找到最长且不重叠的主题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

Musical Theme
Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 6021 Accepted: 2127

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

Hint

Use scanf instead of cin to reduce the read time.
后缀数组+二分=轻松加愉快!
#include<iostream>
#include<string.h>
#include<cmath>
#include<memory.h>
#include<algorithm>
using namespace std;
#define N 20010
int s[N]; // N > 256
int n, ranks[N], sa[N], height[N],tmp[N], top[N];
void makesa(){ // O(N * log N)
int i, j, len, na;
na = (n < 256 ? 256 : n);
memset(top, 0, na * sizeof(int));
for (i = 0; i < n ; i++) top[ ranks[i] = s[i] & 0xff ]++;
for (i = 1; i < na; i++) top[i] += top[i - 1];
for (i = 0; i < n ; i++) sa[ --top[ ranks[i] ] ] = i;
for (len = 1; len < n; len <<= 1) {
for (i = 0; i < n; i++) {
j = sa[i] - len; if (j < 0) j += n;
tmp[ top[ ranks[j] ]++ ] = j;
}
sa[ tmp[ top[0] = 0 ] ] = j = 0;
for (i = 1; i < n; i++) {
if (ranks[ tmp[i] ] != ranks[ tmp[i-1] ] ||
ranks[ tmp[i]+len ]!=ranks[ tmp[i-1]+len ])
top[++j] = i;
sa[ tmp[i] ] = j;
}
memcpy(ranks, sa , n * sizeof(int));
memcpy(sa , tmp, n * sizeof(int));
if (j >= n - 1) break;
}
}
void lcp(){ // O(4 * N)
int i, j, k;
for (j = ranks[height[i=k=0]=0]; i < n - 1; i++, k++)
while (k >= 0 && s[i] != s[ sa[j-1] + k ])
height[j] = (k--), j = ranks[ sa[j] + 1 ];
}
bool check(int mid)
{
int mi=0xfffffff,ma=0;
int i;
for(i=1;i<n;i++)
if(height[i]>=mid)
{
do
{
ma=max(ma,max(sa[i],sa[i-1]));
mi=min(mi,min(sa[i],sa[i-1]));
i ++;
}while(i<n&&height[i]>=mid);
if(ma-mi>=mid)
return true;
}
return false;
}
int main()
{
int i,j;
//char ss[N];
while(scanf("%d",&n),n)
{
for(i=0;i<n;i++)
scanf("%d",&s[i]);
//s[0]=s[i]+88;
for(i=1;i<n;i++)
s[i-1]=s[i]-s[i-1]+88;
s[n-1]=255;
//n++;
//s[0]=0;
s[n++]=0;
makesa();
lcp();
int ans=-1;
int l=4,r=n-1,mid;
while(l <= r)
{
mid=(l+r)>>1;
if(check(mid))
{
ans=mid;
l=mid+1;
}
else
r=mid-1;
}
printf("%d/n",ans+1);
}
return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值