POJ 1743 Musical Theme (后缀树组)

本文介绍了一种算法,用于从给定的旋律中找出最长的主题,即重复出现且不重叠的子序列。通过二分查找及后缀数组的高效处理,实现对大规模数据的有效分析。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

题目大意:将数组前后相减,求最长的重复子串,这两个子串不能重叠。

思路:这是论文上的一道题。首先二分答案,判断是否有一个长度为k的重复且不重叠的子串。然后将排好序的后缀分成若干组,每个组的height值都不小于k,如果该组sa值的最大值减最小值大于等于k,则说明有一个长度为k的不重叠子串。

#include<cstdio>
#include<cstring>
#include<vector>
#include<set>
#include<queue>
#include<map>
#include<iostream>
#include<stack>
#include<cctype>
#include<algorithm>
using namespace std;

const int maxn = 20000 + 10;
int a[maxn];
int t1[maxn], t2[maxn], c[maxn];
int Rank[maxn], height[maxn];
int r[maxn], sa[maxn];

bool cmp(int *r, int a, int b, int l)
{
    return r[a] == r[b] && r[a+l] == r[b+l];
}

void da(int str[], int sa[], int Rank[], int height[], int n, int m)
{
    n++;
    int i, j, p, *x=t1, *y=t2;
    for(i = 0; i < m; i++) c[i] = 0;
    for(i = 0; i < n; i++) c[ x[i] = str[i] ]++;
    for(i = 1; i < m; i++) c[i] += c[i-1];
    for(i = n-1; i >= 0; i--) sa[--c[x[i]]] = i;
    for(j = 1; j <= n; j <<= 1)
    {
        p = 0;
        for(i = n-j; i < n; i++) y[p++] = i;
        for(i = 0; i < n; i++) if(sa[i] >= j) y[p++] = sa[i] - j;
        for(int i = 0; i < m; i++) c[i] = 0;
        for(int i = 0; i < n; i++) c[x[y[i]]]++;
        for(int i = 1; i < m; i++) c[i] += c[i-1];
        for(i = n-1; i >= 0; i--) sa[--c[x[y[i]]]] = y[i];
        swap(x, y);
        p = 1; x[sa[0]] = 0;
        for(i = 1; i < n; i++)
            x[sa[i]] = cmp(y, sa[i-1], sa[i], j) ? p-1 : p++;
        if(p >= n) break;
        m = p;
    }
    int k = 0;
    n--;
    for(i = 0; i <= n; i++) Rank[sa[i]] = i;
    for(i = 0; i < n; i++)
    {
        if(k) k--;
        j = sa[Rank[i]-1];
        while(str[i+k] == str[j+k]) k++;
        height[Rank[i]] = k;
    }
}

int RMQ[maxn], mm[maxn], best[20][maxn];

void initRMQ(int n)
{
    mm[0] = -1;
    for(int i = 1; i <= n; i++)
        mm[i] = ((i&(i-1))==0) ? mm[i-1]+1 : mm[i-1];
    for(int i = 1; i <= n; i++) best[0][i] = 1;
    for(int i = 1; i <= mm[n]; i++)
        for(int j = 1; j+(1<<i)-1 <= n; j++)
        {
            int a = best[i-1][j];
            int b = best[i-1][j + (1<<(i-1))];
            if(RMQ[a] < RMQ[b]) best[i][j] = a;
            else best[i][j] = b;
        }
}

int askRMQ(int a, int b)
{
    int t;
    t = mm[b-a+1];
    b -= (1<<t)-1;
    a = best[t][a]; b = best[t][b];
    return RMQ[a] < RMQ[b] ? a : b;
}

int lcp(int a, int b)
{
    a = Rank[a], b = Rank[b];
    if(a > b) swap(a, b);
    return height[askRMQ(a+1, b)];
}

bool judge(int k, int n)
{
	int mi, mx;
	mi = mx = sa[1];
	for(int i = 2; i <= n; i++) {
		if(height[i] < k) mx = mi = sa[i];
		else {
			mx = max(mx, sa[i]);
			mi = min(mi, sa[i]);
			if(mx - mi >= k) return true;
		}
	}
	return false;
}

int main()
{
    int n;
    while(scanf("%d", &n) && n)
    {
    	for(int i = 0; i < n; i++) scanf("%d", a+i);
    	if(n < 5) { printf("0\n"); continue; }
        for(int i = 0; i < n-1; i++) a[i] = a[i+1] - a[i] + 100;
        a[n-1] = 0; n--;
        da(a, sa, Rank, height, n, 190);
        int L = 0, R = n, ans = 0;
        while(L <= R)
        {
        	int mid = (L + R) >> 1;
        	if(judge(mid, n)) { ans = mid; L = mid+1; }
        	else R = mid - 1;
        }
        ++ans;
        if(ans < 5) printf("0\n");
        else printf("%d\n", ans);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值