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. Appium之编写H5应用测试脚本(切换到Webview)

    App使用H5编写,默认方式找不到元素.启动后获取所有上下文,找到webivew_xxxx的,然后进行切换. 源码: package MyAppium; import io.appium.java_c ...

  2. 解析$(this).data('type');

    html: <button type="button" class="layui-btn layui-btn-sm" data-type="ad ...

  3. 如何设置C-Lodop打印控件的端口

    Lodop是一款功能强大的打印控件,在一些浏览器不再支持np插件之后,Lodop公司又推出了C-Lodop,C-Lodop是以服务的方式解决web打印,摆脱了对浏览器的依赖,支持了所有的浏览器. 该控 ...

  4. LODOP打印css样式rgba显示黑色区块

    当LODOP打印html超文本出现问题的时候,要删减排查一下样式,查看Lodop传入的内部的html超文本和样式,可查看本博客另一篇博文:删减发现有问题的样式,并解决该问题,尽量使用通用的css样式, ...

  5. c++ string替换指定字符串

    string fnd = "dataset"; string rep = "labels"; string buf = "d:/data/datase ...

  6. SQL Server 一张图让你秒懂联合表查询

  7. StringBuffer作为参数传递的问题

    public class Foo {2.   public static void main (String [] args)  {3.      StringBuffer a = new Strin ...

  8. Vmware 给虚拟机传脚本并执行

    #_*_ coding:utf8 _*_ from pysphere import VIServer import ssl import re import sys import os import ...

  9. BZOJ2616 SPOJ PERIODNI(笛卡尔树+树形dp)

    考虑建一棵小根堆笛卡尔树,即每次在当前区间中找到最小值,以最小值为界分割区间,由当前最小值所在位置向两边区间最小值所在位置连边,递归建树.那么该笛卡尔树中的一棵子树对应序列的一个连续区间,且根的权值是 ...

  10. Codeforces Round #488 Div. 1

    A:枚举每个点判断是否同时在两个正方形中即可. #include<iostream> #include<cstdio> #include<cmath> #inclu ...