POJ 1743 Musical Theme (字符串HASH+二分)
| Time Limit: 1000MS | Memory Limit: 30000K | |
| Total Submissions: 15900 | Accepted: 5494 |
Description
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 last test case is followed by one zero.
Output
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
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+二分)的更多相关文章
- Poj 1743 Musical Theme (后缀数组+二分)
题目链接: Poj 1743 Musical Theme 题目描述: 给出一串数字(数字区间在[1,88]),要在这串数字中找出一个主题,满足: 1:主题长度大于等于5. 2:主题在文本串中重复出现 ...
- Poj 1743 Musical Theme(后缀数组+二分答案)
Musical Theme Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 28435 Accepted: 9604 Descri ...
- POJ 1743 Musical Theme (后缀数组,求最长不重叠重复子串)(转)
永恒的大牛,kuangbin,膜拜一下,Orz 链接:http://www.cnblogs.com/kuangbin/archive/2013/04/23/3039313.html Musical T ...
- poj 1743 Musical Theme(最长重复子串 后缀数组)
poj 1743 Musical Theme(最长重复子串 后缀数组) 有N(1 <= N <=20000)个音符的序列来表示一首乐曲,每个音符都是1..88范围内的整数,现在要找一个重复 ...
- POJ 1743 Musical Theme 【后缀数组 最长不重叠子串】
题目冲鸭:http://poj.org/problem?id=1743 Musical Theme Time Limit: 1000MS Memory Limit: 30000K Total Su ...
- [poj 1743] Musical Theme 后缀数组 or hash
Musical Theme 题意 给出n个1-88组成的音符,让找出一个最长的连续子序列,满足以下条件: 长度大于5 不重叠的出现两次(这里的出现可以经过变调,即这个序列的每个数字全都加上一个整数x) ...
- POJ 1743 Musical Theme (Hash)
Musical Theme Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 33820 Accepted: 11259 D ...
- POJ 1743 Musical Theme 二分+后缀数组
Musical Theme Description A musical melody is represented as a sequence of N (1<=N<=20000)no ...
- POJ 1743 Musical Theme Hash+二分法
标题效果:有一个美丽的旋律,它们是由一些不大于88音调.如果计为五个音调的量度,问:是否有相同的节奏的多个部分(相同的差,以及两者之间的相同的节奏不能重叠),并寻求最长长度. 思考:这个问题是八人中的 ...
随机推荐
- VMware Linux 下 Nginx 安装配置 - nginx.conf 配置 [负载两个 Tomcat] (三)
首先启动Nginx 1. 相关浏览 两个 Tomcat 配置: VMware Linux 下 Nginx 安装配置 - Tomcat 配置 (二) Nginx 安装配置启动: VMware Linu ...
- 用Java构建一个简单的WebSocket聊天项目之新增HTTP接口调度
采用框架 我们整个Demo基本不需要大家花费太多时间,就可以实现以下的功能. 用户token登录校验 自我聊天 点对点聊天 群聊 获取在线用户数与用户标签列表 发送系统通知 首先,我们需要介绍一下我们 ...
- 第5月第8天 jsonmodel
1. @implementation JSONValueTransformer (CustomTransformer) //时间戳转NSDate - (NSDate *)NSDateFromNSStr ...
- hibernate的多对多关联映射
在我们实际项目中,多对多的情况也时长存在,比如最常见的就是系统管理的五张表,如下面的一个结构: 在本文学习hibernate多对多关联映射的实验中我简单的写几个字段,达到学习目的即可. 1.多对多的关 ...
- linux调试工具glibc的演示分析-core dump double free【转】
转自:http://www.cnblogs.com/jiayy/p/3475544.html 偶然中发现,下面的两端代码表现不一样 void main(){ void* p1 = malloc(32) ...
- VUE之搭建脚手架
原文转自http://blog.csdn.net/Shiyaru1314/article/details/54963027 目录(?)[-] 1 安装之前需要检查是否已经安装NodeJS的环境 安装文 ...
- JavaScript深入解读
JavaScript是运行在浏览器上的脚本语言.我们平时看到丰富多彩的网页,这要在很大程度上归功于JavaScript. 引子 学点儿历史 JavaScript在编程语言的阵营里也是老资格了.它诞生于 ...
- 【linux】监控磁盘情况并自动删除备份文件
背景:我有一个备份目录/home/kzy/bakup,会每天备份一些信息.随着日子一天天的过去,这个文件夹越来越大,终于把磁盘撑满了..... 需求:当磁盘占有率超过80%时自动删除该文件夹下最老的3 ...
- 【招聘】这一次,我们Hold住了世界杯
作为国内最大的云计算服务商,阿里云在视频领域拥有绝对的技术优势,全球范围内拥有1500多个CDN节点,带宽储备120多T,不仅为优酷.CNTV.CCTV5提供技术支撑,还承担了全网70%的世界杯流量. ...
- tomcat启动不成功(点击startup.bat闪退)的解决办法
今天遇到tomcat手动启动不成功,但是再eclipse里面是可以启动成功的.我一直开始以为是端口被占用了,最后才发现是这么一回事.首先说下我的tomcat是安装版的.免安装的tomcat双击star ...