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

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

思路:
py大佬太强了!!!!
这个题可以算是hash的模板题了,不知道学成这个样子有没有算是入门,接下来就是直接上代码了。。。
emmmm,还有很多题解说是后缀数组,可是我并不知道那是什么东西,等一下去学一下好了。
这份代码是照着py大佬的代码打的:
http://www.cnblogs.com/qldabiaoge/p/9152430.html
代码(Hash 散列表)
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
typedef unsigned long long ll;
const int maxn = 20010,Hash = 10007; /**如果我的推理没有错的话。。。。
这题可以不搞这个结构体呀
为什么不用map,或者是直接用结构体数组二分查找??
**/
struct hashmap//这个玩意只维护了特定长度的序列的值
{
int head[maxn],next[maxn],siz,f[maxn];
ll state[maxn];
void init()//初始化
{
siz=0;//siz就是散列表的大小呀
memset(head,-1,sizeof(head));
}
int add(unsigned long long val,int _id)//将某个值与他的标号加入散列表?
{//好像是一个模拟链表诶
int h=val % Hash;//h模掉hash,作为一个粗略的hash值,加速查询
//h貌似只是用来查询一个引子
for(int i=head[h];i!=-1;i=next[i]){//
if(val==state[i]){return f[i];}//state存的应该就是值了,那么f[i]存的就是出现这个值的
//第一个位置了。
}
f[siz]=_id;//存下标
state[siz]=val;//存值
next[siz]=head[h];//维护查询路线
head[h]=siz++;
return f[siz-1];//返回首次出现的此hash值的位置
}
}H;
const int seed = 13331;
ll p[maxn],s[maxn];
int a[maxn],n;
int check(int x)//x是子串的长度
{
H.init();//每检查一次,就初始化一次
//可能情况太多,内存达不到要求,所以只能一段长度更新一次。
//而将其注释后超时其实是因为数组越界造成的无限循环
for(int i=x;i<n;i++){
if(H.add(s[i]-s[i-x]*p[x],i)<i-x)return 1;//s[i]的值,由a[1]到a[i]决定
//s[i-x]的值,由a[1]到a[i-x]决定;
//就是找出当前的值是否在之前出现过,并且没有重叠
//如何证明s[i]-s[i-x]*p[x]对于每一完全一致的字串,他们的值相等
}return 0;
} int main()
{
p[0]=1;
for(int i=1;i<maxn;i++){
p[i]=p[i-1]*seed;
}//进制数组
while(scanf("%d",&n)&&n){
for(int i=1;i<=n;i++){
scanf("%d",&a[i]);
}//输入
for(int i=1;i<n;i++){
a[i]=a[i+1]-a[i];
}//处理两个数之间的差值
for(int i=1;i<n;i++){
s[i]=s[i-1]*seed+a[i];//a[i]是原始的差值,s[i]应该是一个与a[i]有关,而没有具体意义的数组·
//将s[i]展开,就是∑a[i]*seed^(i-1)
}
int ans=0;
int low=4,high=n-1;//二分答案
while(low<=high){
int mid = (low+high)>>1;
if(check(mid)){//check检查答案是否可行
ans=mid;
low=mid+1;
}
else high = mid-1;
}
if(ans<4){ans=-1;}
printf("%d\n",ans+1);
}
return 0;
}

Thanks♪(・ω・)ノ

接下来是第48处的证明

我把思路写一下。

s[i]数组与a[i]的值与i的值有着密切的关系,根据代码你可以将某一个s[n]展开

同时p[x]你也展开

假设x>y: s[x]-s[y]*p[x-y]

全部展开一下,整理,得到的式子,你会发现,它的值只与每一个a[i]的值有关,而它的位置对值产生的影响,已经在这个式子中被减去为0了。

所以,如果是两个完全一样的字串,无论在那个位置,通过这个式子算出来的值都会相等。

再说一下,这个散列表确实比较骚,不过每次查找表中之前有没有出现相同的hash值时,最坏的情况可能是遍历了一遍,因为hash值是0--2^64,就是相当于10的18次方,那么获得相同h的hash值共有10的18次方/Hash。也就是10的14次方个。。。不过这种情况应该是不可能出现的,或许在程序设计竞赛中,根本不会出现这种情况。

——————————————————————————————————————————————————————————————————————

emmmmm,直接用map超时了,看来我的推理真的错了。。。。

但是也可能只是这一题的数据问题,不能代表所有情况,但是给我们的启示就是,少用map,因为上次MLE了。

直接用结构体二分是绝对不行的,因为如果是这样的话,就要走一次排序一次了。

TLE代码

#include<iostream>
#include<cstdio>
#include<cstring>
#include<map>
using namespace std;
typedef unsigned long long ll;
const int maxn = 20010;
map<ll,int>mp;
const int seed = 13331;
ll p[maxn],s[maxn];
int a[maxn],n;
int check(int x)//x是子串的长度
{
mp.clear();
for(int i=x;i<n;i++){
ll sum = s[i]-s[i-x]*p[x];
if(mp[sum]!=0&&mp[sum]<i-x){return 1;}
if(mp[sum]==0){mp[sum]=i;}
}
return 0;
} int main()
{
p[0]=1;
for(int i=1;i<maxn;i++){
p[i]=p[i-1]*seed;
}
while(scanf("%d",&n)&&n){
for(int i=1;i<=n;i++){
scanf("%d",&a[i]);
}
for(int i=1;i<n;i++){
a[i]=a[i+1]-a[i];
}
a[0]=0;
for(int i=1;i<n;i++){
s[i]=s[i-1]*seed+a[i];
}
int ans=0;
int low=4,high=n-1;
while(low<=high){
int mid = (low+high)>>1;
if(check(mid)){
ans=mid;
low=mid+1;
}
else high = mid-1;
}
if(ans<4){ans=-1;}
printf("%d\n",ans+1);
}
return 0;
}

  

POJ 1743 Musical Theme (Hash)的更多相关文章

  1. POJ 1743 Musical Theme Hash+二分法

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

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

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

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

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

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

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

  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 (字符串HASH+二分)

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

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

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

  8. POJ 1743 Musical Theme 后缀数组 最长重复不相交子串

    Musical ThemeTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://poj.org/problem?id=1743 Description ...

  9. poj 1743 Musical Theme (后缀数组+二分法)

    Musical Theme Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 16162   Accepted: 5577 De ...

随机推荐

  1. dbExpress操作中用TDBGrid显示数据

    由于一些数据感知组件如TDBGrid等是需要用到数据缓存的,这和dbExpress组件的存取机制是矛盾的.所以当打开数据集时会出现如下内容的警告框:“Operation not allowed on ...

  2. css 浮动问题 display显示 和 光标设置cursor

    <!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title>浮 ...

  3. CSS3 flexbox 布局 ---- flex 容器属性介绍

    flexbox布局是CSS3中新增的属性,它可以很轻松地帮我们解决掉一些常见的布局问题,比如导航栏. 我们用普通的方法写导航栏,通常会在ul, li 结构写好后,让li 元素左浮动,然后再给ul 清浮 ...

  4. LNMP平台部署

    LNAP平台概述 百度百科 LNMP代表的就是:Linux系统下Nginx+MySQL+PHP这种网站服务器架构. Linux是一类Unix计算机操作系统的统称,是目前最流行的免费操作系统.代表版本有 ...

  5. PLsql链接oracle配置

    在Oracle的安装文件下查找tnsnames.ora文件 如果真的找不到路径,建议大家在Oracle安装位置全文搜索tnsnames.ora 配置格式 个人配置 下载并安装PL/SQL,成功安装后配 ...

  6. JS获取宽度高度大集合

    网页可见区域宽:document.body.clientWidth 网页可见区域高:document.body.clientHeight 网页可见区域宽:document.body.offsetWid ...

  7. Code POJ - 1850 组合数学

    题意 :字符串从a=1 b=2 c=3....z=26  ab=27开始编号 每个都是升序的 给出字符串问是几号 思路:主要是要看n位字符串有多少个 这里需要用组合数学的思想  组合数用杨辉三角形递推 ...

  8. Codeforces Round #505 Div. 1 + Div. 2

    传送门:>Here< 从来没打过\(CF\)(由于太晚了)-- 不知道开学了以后有没有机会能够熬夜打几场,毕竟到现在为止都是\(unrated\)好尴尬啊~ 今天早上打了几题前几天的比赛题 ...

  9. PSR-0 规范实例讲解 -- php 自动加载

    PSR-0规范 [1]命名空间必须与绝对路径一致 [2]类名首字母必须大写 [3]除去入口文件外,其他“.php”必须只有一个类 [4]php类文件必须自动载入,不采用include等 [5]单一入口 ...

  10. 洛谷 P4705 玩游戏 解题报告

    P4705 玩游戏 题意:给长为\(n\)的\(\{a_i\}\)和长为\(m\)的\(\{b_i\}\),设 \[ f(x)=\sum_{k\ge 0}\sum_{i=1}^n\sum_{j=1}^ ...