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. MyBatis的XML中使用内部类的方式

    内部类需要使用$符号连接,而不是点.,如 com.pingan.job.openapi.model.SMSESBResult$ReceiveResult$ResultInfo 从CSDN论坛查到的. ...

  2. java 中 的 字节流!

    package cn.zhouzhou; import java.io.FileInputStream; import java.io.FileNotFoundException; import ja ...

  3. ABP 番外篇-容器

    一. @using YD.CloudTimetable.Web.Areas.AppAreaName.Startup @{ ViewBag.CurrentPageName = AppAreaNamePa ...

  4. Modbus CRC 16 (C#)

    算法 1.预置一个值为 0xFFFF 的 16 位寄存器,此寄存器为 CRC 寄存器. 2.把第 1 个 8 位二进制数据(即通信消息帧的第 1 个字节)与 16 位的 CRC 寄存器相异或,异或的结 ...

  5. webpack 配置 publicPath的理解

    在学习webpack的时候,配置文件中有一个publicPath属性,一直不是很明白它到底是怎么用,也查了很多资料,得到最多的说法是当打包的时候,webpack会在静态文件路径前面添加publicPa ...

  6. codeforces492C

    Vanya and Exams CodeForces - 492C Vanya wants to pass n exams and get the academic scholarship. He w ...

  7. hdu-4738(tarjan割边)

    题意:给你n个点,m条边,边有权值,问你最小的花费使图不连通: 解题思路:就是求边权最小的割边,但这道题有坑点: 1.有重边(桥的两个点有重边时,你去掉一条边并没什么d用): 2.当权值为0的时候,我 ...

  8. Nginx lingering_close延迟关闭

    L:130

  9. Cmder使用ls中文显示乱码解决方案

    操作系统:Windows 7 旗舰版 Cmder:1.3.2 默认配置不支持使用ls显示中文命名的文件列表. 解决方法: 按下Win+Alt+P打开设置. 在StartUp - Environment ...

  10. redis日常使用汇总--持续更新

    redis日常使用汇总--持续更新 工作中有较多用到redis的场景,尤其是触及性能优化的方面,传统的缓存策略在处理持久化和多服务间数据共享的问题总是不尽人意,此时引入redis,但redis是单线程 ...