POJ 1743 Musical Theme(不可重叠最长重复子串 后缀数组)
来源:互联网 发布:数据专家cnzz干什么的 编辑:程序博客网 时间:2024/05/20 02:27
Musical Theme
Time Limit: 1000MS Memory Limit: 30000KTotal Submissions: 30961 Accepted: 10341
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:
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!
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.
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
3025 27 30 34 39 45 52 60 69 79 69 60 52 45 39 34 30 26 22 1882 78 74 70 66 67 64 60 65 800
Sample Output
5
Hint
Use scanf instead of cin to reduce the read time.
Source
LouTiancheng@POJ
来自论文后缀数组 罗穗骞
给定一个字符串,求最长重复子串,这两个子串不能重叠。
算法分析:
这题比上一题稍复杂一点。 先二分答案, 把题目变成判定性问题: 判断是否
存在两个长度为 k 的子串是相同的,且不重叠。解决这个问题的关键还是利用height 数组。把排序后的后缀分成若干组,其中每组的后缀之间的height 值都不小于 k。例如,字符串为 “aabaaaab”,当 k=2 时,后缀分成了 4 组。
容易看出, 有希望成为最长公共前缀不小于k 的两个后缀一定在同一组。然后对于每组后缀,只须判断每个后缀的 sa 值的最大值和最小值之差是否不小于k。如果有一组满足,则说明存在,否则不存在。整个做法的时间复杂度为O(nlogn)。本题中利用 height 值对后缀进行分组的方法很常用,请读者认真体会。
#include <iostream>#include <string.h>#include <stdio.h>#include <algorithm>#include <queue>#include <math.h>#include <vector>using namespace std;const int maxn = 20100;int w[maxn],sa[maxn],rk[maxn],height[maxn],res[maxn],cnt[maxn];void getSa(int len,int up){ int *k=rk,*r=res,*id=height; for(int i=0;i<up;i++) cnt[i]=0; for(int i=0;i<len;i++) cnt[k[i]=w[i]]++; for(int i=0;i<up;i++) cnt[i+1]+=cnt[i]; for(int i=len-1;i>=0;i--){ sa[--cnt[k[i]]]=i; } int d=1,p=0; while(p<len){ for(int i=len-d;i<len;i++) id[p++]=i; for(int i=0;i<len;i++) if(sa[i]>=d) id[p++]=sa[i]-d; for(int i=0;i<len;i++) r[i]=k[id[i]]; for(int i=0;i<up;i++) cnt[i]=0; for(int i=0;i<len;i++) cnt[r[i]]++; for(int i=0;i<up;i++) cnt[i+1]+=cnt[i]; for(int i=len-1;i>=0;i--){ sa[--cnt[r[i]]]=id[i]; } p=0; swap(r,k); k[sa[0]]=p++; for(int i=0;i<len-1;i++){ if(sa[i]+d<len&&sa[i+1]+d<len&&r[sa[i]]==r[sa[i+1]]&&r[sa[i+1]+d]==r[sa[i]+d]) k[sa[i+1]]=p-1; else k[sa[i+1]]=p++; } if(p>=len) return; d*=2,up=p,p=0; }}void getHeight(int len){ for(int i=0;i<len;i++) rk[sa[i]]=i; int p=0; for(int i=0;i<len-1;i++){ int j=sa[rk[i]-1]; while(i+p<len&&j+p<len&&w[i+p]==w[j+p]) p++; height[rk[i]]=p; p=max(0,p-1); }}void getSuffix(int len){ w[len++]=0; getSa(len,333); getHeight(len);}int check(int k,int len){// int Max=-1,Min=999999;// for(int i=2;i<=len;i++){// if(height[i]<k) Max=Min=sa[i];// else{// if(Max<sa[i]) Max=sa[i];// if(Min>sa[i]) Min=sa[i];// if(Max-Min>k) return 1;// }// } int num=0; int ans[maxn*3]; for(int i=2;i<=len;i++){ if(height[i]>=k) ans[++num]=sa[i],ans[++num]=sa[i-1]; else{ if(num>0){ int max=-1,min=999999; for(int i=1;i<=num;i++){ if(max<ans[i]) max=ans[i]; if(min>ans[i]) min=ans[i]; } num=0; if(max-min>k) return 1; } } } if(num>0){ int max=-1,min=999999; for(int i=1;i<=num;i++){ if(max<ans[i]) max=ans[i]; if(min>ans[i]) min=ans[i]; } num=0; if(max-min>k) return 1; } return 0;}int main(){ int n; while(cin>>n){ if(n==0) break; int a[maxn]; for(int i=0;i<n;i++){ scanf("%d",&a[i]); } for(int i=0;i<n-1;i++){ w[i]=a[i+1]-a[i]+100; } getSuffix(n-1); int l=1,r=n; int mid; int ans=0; while(l<=r){ mid=(l+r)>>1; if(check(mid,n-1)){ ans=mid; l=mid+1; }else{ r=mid-1; } } if(ans<4) printf("0\n"); else printf("%d\n",ans+1); } return 0;}
阅读全文
0 0
- POJ 1743 Musical Theme(后缀数组求不可重叠最长重复子串)
- POJ 1743 Musical Theme (后缀数组加二分求不可重叠最长重复子串)
- POJ - 1743 - Musical Theme(后缀数组 - 不可重叠最长重复子串)
- POJ 1743 Musical Theme(不可重叠最长重复子串 后缀数组)
- Poj 1743 Musical Theme (后缀数组 不可重叠最长重复子串)
- poj 1743 Musical Theme (后缀数组 不可重叠最长重复子串)
- poj 1743 Musical Theme(不可重叠的最长重复子串,后缀数组)
- POJ 1743 Musical Theme ( 后缀数组 + 二分 不可重叠最长重复子串 )
- POJ - 1743 Musical Theme (后缀数组求不可重叠最长重复子串)
- POJ 1743 Musical Theme 后缀数组 不可重叠最长重复子串
- POJ 1743 Musical Theme(后缀数组[不可重叠最长重复子串])
- POJ 1743Musical Theme(后缀数组+不可重叠最长子串)
- hihoCoder 1407 后缀数组二·重复旋律2 && POJ 1743 Musical Theme(最长不可重叠重复子串问题)
- POJ 1743 Musical Theme (后缀数组,求最长不重叠重复子串)
- POJ 1743 Musical Theme (后缀数组,求最长不重叠重复子串)
- poj 1743 Musical Theme (不可重叠最长重复子串)
- poj1743 Musical Theme(后缀数组--不可重叠最长重复子串+二分)
- POJ 1743 Musical Theme 不可重叠最长重复字串(后缀数组)
- HDU1050-Moving Table
- 用jquery制作轮播图
- BZOJ2125: 最短路
- linux基础练习
- HTML知识点总结
- POJ 1743 Musical Theme(不可重叠最长重复子串 后缀数组)
- C#分页Dome
- 条件语句及循环语句基础
- hdu 1255 覆盖的面积
- Python 基础教程
- Partition List
- Android ObjectAnimator详解
- 将大于形参m且紧靠m的k个素数存入xx所指的数组中。 例如,若输入17, 5,则应输出:19, 23, 29, 31, 37。C语言编程题【21题】
- 说说事务的概念,在JDBC编程中处理事务的步骤。