POJ 1743 Musical Theme (字符串HASH+二分)

时间:2023-02-16 23:03:51
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 &lpar;后缀数组&plus;二分&rpar;

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

  2. Poj 1743 Musical Theme&lpar;后缀数组&plus;二分答案&rpar;

    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. &lbrack;poj 1743&rsqb; Musical Theme 后缀数组 or hash

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

  7. POJ 1743 Musical Theme &lpar;Hash&rpar;

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

  8. POJ 1743 Musical Theme 二分&plus;后缀数组

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

  9. POJ 1743 Musical Theme Hash&plus;二分法

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

随机推荐

  1. mysql常用函数

    一.数学函数 数学函数主要用于处理数字,包括整型.浮点数等. 注:对大小写不敏感 ABS(x) 返回x的绝对值 SELECT ABS(-1) -- 返回1 CEIL(x),CEILING(x) 返回大 ...

  2. Setting Up KeePass For Centos 6

    This mini-howto describes how to set up KeePass on Centos 6. It requires building mono from source a ...

  3. 用 JMH 检测 Lambdas 序列化性能

    本文将介绍如何进行 Java Lambdas 序列化性能检测.Lambdas 的重要性以及 Lambdas 在分布式系统中的应用. Lambdas 表达式是 Java 8 中万众期待的新特性,其若干用 ...

  4. linux下CDROM挂载

    在VM-->removableDevice-->CD DVD-->加载iso镜像文件: [root@rusky2 mnt]# mount /dev/cdrom /mnt/cdrom ...

  5. Asp&period;Net Core 2&period;1&plus;的视图缓存&lpar;响应缓存&rpar;

    响应缓存Razor 页与 ASP.NET 核心 2.0 中不支持. 此功能将支持ASP.NET 核心 2.1 版本. 在老的版本的MVC里面,有一种可以缓存视图的特性(OutputCache),可以保 ...

  6. Mac下显示网页全屏快捷键

    control+command+F mac下谷歌浏览器全屏时隐藏头部:(隐藏标签页和地址栏) command+shift+B

  7. web初级开发的那些坑

    1.在使用js原生的XMLHttpRequest加载.xml文件时,老是不对,按照书上的写的没错,后来才发现是我的web.xml文件中阻止了.xml文件的加载. 2.有关于string解析成json数 ...

  8. my&period;cnf 配置文件参数解释

    my.cnf 配置文件参数解释: #*** client options 相关选项 ***# #以下选项会被MySQL客户端应用读取.注意只有MySQL附带的客户端应用程序保证可以读取这段内容.如果你 ...

  9. Java解析XML格式串(JDOM解析)

    import java.io.IOException; import java.io.StringReader; import java.util.List; import org.jdom.Docu ...

  10. unity 大游戏使用什么框架

    关于Unity的架构有如下几种常用的方式.1.EmptyGO在Hierarchy上创建一个空的GameObject,然后挂上所有与GameObject无关的逻辑控制的脚本.使用GameObject.F ...