POJ 1743 Musical Theme(后缀数组,3级)
来源:互联网 发布:哪个软件可以手绘 编辑:程序博客网 时间:2024/04/28 05:37
H - Musical Theme
Time Limit:1000MS Memory Limit:30000KB 64bit IO Format:%I64d & %I64uAppoint description:
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.
#include<iostream>#include<cstring>#include<cstdio>#define FOR(i,a,b) for(int i=a;i<=b;++i)#define clr(f,z) memset(f,z,sizeof(f))using namespace std;const int msize=2e4+9;int f[msize];class SUFFIX_ARRAY{ public:int sa[msize],rank[msize],h[msize],c[msize]; bool cmp(int*r,int i,int k) { return r[ sa[i] ]==r[ sa[i-1] ]&&r[ sa[i]+k ]==r[ sa[i-1]+k ]; } void build_SA(int*s,int n,int m) { int*wx=rank,*wy=h; FOR(i,0,m-1)c[i]=0; FOR(i,0,n-1)++c[ wx[i]=s[i] ]; FOR(i,1,m-1)c[i]+=c[i-1]; for(int i=n-1;i>=0;--i)sa[ --c[ wx[i] ] ]=i; for(int k=1;k<=n;k<<=1) { int p=0; FOR(i,n-k,n-1)wy[p++]=i; FOR(i,0,n-1)if(sa[i]>=k)wy[p++]=sa[i]-k; FOR(i,0,m-1)c[i]=0; FOR(i,0,n-1)++c[ wx[ wy[i] ] ]; FOR(i,1,m-1)c[i]+=c[i-1]; for(int i=n-1;i>=0;--i)sa[ --c[ wx[ wy[i] ] ] ]=wy[i]; p=1;swap(wx,wy); wx[ sa[0] ]=0; FOR(i,1,n-1)wx[ sa[i] ]=cmp(wy,i,k)?p-1:p++; if(p>=n)break; m=p; } } void get_H(int*s,int n) { int k=0; FOR(i,0,n)rank[ sa[i] ]=i; FOR(i,0,n-1) { if(k)--k; int j=sa[ rank[i]-1 ]; while(s[i+k]==s[j+k])k++; h[ rank[i] ]=k; } } bool check(int mid,int n) { int l=sa[1],r=sa[1]; FOR(i,2,n) { if(h[i]<mid)l=r=sa[i]; else { if(sa[i]<l)l=sa[i]; if(sa[i]>r)r=sa[i]; if(r-l>mid)return 1; } } return 0; }}tf;int main(){ int n; #ifdef __FOI freopen("data.in","r",stdin); #endif while(scanf("%d",&n)&&n) { for(int i=0;i<n;++i) scanf("%d",&f[i]); FOR(i,0,n-2)f[i]=f[i+1]-f[i]+90; --n; f[n]=0; tf.build_SA(f,n+1,222); tf.get_H(f,n); int ans=-1; int l=1,r=n/2,mid; while(l<=r) { mid=(l+r)/2; if(tf.check(mid,n)) { l=mid+1;ans=mid; } else r=mid-1; } if(ans<4)ans=-1; ++ans; printf("%d\n",ans); }}
- POJ 1743 Musical Theme(后缀数组,3级)
- 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(后缀数组)
- poj 1743 Musical Theme 后缀数组
- POJ 1743 Musical Theme 后缀数组
- poj 1743 Musical Theme 【后缀数组】
- poj 1743 Musical Theme 后缀数组
- POJ 1743 Musical Theme(后缀数组)
- XMLHTTPRequest实现带进度条的文件上传
- 我希望早几年知道的 5 个 Unix 命令
- calendar显示为中文修改fullcalendar.js
- ubuntu 解压到指定目录
- js笔记
- POJ 1743 Musical Theme(后缀数组,3级)
- j2ee中常用的五种远程调用协议
- 调用系统的方法,打开文件的源代码
- POSIX标准, PThread和OpenMP
- ubuntu 上的 curl 安装 针对 sftp
- 【LeetCode】Valid Sudoku解题笔记
- 使用 Shell 进行进制转换
- GUI的学习
- hdu1430(康拓展开+bfs打表)