PKU1743 Musical Theme 楼教主男人八题之一
来源:互联网 发布:程序员的思维修炼 豆瓣 编辑:程序博客网 时间:2024/05/29 21:36
Musical Theme
Time Limit: 1000MS Memory Limit: 30000KTotal Submissions: 6021 Accepted: 2127
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
后缀数组+二分=轻松加愉快!
#include<iostream>
#include<string.h>
#include<cmath>
#include<memory.h>
#include<algorithm>
using namespace std;
#define N 20010
int s[N]; // N > 256
int n, ranks[N], sa[N], height[N],tmp[N], top[N];
void makesa(){ // O(N * log N)
int i, j, len, na;
na = (n < 256 ? 256 : n);
memset(top, 0, na * sizeof(int));
for (i = 0; i < n ; i++) top[ ranks[i] = s[i] & 0xff ]++;
for (i = 1; i < na; i++) top[i] += top[i - 1];
for (i = 0; i < n ; i++) sa[ --top[ ranks[i] ] ] = i;
for (len = 1; len < n; len <<= 1) {
for (i = 0; i < n; i++) {
j = sa[i] - len; if (j < 0) j += n;
tmp[ top[ ranks[j] ]++ ] = j;
}
sa[ tmp[ top[0] = 0 ] ] = j = 0;
for (i = 1; i < n; i++) {
if (ranks[ tmp[i] ] != ranks[ tmp[i-1] ] ||
ranks[ tmp[i]+len ]!=ranks[ tmp[i-1]+len ])
top[++j] = i;
sa[ tmp[i] ] = j;
}
memcpy(ranks, sa , n * sizeof(int));
memcpy(sa , tmp, n * sizeof(int));
if (j >= n - 1) break;
}
}
void lcp(){ // O(4 * N)
int i, j, k;
for (j = ranks[height[i=k=0]=0]; i < n - 1; i++, k++)
while (k >= 0 && s[i] != s[ sa[j-1] + k ])
height[j] = (k--), j = ranks[ sa[j] + 1 ];
}
bool check(int mid)
{
int mi=0xfffffff,ma=0;
int i;
for(i=1;i<n;i++)
if(height[i]>=mid)
{
do
{
ma=max(ma,max(sa[i],sa[i-1]));
mi=min(mi,min(sa[i],sa[i-1]));
i ++;
}while(i<n&&height[i]>=mid);
if(ma-mi>=mid)
return true;
}
return false;
}
int main()
{
int i,j;
//char ss[N];
while(scanf("%d",&n),n)
{
for(i=0;i<n;i++)
scanf("%d",&s[i]);
//s[0]=s[i]+88;
for(i=1;i<n;i++)
s[i-1]=s[i]-s[i-1]+88;
s[n-1]=255;
//n++;
//s[0]=0;
s[n++]=0;
makesa();
lcp();
int ans=-1;
int l=4,r=n-1,mid;
while(l <= r)
{
mid=(l+r)>>1;
if(check(mid))
{
ans=mid;
l=mid+1;
}
else
r=mid-1;
}
printf("%d/n",ans+1);
}
return 0;
}
- PKU1743 Musical Theme 楼教主男人八题之一
- POJ 1743 Musical Theme 后缀数组 楼教主男人八题之一
- 【pku1743】Musical Theme
- poj1740-楼教主所谓的男人八题之一
- poj 1741 楼教主男人八题之一:树分治
- poj1743Musical Theme【后缀数组求最长不重叠重复子串】楼教主男人八题
- 楼教主男人八题之poj1743
- POJ 1739 Tony's Tour 楼教主的男人八题之一
- POJ 1741 Tree (树上点分治)(楼教主男人八题之一)
- poj1739Tony's Tour【插头DP】楼教主男人八题之一
- nyoj137 取石子(三) 楼教主男人八题之一
- poj 1743 Musical Theme(男人八题&后缀数组第一题)
- hdu 2844 Coins 多重背包模板题 ,二进制优化。据说是楼教主的男人八题之一
- 楼教主男人八题--POJ1741--点分治
- nyoj-137 取石子(三) (博弈,楼教主真男人八题)
- POJ 1742 Coins(多重背包变型,楼教主男人八题)
- PKU1743(Musical Theme)求不可重叠最长重复子串(后缀数组+二分)
- 楼教主男人八题 POJ 1741(树分治(我自然是看题解搞懂的))
- Perl function: moveDirectory
- Coordinator
- 固态哈希
- linux IPC-消息队列 的内核限制
- 完美的解决select只读的方法
- PKU1743 Musical Theme 楼教主男人八题之一
- 你好1
- 基于ARM9的智能车载系统设计
- 社交功能突出 MeeGo系统截图再曝光
- php中的安全模式?
- EXT文本编辑器(htmleditor)[初始化界面]
- C#压缩与解压缩文件
- 男人忙,所以经常上错床
- SQL Server 2005 收缩数据库