您的位置:首页 > 其它

POJ1743 Musical Theme(后缀数组,高度数组分组)

2017-07-30 13:46 399 查看
Musical Theme

Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 29855 Accepted: 9996
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<cstdio>
#include<cstring>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;
const int MAXN=20000+10;
const int INF=1e9+7;
int n,k;
int rnk[MAXN+1];
int tmp[MAXN+1];

//比较(rnk[i],rnk[i+k])和(rnk[j],rnk[j+k])
bool compare_sa(int i,int j){
if(rnk[i]!=rnk[j])
return rnk[i]<rnk[j];
else{
int ri=i+k<=n?rnk[i+k]:-1;
int rj=j+k<=n?rnk[j+k]:-1;
return ri<rj;
}
}
//rank用来记录字符串的排序,sa用来记录开头字符的位置,S用来记录字符串
//第一个通常是空字符串
void construct_sa(int *S,int *sa){
//初始长度为1,rank直接取字符的编码.
for(int i=0;i<=n;i++){
sa[i]=i;
rnk[i]=i<n?S[i]:-1;
}

//利用对长度为k的排序的结果对长度为2k的排序
for(k=1;k<=n;k*=2){
sort(sa,sa+n+1,compare_sa);

//先在tmp中临时储存新计算的rank,再转存回rank中
tmp[sa[0]]=0;
for(int i=1;i<=n;i++){
tmp[sa[i]]=tmp[sa[i-1]]+(compare_sa(sa[i-1],sa[i])?1:0);
}
for(int i=0;i<=n;i++){
rnk[i]=tmp[i];
}
}
}

//高度数组lcp的计算
void construct_lcp(int *S,int *sa,int *lcp){
for(int i=0;i<=n;i++) rnk[sa[i]]=i;
int h=0;
lcp[sa[0]]=0;
for(int i=0;i<n;i++){
//计算字符串中从位置i开始的后缀及其在后缀数组中的前一个后缀的lcp
int j=sa[rnk[i]-1];

//将h先减去首字母的1长度,在保持前缀相同的前提下不断地增加
if(h>0) h--;
for(;j+h<n&&i+h<n;h++){
if(S[j+h]!=S[i+h]) break;
}
lcp[rnk[i]-1]=h;
}
}

int sa[MAXN],lcp[MAXN];
int a[MAXN];
void init(){
memset(sa,0,sizeof sa);
memset(lcp,0,sizeof lcp);
memset(rnk,0,sizeof rnk);
memset(tmp,0,sizeof tmp);
}

bool C(int x){
int pre=1;
int MAX=0;
int MIN=INF;
for(int i=1;i<=n;i++){
MAX=max(sa[i],MAX);
MIN=min(sa[i],MIN);
if(lcp[i]<x||i==n){
if(MAX-MIN>=x&&i>pre){
return true;
}
pre=i;
MAX=0;
MIN=INF;
}
}
return false;
}

int main(){
while(scanf("%d",&n)!=EOF&&n){
for(int i=0;i<n;i++){
scanf("%d",a+i);
}
n--;
for(int i=0;i<n;i++){
a[i]=a[i+1]-a[i]+100;
}
a
=0;
init();
construct_sa(a, sa);
construct_lcp(a, sa, lcp);
int l=0,r=n+1;
while(r>l+1){
int m=(l+r)>>1;
if(C(m)){
l=m;
}else{
r=m;
}
}
l++;
if(l>=5){
printf("%d\n",l);
}else{
printf("0\n");
}
}
}

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