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. Git Pull Failed: cannot lock ref 'refs/remotes/origin/xxxxxxxx': unable to resolve ref

    1.xxxxxxxx代表目录名称,我要pull的目录是supman_creditmall_v5: 2.从代码库中pull代码时报这个错误,代码pull失败: 3.解决办法,看下图,删除文件后再pull ...

  2. CSS font系列

    font-family font-family: Verdana,Helvetica,Arial,"Microsoft YaHei",sans-serif; font-family ...

  3. hibernate介绍及环境搭建

    1.前言 hibernate与mybatis的位置一样,都是属于DAO层的框架,代替我们原来的JDBC操作数据库,属于ORM(object relationg mapping. 对象关系映射)框架.O ...

  4. mybatis输入输出映射——(五)

    0.#{}与${}区别 #{}实现的是向prepareStatement中的预处理语句中设置参数值,sql语句中#{}表示一个占位符即?. <!-- 根据id查询用户信息 --> < ...

  5. Densenet-Tensorflow

    在寻找densnet网络的时候,我发现了一个结构清晰完整的网络代码,在此作备份. https://github.com/taki0112/Densenet-Tensorflow Densenet-Te ...

  6. python标准模块(下)

    Python 系统标准模块(shutil.logging.shelve.configparser.subprocess.xml.yaml.自定义模块) 目录: shutil logging模块 she ...

  7. watch案例解析(element-ui el-select 无法选中问题剖析)

    fire 读在最前面: 1.此文章衔接Vue 虚拟Dom 及 部分生命周期初探,相关整体知识点请先阅读后再继续本文阅读 问:子组件中明明有watch value,为什么this.$emit('inpu ...

  8. 定制自己的new和delete:operator new 和 operator delete

    new和delete不同用法 基本用法 int * aptr = new int(10); delete aptr, aptr = nullptr; 上面的代码是我们最基本也是最常见的使用new和de ...

  9. java多线程整理

    参考博客: http://blog.csdn.net/javazejian/article/details/50878598

  10. Mvc+Dapper+存储过程分页10万条数据

    10万条数据采用存储过程分页实现(Mvc+Dapper+存储过程) 有时候大数据量进行查询操作的时候,查询速度很大强度上可以影响用户体验,因此自己简单写了一个demo,简单总结记录一下: 技术:Mvc ...