Musical Theme
Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 15900   Accepted: 5494

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

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

Hint

Use scanf instead of cin to reduce the read time.

Source

对两个相邻的之间的差值进行HASH

二分判断就可以了

主要是学习下HASH的方法。

这题用SA也很快

 /* ***********************************************
Author :kuangbin
Created Time :2013-11-5 17:25:20
File Name :E:\2013ACM\专题学习\字符串HASH\POJ1743.cpp
************************************************ */ #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 HASH = ;
const int MAXN = ;
struct HASHMAP
{
int head[HASH],next[MAXN],size;
unsigned long long state[MAXN];
int f[MAXN];
void init()
{
size = ;
memset(head,-,sizeof(head));
}
int insert(unsigned long long val,int _id)
{
int h = val%HASH;
for(int i = head[h]; i != -;i = next[i])
if(val == state[i])
{
return f[i];
}
f[size] = _id;
state[size] = val;
next[size] = head[h];
head[h] = size++;
return f[size-];
}
};
HASHMAP H;
const int SEED = ;
unsigned long long P[MAXN];
unsigned long long S[MAXN];
int A[MAXN];
int n;
bool check(int x)
{
H.init();
for(int i = x;i < n;i++)
if(H.insert(S[i] - S[i-x]*P[x],i) < i-x)
return true;
return false;
} int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
P[] = ;
for(int i = ;i < MAXN;i++)
P[i] = P[i-] * SEED;
while(scanf("%d",&n) && n)
{
for(int i = ;i <= n;i++)
scanf("%d",&A[i]);
for(int i = ;i < n; i++)
A[i] = A[i+] - A[i];
S[] = ;
for(int i = ;i < n;i++)
S[i] = S[i-]*SEED + A[i];
int ans = ;
int l = , r = n-;
while(l <= r)
{
int mid = (l + r)/;
if(check(mid))
{
ans = mid;
l = mid+;
}
else r = mid-;
}
if(ans < )ans = -;
ans++;
printf("%d\n",ans);
}
return ;
}

POJ 1743 Musical Theme (字符串HASH+二分)的更多相关文章

  1. Poj 1743 Musical Theme (后缀数组+二分)

    题目链接: Poj  1743 Musical Theme 题目描述: 给出一串数字(数字区间在[1,88]),要在这串数字中找出一个主题,满足: 1:主题长度大于等于5. 2:主题在文本串中重复出现 ...

  2. Poj 1743 Musical Theme(后缀数组+二分答案)

    Musical Theme Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 28435 Accepted: 9604 Descri ...

  3. POJ 1743 Musical Theme (后缀数组,求最长不重叠重复子串)(转)

    永恒的大牛,kuangbin,膜拜一下,Orz 链接:http://www.cnblogs.com/kuangbin/archive/2013/04/23/3039313.html Musical T ...

  4. poj 1743 Musical Theme(最长重复子串 后缀数组)

    poj 1743 Musical Theme(最长重复子串 后缀数组) 有N(1 <= N <=20000)个音符的序列来表示一首乐曲,每个音符都是1..88范围内的整数,现在要找一个重复 ...

  5. POJ 1743 Musical Theme 【后缀数组 最长不重叠子串】

    题目冲鸭:http://poj.org/problem?id=1743 Musical Theme Time Limit: 1000MS   Memory Limit: 30000K Total Su ...

  6. [poj 1743] Musical Theme 后缀数组 or hash

    Musical Theme 题意 给出n个1-88组成的音符,让找出一个最长的连续子序列,满足以下条件: 长度大于5 不重叠的出现两次(这里的出现可以经过变调,即这个序列的每个数字全都加上一个整数x) ...

  7. POJ 1743 Musical Theme (Hash)

    Musical Theme Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 33820   Accepted: 11259 D ...

  8. POJ 1743 Musical Theme 二分+后缀数组

    Musical Theme   Description A musical melody is represented as a sequence of N (1<=N<=20000)no ...

  9. POJ 1743 Musical Theme Hash+二分法

    标题效果:有一个美丽的旋律,它们是由一些不大于88音调.如果计为五个音调的量度,问:是否有相同的节奏的多个部分(相同的差,以及两者之间的相同的节奏不能重叠),并寻求最长长度. 思考:这个问题是八人中的 ...

随机推荐

  1. waven 常用构建命令

    常用命令 mvn compile : 编译maven项目 mvn test : 运行项目测试用例 mvn package : 将项目打成jar包 mvn clean : 删除target目录下生成的文 ...

  2. Python基础教程-第3版(文字版) 超清文字-非扫描版 [免积分、免登录]

    此处免费下载,无需账号,无需登录,无需积分.收集自互联网,侵权通知删除. 点击下载:Python基础教程-第3版 备用下载:Python基础教程-第3版

  3. mysql区间范围查询问题

    一,日期区间查询,表里有一个时间字段 最常见的就是某时间段查询,比如xxxx时间---xxxx时间有多少条数据.例如数据库里的字段是 income_period, 该字段类型可以是字符串(varcha ...

  4. Quartus II 破解教程—FPGA入门教程【钛白Logic】

    这一节主要说明如何破解Quartus II 13.1.首先找到我们提供的破解工具,这里我们的电脑是64位的,所以使用64位破解器.如下图. 第一步:将破解工具拷贝到安装目录下“D:\altera\13 ...

  5. PowerDesigner显示Common注释列并自动赋值

    PowerDesigner中默认不显示Common注释列,可根据以下步骤显示并紫东填充Name列内容. 1.显示Common注释列 2.运行VB Script脚本自动赋值 使用Shift+Ctrl+X ...

  6. 过滤掉文本中的javascript标签代码

    2014年1月21日 11:51:19 php代码: $content = preg_replace('#<\s*[script].*>#', '', $a);//有些攻击可以在scrip ...

  7. Eclipse的git插件冲突合并方法

    Eclipse有一个git的插件叫EGit,用于实现本地代码和远程代码对比.合并以及提交.但是在本地代码和远程代码有冲突的时候,EGit的处理方案还是有点复杂.今天就彻底把这些步骤给理清楚,并公开让一 ...

  8. java虚拟机规范(se8)——java虚拟机结构(一)

    本文翻译自:https://docs.oracle.com/javase/specs/jvms/se8/html/jvms-2.html 第二章 虚拟机结构 本文档描述了一个抽象的虚拟机规范,并不描述 ...

  9. vue项目使用vw单位适配移动端方法

    传送门:  https://blog.csdn.net/zjw0742/article/details/79337336

  10. HTTPS那-攻击实例与防御

    在<HTTPS-SSL证书>我描述了使用SSL证书时一些需要注意的安全问题,在这一篇文章里面我再演示一下针对HTTPS攻击的一些实例,通过这些实例能更安全的使用HTTPS.知己知彼百战不殆 ...