POJ 1743 后缀数组+分组二分
2014-01-27 17:25
316 查看
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
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 16365 | Accepted: 5636 |
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 后缀数组简单应用,求出sa,rank,height数组,然后二分答案,判断可行性。 代码:
/* *********************************************** Author :xianxingwuguan Created Time :2014-1-27 10:57:50 File Name :1.cpp ************************************************ */ #pragma comment(linker, "/STACK:102400000,102400000") #include <stdio.h> #include <string.h> #include <iostream> #include <algorithm> #include <vector> #include <queue> #include <set> #include <map> #include <string> #include <math.h> #include <stdlib.h> #include <time.h> using namespace std; const int maxn=600010; /* * sa 数组: 排第几的是谁 * rank数组:谁排第几。 * height[i]:sa[i],sa[i-1]的最长公共前缀。 * */ int str[maxn],sa[maxn],rank[maxn],height[maxn],t1[maxn],t2[maxn],c[maxn]; void da(int *str,int n,int m) { int i,j,k,p,*x=t1,*y=t2; for(i=0;i<m;i++)c[i]=0; for(i=0;i<n;i++)c[x[i]=str[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(k=1;k<=n;k<<=1) { 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); x[sa[0]]=0; p=1; for(i=1;i<n;i++) x[sa[i]]=y[sa[i]]==y[sa[i-1]]&&y[sa[i]+k]==y[sa[i-1]+k]?p-1:p++; if(p>=n)break; m=p; } } void calheight(int *str,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--; j=sa[rank[i]-1]; while(str[i+k]==str[j+k])k++; height[rank[i]]=k; } } bool judge(int *sa,int n,int k) { int i,mx=sa[1],mn=sa[1]; for(i=2;i<=n;i++) { if(height[i]<k)mx=mn=sa[i]; else { if(sa[i]<mn)mn=sa[i]; if(sa[i]>mx)mx=sa[i]; if(mx-mn>k)return 1; } } return 0; } int main() { //freopen("data.in","r",stdin); //freopen("data.out","w",stdout); int i,j,k,m,n; while(~scanf("%d",&n)&&n) { scanf("%d",&j); for(i=0;i<n-1;i++) { scanf("%d",&k); str[i]=k-j+100; j=k; } str[n-1]=0; da(str,n,200); calheight(str,n-1); int l=1,r=n/2; while(l<r) { m=(l+r+1)>>1; if(!judge(sa,n-1,m))r=m-1; else l=m; } if(l>=4)l++;else l=0; printf("%d\n",l); } return 0; }
相关文章推荐
- POJ 3261 后缀数组分组二分
- poj 3789 Milk Patterns(后缀数组+二分答案+height数组分组)
- POJ3294---Life Forms(后缀数组,二分+给后缀分组)
- LA 3268 号码簿分组(最大流+二分)
- 51nod 1128 正整数分组 V2(二分)
- SPOJ220---Relevant Phrases of Annihilation(后缀数组+二分,对后缀分组)
- poj 1743 Musical Theme(后缀数组+二分答案+height数组分组)
- 51nod 1128 正整数分组 V 二分+贪心
- URAL 1156 Two Rounds (DFS二分染色 + 分组背包)
- SPOJ220---Relevant Phrases of Annihilation(后缀数组+二分,对后缀分组)
- 《机器学习实战》学习笔记-[14]-无监督学习-利用二分K-均值聚类对未标注数据分组
- SDUT 2776 小P的故事——神奇的分组 二分
- poj 1743 Musical Theme 求不可重叠最长重复子串(二分答案+height分组)
- 计蒜客-二分匹配习题 男女分组
- HDU 4033 Regular Polygon(二分+高精度)
- ThinkPHP 项目分组URL生成与URL访问
- 2015 UESTC 搜索专题D题 基爷的中位数 二分
- [POJ 1759] Garland (二分)
- hive 分组+组内排序 , 求topN
- Hadoop之MapReduce中的排序和分组(笔记11)