您的位置:首页 > 其它

pku 1743 Musical Theme 最长重复不重叠子串 后缀数组

2011-03-11 21:15 429 查看
Musical Theme

Time Limit: 1000MSMemory Limit: 30000K
Total Submissions: 7145Accepted: 2539
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<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
///后缀数组 倍增算法
const int maxn=500000;
char str[maxn];
int wa[maxn],wb[maxn],wv[maxn],wn[maxn],a[maxn],sa[maxn];
int cmp(int* r,int a,int b,int l)
{return r[a]==r[b]&&r[a+l]==r[b+l];}
/**n为字符串长度,m为字符的取值范围,r为字符串。后面的j为每次排
序时子串的长度*/
void DA(int* r,int* sa,int n,int m)
{
int i,j,p,*x=wa,*y=wb,*t;
///对R中长度为1的子串进行基数排序
for(i=0;i<m;i++)wn[i]=0;
for(i=0;i<n;i++)wn[x[i]=r[i]]++;
for(i=1;i<m;i++)wn[i]+=wn[i-1];
for(i=n-1;i>=0;i--)sa[--wn[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++)wn[i]=0;
for(i=0;i<n;i++)wn[wv[i]]++;
for(i=1;i<m;i++)wn[i]+=wn[i-1];
for(i=n-1;i>=0;i--)sa[--wn[wv[i]]]=y[i];
///当p=n的时候,说明所有串都已经排好序了
///在第一次排序以后,rank数组中的最大值小于p,所以让m=p
for(t=x,x=y,y=t,p=1,x[sa[0]]=0,i=1;i<n;i++)
x[sa[i]]=cmp(y,sa[i-1],sa[i],j)?p-1:p++;
}
return;
}
///后缀数组 计算height数组
/**
height数组的值应该是从height[1]开始的,而且height[1]应该是等于0的。
原因是,+因为我们在字符串后面添加了一个0号字符,所以它必然是最小的
一个后缀。而字符串中的其他字符都应该是大于0的(前面有提到,使用倍
增算法前需要确保这点),所以排名第二的字符串和0号字符的公共前缀
(即height[1])应当为0.在调用calheight函数时,要注意height数组的范
围应该是[1..n]。所以调用时应该是calheight(r,sa,n)
而不是calheight(r,sa,n+1)。*/
int rank[maxn],height[maxn];
void calheight(int* r,int* sa,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];r[i+k]==r[j+k];k++);
return;
}
int main()
{
//后缀数组 倍增算法 使用方法
/**
在使用倍增算法前,需要保证r数组的值均大于0。然后要在原字
符串后添加一个0号字符,具体原因参见罗穗骞的论文。这时候,
若原串的长度为n,则实际要进行后缀数组构建的r数组的长度应
该为n+1.所以调用DA函数时,对应的n应为n+1.*/
/* int n=strlen(str);//str 待处理字符串
for(int i=0;i<n;i++) a[i]=(int)str[i];
a
=0;
DA(a,sa,n+1,256);
calheight(a,sa,n);*/
//....................................
int num[maxn];
int n,m;
const int inf=(1<<28);
while(scanf("%d",&m)==1&&m)
{
for(int i=0;i<m;i++) scanf("%d",&num[i]);
n=m-1;
///这里构造很是巧妙
for(int i=0;i<n;i++) a[i]=num[i+1]-num[i]+100;//positive
a
=0;
DA(a,sa,n+1,256);
calheight(a,sa,n);

/*题意:
给出一个旋律,用n个数字[1,88]表示其音符,问它最长的主题长度是
多少。一个旋律的主题是一段至少出现过两次的不重叠音乐片段。所谓
重复出现,包括一段音乐全体加上某个数后再次出现。如1 2 3 4 5和5
6 7 8 9是同一个音乐片段。主题长度至少为5.无解输出0
*/
///最长重复不重叠子串
/**
首先,根据height数组,将后缀分成若干组,使得每组后缀中,
后缀之间的height值不小于k。这样分组之后,不难看出,如果
某组后缀数量大于1,那么它们之中存在一个公共前缀,其长度
为它们之间的height值的最小值。而我们分组之后,每组后缀之
间height值的最小值大于等于k。所以,后缀数大于1的分组中,
有可能存在满足题目限制条件的长度不小于k的子串。只要判断
满足题目限制条件成立,那么说明存在长度至少为k的合法子串。
对于本题,限制条件是不重叠,判断的方法是,一组后缀中,起
始位置最大的后缀的起始位置减去起始位置最小的后缀的起始位
置>=k。满足这个条件的话,那么这两个后缀的公共前缀不但出现
两次,而且出现两次的起始位置间隔大于等于k,所以不会重叠*/

int l=0,r=n,mid,ans=0;
while(l<r)
{
int _min=inf,_max=-inf;
mid=(l+r)>>1;
int flag=0;
for(int i=1;i<=n;i++)
{
if(height[i]>=mid)
{
_max=max(_max,sa[i-1]);_max=max(_max,sa[i]);
_min=min(_min,sa[i-1]);_min=min(_min,sa[i]);
if(_max-_min>mid)
{
ans=max(ans,mid+1),flag=1;
break;
}
else if(_max-_min==mid)//important
{
ans=max(ans,mid),flag=1;
}
}
else
{
_min=inf,_max=-inf;
}
}
if(flag) l=mid+1;
else r=mid;
}
if(ans>=5) printf("%d/n",ans);//这道题目要求要大于5
else printf("%d/n",0);
}
return 0;
}
/*
9
1 2 3 4 5 6 7 8 9
*/

/// 1 1 1 1 1 1 1 1 h[5]=4,h[8]=7;
/*
1 7
1 1 6
1 1 1 5
1 1 1 1 4
1 1 1 1 1 3
1 1 1 1 1 1 2
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 0
*/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: