楼教主男人八题(第三题)

按照通过数排序,今天开搞 1743 Musical Theme。

题目链接

http://poj.org/problem?id=1743

题目描述

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 theme, 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!

输入

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.

输出

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.

样例输入

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

样例输出

5

样例解释

本题大意是,给出一个长度为 n n n 的数组 s e q seq seq n ∈ [ 1 , 20000 ] n ∈[1, 20000] n[1,20000] s e q i ∈ [ 1 , 88 ] seq_i ∈ [1,88] seqi[1,88]。需要求出两个不重叠的相似的子数组的最大长度。如果最大长度不小于 5 5 5,则输出最大长度,否则输出 0。

相似的定义是:两个子数组 A , B A, B A,B 长度相等,且存在整数 x x x,将每个 A i A_i Ai 加上 x x x 后可以得到 B B B,即 A i + x = B i A_i + x = B_i Ai+x=Bi

比如样例中的 34 , 30 , 26 , 22 , 18 34,30,26,22,18 34,30,26,22,18 82 , 78 , 74 , 70 , 66 82,78,74,70,66 82,78,74,70,66 就是相似的。前者将每个元素都加上 48 48 48 即可得到后者。而且找不到更长的相似子数组,所以输出是 5 5 5

题解

主流的解法是利用后缀数组求解答案,今天介绍一个非主流的解法:二分+哈希,时间复杂度 O ( n ∗ lg ⁡ n ) O(n*\lg n) O(nlgn)

首先,讲一下如何利用哈希快速判定两个数组相似。

如果两个数组 A A A B B B 相似,设下标从 0 开始,不难发现,对于 i ≥ 1 i \ge 1 i1 有:
B i − B i − 1 = A i − A i − 1 B_i - B_{i-1} = A_i - A_{i-1} BiBi1=AiAi1

比如,两个数组 1 , 3 , 4 , 7 , 5 1,3,4,7,5 1,3,4,7,5 3 , 5 , 6 , 9 , 7 3,5,6,9,7 3,5,6,9,7 是相似的,前者加上 2 2 2 可以得到后者。

分别求出两数组相邻项作差之后的数组:

  • 1 , 3 , 4 , 7 , 5 1,3,4,7,5 1,3,4,7,5 -> 1 , 2 , 1 , 3 , − 2 1,2,1,3,-2 1,2,1,3,2
  • 3 , 5 , 6 , 9 , 7 3,5,6,9,7 3,5,6,9,7 -> 3 , 2 , 1 , 3 , − 2 3,2,1,3,-2 3,2,1,3,2

不难发现,除首位外的其他位均相等。其实相邻项作差描述的是变化的规律。

s e q seq seq 的相邻项作差,得到 s e q ′ seq' seq,那么问题变成了:在 s e q ′ seq' seq 中寻找两个不重叠的不相邻的相同的的子数组,其长度加上一,则等价于 s e q seq seq 中的相似子数组的长度。

加了一个不相邻的限制,是因为相同子数组丢失了相似子数组的首位,所以不能相邻。如果相邻,则相似子数组就重叠了。

先介绍一中线性时间计算 n − c + 1 n-c+1 nc+1 个长度为 c c c 的子数组的哈希值的计算方法。

不妨设 h i h_i hi 是以 s e q i ′ seq'_i seqi 为起始位置的长度 c c c 的子数组的哈希值。

特殊的,当 i = 0 i = 0 i=0 时有:
h 0 = ∑ j = 0 c − 1 s e q j ∗ s c − j − 1 h_0 = \sum_{j=0}^{c-1}seq_j*s^{c-j-1} h0=j=0c1seqjscj1

一般的,当 i ≠ 0 i\ne 0 i=0 时有:
h i = h i − 1 ∗ s − s e q i − 1 ∗ s c + s e q i + c − 1 h_i = h_{i-1}*s - seq_{i-1}*s^{c} + seq_{i+c-1} hi=hi1sseqi1sc+seqi+c1

s s s 一般为大素数,比如 100007 100007 100007 等。

关于哈希冲突的问题:本题中 n n n的上限为 20000,如果用 用 uint64_t 存储哈希值的话,相当于把两万个球随机分到约 4 ∗ 1 0 10 4*10^{10} 41010个槽里面,一个槽分到多个球的概率还是很小的。

现在我们有了用一个整数表示一个子数组的能力,那么问题就变成了,对于每个 h i h_i hi 判断在 h 0 , h 1 , . . . , h i − c h_0,h_1, ..., h_{i-c} h0,h1,...,hic 中是否出现过。如果出现过则说明在 s e q seq seq 中存在长度为 c + 1 c+1 c+1 的相似子数组。

还有一个问题, c c c 的值如何确定呢?那当然是二分咯。

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>

using namespace std;

int seq[20000];

typedef unsigned long long uint64_t;

uint64_t seed = 10000007;
uint64_t pow_seed[20001];

struct Node {
  uint64_t hash;
  int pos;
  int next;
}node_pool[20000];

int top;

int head[20000];

bool find(uint64_t hash, int pos, int n, int c) {
  int slot = hash % n;
  for (int i = head[slot]; i != -1; i = node_pool[i].next) {
    if (node_pool[i].hash == hash && node_pool[i].pos + c < pos) {
      return true;
    }
  }
  return false;
}

void insert(uint64_t hash, int pos, int n) {
  int slot = hash % n;
  node_pool[top].hash = hash;
  node_pool[top].pos = pos;
  node_pool[top].next = head[slot];
  head[slot] = top++;
}

bool check(int *seq, int n, int c) {
  memset(head, -1, sizeof(int)*n);
  top = 0;

  uint64_t hash = 0;

  for (int i = 0; i < n; i++) {
    (hash *= seed) += seq[i];
    if (i >= c) {
      hash -= seq[i-c] * pow_seed[c];
      if (find(hash, i, n, c)) {
        return true;
      }
      insert(hash, i, n);
    }
  }

  return false;
}

int main() {
  pow_seed[0] = 1;
  for (int i = 1; i <= 10000; i++) {
    pow_seed[i] = pow_seed[i-1]*seed;
  }
  int n;
  while (scanf("%d", &n) != EOF && n != 0) {
    for (int i = 0; i < n; i++) {
      scanf("%d", seq + i);
    }
    for (int i = n-1; i >= 1; i--) {
      seq[i] -= seq[i-1];
    }
    //check(seq, n, 2);
    int L = 3, R = n/2;
    while (L <= R) {
      int mid = (L+R) >> 1;
      if (check(seq, n, mid)) {
        L = mid+1;
      } else {
        R = mid-1;
      }
    }
    printf("%d\n", L < 4 ? 0 : L);
  }
  return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值