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. 20155210潘滢昊 2016-2017-2 《Java程序设计》第8周学习总结

    20155210 2016-2017-2 <Java程序设计>第8周学习总结 教材学习内容总结 认识NIO Channel: 衔接数据节点(与IO中的流对比) isOpen close R ...

  2. 20155338 2016-2017-2 《Java程序设计》第6周学习总结

    20155338 2016-2017-2 <Java程序设计>第6周学习总结 教材学习内容总结 输入和输出 • 串流设计概念 要想活用输入/输出API,一定先要了解Java中如何以串流抽象 ...

  3. Passbook

    CHENYILONG Blog Passbook 技术博客http://www.cnblogs.com/ChenYilong/ 新浪微博http://weibo.com/luohanchenyilon ...

  4. 第8月第22天 python scrapy

    1. cd /Users/temp/Downloads/LagouSpider-master ls ls ls lagou/settings.py cat lagou/settings.py ls p ...

  5. rstful登陆认证并检查session是否过期

    一:restful用户视图 #!/usr/bin/env python # -*- coding:UTF-8 -*- # Author:Leslie-x from users import model ...

  6. 关于python开发CRM系统

    注意本项目是针对培训学校开发简化的CRM CRM简介 CRM全称:customer relationship management 无CRM的痛点 没有CMR的缺点及痛点: 每个销售会通过Excel来 ...

  7. ARMCC和GCC编译ARM代码的软浮点和硬浮点问题【转】

    转自:https://blog.csdn.net/hunanchenxingyu/article/details/47003279 本文介绍了ARM代码编译时的软浮点(soft-float)和硬浮点( ...

  8. Kali Linux上安装SSH服务

    安装 SSH 从终端使用 apt-get 命令安装 SSH 包: # apt-get update # apt-get install ssh 启用和开始使用 SSH 为了确保安全 shell 能够使 ...

  9. JavaScript如何获得input元素value值

    转载地址:http://aquarius-zf.iteye.com/blog/605144 在页面中我们最常见的页面元素就是input了,但是我们如何用JavaScript得到网页input中输入的v ...

  10. h5手势密码开发(使用jq)

    直接上代码: 目录结构: 本次开用到的技术jq,以及引入的jq插件jquery.gesture.password.min.js index.html <!DOCTYPE html> < ...