Musical Theme+POJ+后缀数组
来源:互联网 发布:大数据市场 编辑:程序博客网 时间:2024/06/10 22:51
Musical Theme
Time Limit: 1000MS Memory Limit: 30000KTotal Submissions: 18583 Accepted: 6368
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
解决方案:求的是最长的出现过次数超过两次且最短距离为5不重叠的子串,该子串同时加上或减去一个数被认为是一样的。所以可用转成后一个数与前的一个的差的串,再求该子串。直接套用后缀数组模板,然后二分即可。(ps:POJ上的题目的数据太水了。)
code:#include<iostream>#include<cstdio>#include<cstring>#include<cmath>using namespace std;const int maxn=30000;int wa[maxn],wb[maxn],wv[maxn],WS[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++); return;}char text[maxn];int num[maxn];int sa[maxn];bool judge(int mid,int len){ for(int i=2; i<=len; i++) { if(height[i]>=mid) { for(int j=i-1; j>=2; j--) { if(fabs(sa[i]-sa[j])>=mid) { return true; } if(height[j]<mid) break; } } } return false;}int main(){ int n; //freopen("in.txt","r",stdin); // freopen("out.txt","w",stdout); while(~scanf("%d",&n)&&n) { for(int i=0; i<n; i++) { scanf("%d",&num[i]); } // if(n<9) {printf("0\n");continue;} for(int i=0; i<n-1; i++) { num[i]=num[i+1]-num[i]+90; } num[n-1]=0; da(num,sa,n,200); calheight(num,sa,n-1); int low=3,hight=n-1,mid; while(low<hight){ mid=(low+hight)>>1; if(judge(mid,n-1)){ low=mid+1; } else{ hight=mid; } } if(low<5) printf("0\n"); else printf("%d\n",low); } return 0;}
0 0
- Musical Theme+POJ+后缀数组
- poj 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 1743】Musical Theme【后缀数组】
- POJ 1743 Musical Theme 后缀数组
- [后缀数组+二分] poj 1743 Musical Theme
- 【后缀数组】【poj 1743】Musical Theme
- 【POJ】1743 Musical Theme 【后缀数组】
- POJ 1743 Musical Theme(后缀数组)
- HDOJ 题目1269迷宫城堡(并查集)
- Linux TC 带宽管理队列规则
- 什么是实例内部类 Instance inner class有什么语法?
- java中内部类中还有内部类请给实例!
- hadoop Map 100% reduce 0% 问题
- Musical Theme+POJ+后缀数组
- vs2012运行c语言出现:无法查找或打开 PDB 文件
- 01-hibernate jpa 注解 @Temporal(TemporalType.DATE) 日期注解
- U-BOOT官网链接和linux内核
- codeforces 364B Free Market dp
- get & post
- android之HttpURLConnection
- yii 调用c#写的Webservice时客户端编程方式
- [java多线程]多线程同步(二)——wait, notify, notifyAll, join以及sleep