您的位置:首页 > 其它

Poj 1743——Musical Theme——————【后缀数组,求最长不重叠重复子串长度】

2015-10-16 11:36 525 查看
Musical Theme

Time Limit: 1000MSMemory Limit: 30000K
Total Submissions: 22499Accepted: 7679
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.
Source

LouTiancheng@POJ

题目大意:让你找最长的音乐主题,主题:1.需要最少由5个音符组成 2.音符之间不重叠 3.同一个主题可以是由同时升或降的一段音调组成(如 1 2 3 4 5 10 11 12 13 14 15 是一个音乐主题)。

解题思路:相邻的音符之间做差,再加上一个不会让跟差值做和后出现零或者负值的整数,在最后赋值为0,表示结束位置。然后就直接模板套就ok了。

#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<stdlib.h>
using namespace std;
const int maxn = 1e5+200;
const int INF = 0x3f3f3f3f;
int a[maxn],s[maxn];
int sa[maxn], t[maxn], t2[maxn], c[maxn];
int rank[maxn], height[maxn];
void build_sa(int n, int m){
int i,*x = t, *y = t2;
//初始化,基数排序
for(i = 0; i < m; i++) c[i] = 0;
for(i = 0; i < n; i++) c[x[i] = s[i]]++;
for(i = 1; i < m; i++) c[i] += c[i-1];
for(i = n-1; i >= 0; i--) sa[--c[x[i]]] = i;
for(int k = 1; k <= n; k <<= 1){
int p = 0;
for(i = n-k; i < n; i++) y[p++] = i;
for(i = 0; i < n; i++) if(sa[i] >= k) y[p++] = sa[i]-k;
for(i = 0; i < m; i++) c[i] = 0;
for(i = 0; i < n; i++) c[x[y[i]]]++;
for(i = 1; i < m; i++) c[i] += c[i-1];
for(i = n-1; i >= 0; i--) sa[--c[x[y[i]]]] = y[i];
swap(x,y);
p = 1; x[sa[0]] = 0;
for(i =1; i < n; i++)
x[sa[i]] = y[sa[i-1]] == y[sa[i]] && y[sa[i-1]+k] ==y[sa[i]+k] ? p-1:p++;
if(p >= n) break;
m = p;
}
return ;
}
void getheight(int n) {

int i, j, k = 0;
for(i = 0; i < n; i++) {
rank[sa[i]] = i;
}
for(i = 0; i < n; i++) {
if(k) k--;
int j = sa[rank[i]-1];
while(s[i+k] == s[j+k]){
k++;
}
height[rank[i]] = k;
}
}
bool check(int mid , int n){
int mi=INF , mx = 0;
for(int i=2;i<=n+1;i++){
if(i==n+1 || height[i] < mid){
//   printf("%d %d %d\n",i,height[i],mid);
mi = min(mi, sa[i-1]);
mx = max(mx, sa[i-1]);
if(mx - mi >= mid){
return true;
}
mx = 0;mi = INF;
}
else if(height[i] >= mid){
mi= min(mi,sa[i-1]);
mx= max(mx,sa[i-1]);
}
}
return false;
}
int main(){
int n;
while(scanf("%d",&n)!=EOF && n ){
for(int i=0;i<n;i++){
scanf("%d",&a[i]);
}
if(n<10){
puts("0");
continue;
}
for(int i=0;i<n-1;i++){
s[i]=a[i+1]-a[i]+89;
}
s[n-1]=0;
//        for(int i=0;i<=n;i++){
//            printf("%d ",s[i]);
//        }puts("");
build_sa(n,200);
//        for(int i=0;i<=n;i++){
//            printf("%d %d-+-+-+\n",i,sa[i]);
//        }
getheight(n);
//        for(int i=0;i<n;i++){
//            printf("%d %d------------------\n",i,height[i]);
//        }
int l=4,r=n/2+1,mid;
int ans = 0;
while(l<=r){
mid=(l+r)/2;
if(check(mid , n)){
l=mid+1;
ans=max(mid,ans);
}else{
r=mid-1;
}
}
if(ans<4) puts("0");
else  printf("%d\n",ans+1);
}
return 0;
}

/*
10
1 1 1 1 1 1 1 1 1 1

*/


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