PKU 1743(求最长重复但不重叠子串,后缀数组 + 二分枚举答案)
2010-08-15 08:50
453 查看
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.
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 5545 | Accepted: 1936 |
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 <stdio.h> #define max(a,b) (a>b?a:b) #define min(a,b) (a>b?b:a) const int maxn = 1000000; int wa[maxn],wb[maxn],wv[maxn],ws[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];} void da(int *r,int *sa,int n,int m) { int i,j,p,*x=wa,*y=wb,*t; for(i=0;i<m;i++) ws[i]=0; for(i=0;i<n;i++) ws[x[i]=r[i]]++; for(i=1;i<m;i++) ws[i]+=ws[i-1]; for(i=n-1;i>=0;i--) sa[--ws[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++) ws[i]=0; for(i=0;i<n;i++) ws[wv[i]]++; for(i=1;i<m;i++) ws[i]+=ws[i-1]; for(i=n-1;i>=0;i--) sa[--ws[wv[i]]]=y[i]; 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; } 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++); } int n; bool ask(int len) { int i; int MAX = n,MIN = 0; for(i=1;i<=n;i++) { if(height[i]<len) { MAX = sa[i]; MIN = sa[i]; } else { MAX = max(MAX,sa[i]); MAX = max(MAX,sa[i-1]); MIN = min(MIN,sa[i]); MIN = min(MIN,sa[i-1]); if(MAX - MIN > len) return true; } } return false; } int main() { int i,x,temp; while (scanf("%d",&n),n) { scanf("%d",&x); temp = x; for(i=1;i<n;i++) { scanf("%d",&x); a[i-1] = x - temp + 100; temp = x; } a[n-1] = 0; n--; da(a,sa,n+1,250); calheight(a,sa,n); if(!ask(4)) { printf("0/n"); continue; } int low,high,mid; low = 0; high = n ; while (low<high) //二分枚举 { mid = (low+high+1)>>1; if(ask(mid)) low = mid; else high = mid-1; } printf("%d/n",low +1); } return 0; }
相关文章推荐
- PKU1743(Musical Theme)求不可重叠最长重复子串(后缀数组+二分)
- POJ1743 Musical Theme,后缀数组,最长重复不重叠子串,二分检索答案
- poj1743 Musical Theme(后缀数组--不可重叠最长重复子串+二分)
- POJ 1743 Musical Theme (后缀数组加二分求不可重叠最长重复子串)
- pku 1743 Musical Theme 最长重复不重叠子串 后缀数组
- POJ - 1743 Musical Theme (后缀数组求不可重叠最长重复子串)
- poj 1743 后缀数组 求最长不重叠重复子串
- POJ 1743 Musical Theme(后缀数组[不可重叠最长重复子串])
- Poj 1743——Musical Theme——————【后缀数组,求最长不重叠重复子串长度】
- Poj 1743 Musical Theme (后缀数组 不可重叠最长重复子串)
- poj 1743 男人八题之后缀数组求最长不可重叠最长重复子串
- POJ 1743 Musical Theme (后缀数组,求最长不重叠重复子串)
- POJ-1743 Musical Theme (后缀数组 不重叠最长重复子串)
- 后缀数组 不重叠最长重复子串 POJ 1743
- POJ 1743 Musical Theme (后缀数组,求最长不重叠重复子串)
- POJ 1743 Musical Theme(后缀数组求不可重叠最长重复子串)
- poj1743 后缀数组求不可重叠的重复出现的子串最长长度
- POJ 1743 Musical Theme (后缀数组,求最长不重叠重复子串)
- poj 1743 字符串 后缀数组 不可重叠最长重复子串
- poj3261(后缀数组--可重叠的k次最长重复子串+二分+离散化)