您的位置:首页 > 其它

poj1743 Musical Theme 后缀数组之不可重叠最长公共子串

2017-02-10 17:04 513 查看
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

Hint

Use scanf instead of cin to reduce the read time.
Source
LouTiancheng@POJ

题意:音符的问题,这里介绍了一种主题

1、长度至少为5个音符。

2、在乐曲中重复出现。(可能经过转调,“转调”的意思是主题序列中每个音符都被加上或减去了同一个整数值)

3、重复出现的同一主题不能有公共部分。
思路:
如果只是单纯的相同直接后缀数组求不可重叠最长公共子串,但是现在有一个问题就是能够调转后成相同串的串也看成是相同的串,所以这里需要转化一下,求得相邻元素间的差值,然后再求差值数组的不可重叠最长公共子串

不可重叠最长公共子串的解法:
也是二分结果,然后变成判断性问题,然后就要用到height数组,根据height数组的大小分成几个组,然后只要该组中的最大值最小值的差值>=x,此时x成立
ac代码:
#include<iostream>

#include <cstdio>

#include <cmath>

using namespace std;

const int MAX = 20050;

const int M = 20000;

int n, k, num[MAX];

int sa[MAX], rank[MAX], height[MAX];

int wa[MAX], wb[MAX], wv[MAX], wd[MAX];

int cmp(int *r, int a, int b, int l)

{

    return r[a] == r[b] && r[a+l] == r[b+l];

}

void da(int *r, int n, int m)

{

    int i, j, p, *x = wa, *y = wb, *t;

    for(i = 0; i < m; i ++) wd[i] = 0;

    for(i = 0; i < n; i ++) wd[x[i]=r[i]] ++;

    for(i = 1; i < m; i ++) wd[i] += wd[i-1];

    for(i = n-1; i >= 0; i --) sa[-- wd[x[i]]] = i;

    for(j = 1, p = 1; p < n; j *= 2, m = p)

    {

        for(p = 0, 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(i = 0; i < n; i ++) wv[i] = x[y[i]];

        for(i = 0; i < m; i ++) wd[i] = 0;

        for(i = 0; i < n; i ++) wd[wv[i]] ++;

        for(i = 1; i < m; i ++) wd[i] += wd[i-1];

        for(i = n-1; i >= 0; i --) sa[-- wd[wv[i]]] = y[i];

        for(t = x, x = y, y = t, p = 1, x[sa[0]] = 0, i = 1; i < n; i ++)

        {

            x[sa[i]] = cmp(y, sa[i-1], sa[i], j) ? p - 1 : p ++;

        }

    }

}

void calHeight(int *r, int n)

{

    int i, j, k = 0;

    for(i = 1; i <= n; i ++) rank[sa[i]] = i;

    for(i = 0; i < n; height[rank[i ++]] = k)

    {

        for(k ? k -- : 0, j = sa[rank[i]-1]; r[i+k] == r[j+k]; k ++);

    }

}

int check(int len)

{

    int maxn,minn;

    for(int i=1;i<=n;i++)

    {

        if(height[i]<len)

        maxn=minn=sa[i];

        else

        {

            maxn=max(sa[i],maxn);

            minn=min(sa[i],minn);

            if(maxn-minn>=len)

            return 1;

        }

    }

    return 0;

}

int main()

{

    int x;

    while(scanf("%d",&x)&&x!=0)

    {

        for(int i=0;i<x;i++)

        scanf("%d",&num[i]);

        for(int i=0;i<x-1;i++)

        num[i]=num[i+1]-num[i]+90;

        n=x-1;

        num
=0;

        da(num,n+1,M);

        calHeight(num,n);

        int l=1,r=n,mid;

        while(l<=r)

        {

            mid=(l+r)/2;

            if(check(mid))

            {

                l=mid+1;

            }

            else

            r=mid-1;

        }

        if(l>=5)

        printf("%d\n",l);

        else

        printf("0\n");

    }

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: