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

ACM比赛整理

2021-12-27 15:22

按照通过数排序,今天开搞 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

样例解释

本题大意是,给出一个长度为 的数组 。需要求出两个「不重叠的」「相似的」子数组的最大长度。如果最大长度不小于,则输出最大长度,否则输出 0。

相似的定义是:两个子数组 长度相等,且存在整数 ,将每个 加上 后可以得到 ,即

比如样例中的 就是相似的。前者将每个元素都加上 即可得到后者。而且找不到更长的相似子数组,所以输出是

题解

主流的解法是利用「后缀数组」求解答案,今天介绍一个非主流的解法:「二分+哈希」,时间复杂度

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

如果两个数组 相似,设下标从 0 开始,不难发现,对于 有:

比如,两个数组 是相似的,前者加上 可以得到后者。

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

  • ->
  • ->

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

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

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

先介绍一中线性时间计算 个长度为 的子数组的哈希值的计算方法。

不妨设 是以 为起始位置的长度 的子数组的哈希值。

特殊的,当 时有:

一般的,当 时有:

一般为大素数,比如 等。

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

现在我们有了用一个整数表示一个子数组的能力,那么问题就变成了,对于每个 判断在 中是否出现过。如果出现过则说明在 中存在长度为 的相似子数组。

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

#include 
#include 
#include 
#include 

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, -1sizeof(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;
}

浏览 28
点赞
评论
收藏
分享

手机扫一扫分享

举报
评论
图片
表情
推荐
点赞
评论
收藏
分享

手机扫一扫分享

举报