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. css的特性

    一.继承性: 继承是一种规则,它允许样式不仅应用于某个特定html标签元素,而且应用于其后代. /* 不具有继承性的css样式: */p{border:1px solid red;} 二.特殊性(优先 ...

  2. Auth模块使用方法大全

    auth认证 导包 from django.contrib import auth 默认数据库中使用auth_user表 创建超级用户 python manage.py createsuperuser ...

  3. Civil 3D 二次开发 翻转曲面高程分析颜色

    不解释,直接上代码及截图. [CommandMethod("RvsSEA")] public void ReverseSurfaceElevationAnalysis() { Ci ...

  4. zabbix自动注册

    实现方法是: 第一: 选择动作-->事件源-->自动注册-->创建动作 第二: 动作-->触发条件-->主机元数据-->contains-->Linux 第四 ...

  5. SPOJ705-New Distinct Substrings-后缀数组

    计算所都不相同子串的个数,做法是所有子串的个数减去sigma(height[]).其中height数组的和便是所有相同子串的个数. 注意 N×(N+1)/2会爆int!但是最终答案在int内.所以使用 ...

  6. alter table,复制, 单表查询

    修改表 语法:1. 修改表名      ALTER TABLE 表名                           RENAME 新表名; 2. 增加字段      ALTER TABLE 表名 ...

  7. CodeForces 632C The Smallest String Concatenation//用string和sort就好了&&string的基础用法

    Description You're given a list of n strings a1, a2, ..., an. You'd like to concatenate them togethe ...

  8. 实验六 MapReduce实验:二次排序

    实验指导: 6.1 实验目的基于MapReduce思想,编写SecondarySort程序. 6.2 实验要求要能理解MapReduce编程思想,会编写MapReduce版本二次排序程序,然后将其执行 ...

  9. 聊聊GarbageCollectionNotificationInfo

    序本文主要研究一下GarbageCollectionNotificationInfo CompositeDatajava.management/javax/management/openmbean/C ...

  10. HDU1542-Atlantis【离散化&线段树&扫描线】个人认为很全面的详解

    刚上大一的时候见过这种题,感觉好牛逼哇,这都能算 如今已经不打了,不过适当写写题保持思维活跃度还是不错的,又碰到这种题了,想把它弄出来 说实话,智商不够,看了很多解析,花了4.5个小时才弄明白 网上好 ...