poj 1743 Musical Theme(后缀数组+二分)
2014-04-24 09:59
323 查看
Musical Theme
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
Sample Output
Hint
Use scanf instead of cin to reduce the read time.
Source
LouTiancheng@POJ
题意:求数列作差后的数列,最长的相同子串,且2个串不可重叠,且长度要大于等于5
题解:对数列最差后求后缀数组的height数组,然后二分长度值在height满足长度的情况下判定sa【i】和sa【j】的差值是否重叠
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 17199 | Accepted: 5912 |
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
题意:求数列作差后的数列,最长的相同子串,且2个串不可重叠,且长度要大于等于5
题解:对数列最差后求后缀数组的height数组,然后二分长度值在height满足长度的情况下判定sa【i】和sa【j】的差值是否重叠
#include<stdio.h> #include<string.h> #define maxn 20008 int bucket[maxn],rankx[maxn],ranky[maxn]; int sa[maxn],height[maxn],r[maxn],num[maxn]; int myabs(int x){ return x<0?-x:x; } int cmp(int *r,int a,int b,int l) { return r[a]==r[b]&&r[a+l]==r[b+l]; } void suffix_array(int *s,int *sa,int n,int m) { int i,j,p,*x=rankx,*y=ranky,*t; for(i=0;i<m;i++) bucket[i]=0; for(i=0;i<n;i++) bucket[x[i]=s[i]]++; for(i=1;i<m;i++) bucket[i]+=bucket[i-1]; for(i=n-1;i>=0;i--) sa[--bucket[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<m;i++) bucket[i]=0; for(i=0;i<n;i++) bucket[x[y[i]]]++; for(i=1;i<m;i++) bucket[i]+=bucket[i-1]; for(i=n-1;i>=0;i--) sa[--bucket[x[y[i]]]]=y[i]; t=x,x=y,y=t; for(p=i=1,x[sa[0]]=0;i<n;i++) x[sa[i]]=cmp(y,sa[i-1],sa[i],j)?p-1:p++; } } void calculate_height(int *r,int *sa,int *x,int *height,int n) { int i,j,k=0; for(i=0;i<=n;i++) x[sa[i]]=i; for(i=0;i<n;height[x[i++]]=k) { if(x[i]==0) continue; for(k?k--:0,j=sa[x[i]-1];r[i+k]==r[j+k];k++); } } int check(int x,int n) { for(int i=2;i<=n;i++) { for(int j=i-1;j>=2&&height[j+1]>=4&&height[j+1]>=x-1;j--) if(myabs(sa[i]-sa[j])>=x) return 1; } return 0; } int main() { int n,i; while(scanf("%d",&n),n) { for(i=0;i<n;i++) { scanf("%d",num+i); if(i) r[i-1]=num[i-1]-num[i]+90; } r[n-1]=0; suffix_array(r,sa,n,200); calculate_height(r,sa,rankx,height,n-1); int ans=4,left=4,height=n; while(left<=height) { int mid=(left+height)>>1; if(check(mid,n)) ans=mid,left=mid+1; else height=mid-1; } if(ans<=4) printf("0\n"); else printf("%d\n",ans); } return 0; }
相关文章推荐
- POJ 1743 Musical Theme(后缀数组+二分)
- POJ1743 Musical Theme,后缀数组,最长重复不重叠子串,二分检索答案
- POJ1743---Musical Theme(后缀数组+二分)
- poj1743 Musical Theme(后缀数组--不可重叠最长重复子串+二分)
- Poj 1743 Musical Theme(后缀数组+二分答案)
- poj 1743 Musical Theme(后缀数组+二分答案+height数组分组)
- poj1743 Musical Theme(后缀数组+二分答案)
- poj 1743 Musical Theme(后缀数组、二分)
- POJ 1743 Musical Theme(后缀数组+二分答案)
- poj 1743 Musical Theme(最长不重叠重复子串 后缀数组+二分)
- POJ-1743-Musical Theme(后缀数组+二分)
- POJ1743---Musical Theme (后缀数组+二分)
- POJ 1743 Musical Theme(后缀数组 求最长不重复子串)
- poj 1743 Musical Theme (后缀数组+二分答案)
- POJ 1743 Musical Theme(后缀数组[不可重叠最长重复子串])
- POJ 1743 Musical Theme (后缀数组 + 二分)
- poj 1743 Musical Theme(不可重叠的最长重复子串,后缀数组)
- poj1743 Musical Theme(后缀数组|后缀自动机)
- POJ 1743 Musical Theme(后缀数组求不可重叠最长重复子串)
- POJ 1743 Musical Theme (二分后缀数组LCP)