POJ 1743 Musical Theme
来源:互联网 发布:软件测试书籍电子版 编辑:程序博客网 时间:2024/05/21 09:03
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!
【题目分析】
后缀数组+二分最优转判定 思路简直不要太好。由于有转调(同时加上或者减去一个值)只能记录下变化量,输出的时候再加上一。注意的是,最终结果小于4要输出量。
复制的题解如下
先二分答案,把题目变成判定性问题:判断是否存在两个长度为k的子串是相同的,且不重叠。解决这个问题的关键还是利用height数组。把排序后的后缀分成若干组,其中每组的后缀之间的height值都不小于k。容易看出,有希望成为最长公共前缀不小于k的两个后缀一定在同一组。然后对于每组后缀,只须判断每个后缀的sa值的最大值和最小值之差是否不小于k。如果有一组满足,则说明存在,否则不存在。整个做法的时间复杂度为O(nlogn)。
思路太好,写起来水道渠成,然后WA。最后加了小于5的判断才过。
【代码】
#include <cstring>#include <cstdio>#define maxn 20001#define m(a) memset(a,0,sizeof a)int wa[maxn],wb[maxn],wv[maxn],ws[maxn],sa[maxn],rank[maxn],height[maxn],s[maxn],n,a[maxn];inline int cmp(int *r,int a,int b,int l){return r[a]==r[b]&&r[a+l]==r[b+l];}inline void getsa(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++; }}inline void gethi(int *r,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++);}inline int min(int a,int b){return a>b?b:a;}inline int max(int a,int b){return a>b?a:b;}inline bool test(int k){ int minn=0x3f3f3f3f,maxx=-0x3f3f3f3f; int i=2; while (i<=n) { if (height[i]>=k) { minn=min(minn,sa[i-1]),maxx=max(maxx,sa[i-1]); while (height[i+1]>=k) ++i,minn=min(minn,sa[i-1]),maxx=max(maxx,sa[i-1]); minn=min(minn,sa[i]),maxx=max(maxx,sa[i]); if (maxx-minn>=k) return true; } minn=0x3f3f3f3f,maxx=-0x3f3f3f3f; i++; } return false;}int main(){ while (scanf("%d",&n)&&n) { for (int i=0;i<n;++i) scanf("%d",&a[i]); if (n<10){printf("0\n");continue;} for (int i=0;i<n-1;++i) s[i]=a[i+1]-a[i]+89; n--; s[n]=0; getsa(s,sa,n+1,200); gethi(s,n); int l=4,r=(n-1)/2; while (l<r) { int mid=(l+r+1)/2; if (test(mid)) l=mid; else r=mid-1; } printf("%d\n",l<4?0:l+1); }}
- poj 1743 Musical Theme
- poj 1743 Musical Theme
- poj 1743 Musical Theme
- Musical Theme POJ 1743
- 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
- chapter2 变量和常量
- 《用tcp/ip进行网际互联》第7章 网际协议:转发IP数据报 笔记
- tensorflow 笔记1
- Java异常处理的10个最佳实践
- cocos2dx——lua
- POJ 1743 Musical Theme
- [搜索]八数码问题
- uva348 Optimal Array Multiplication Sequence
- chapter3 常用的运算符
- mybatis返回插入值的主键
- Android 中文件类型与MIME的匹配表
- C++第四次作业(补)x
- 用双向链表实现一个栈和队列
- Twitter-Snowflake,64位自增ID算法详解