首页 > 代码库 > POJ 1743 Musical Theme(后缀数组)

POJ 1743 Musical Theme(后缀数组)

题目大意:给出n个数字。首先将这n个数前后做差,得到另一个长度是n-1的序列。求出这个序列的最长重复子串,且这些子串不能重叠。

PS:这题论文上有解析。

解题思路:先二分答案,把题目变成判断性问题:判断是否存在两个长度为k的字串是否相同的,且不重叠。解决这个问题的关键还是利用height数组。把排序后的后缀数组分成若干组,其中每组的后缀之间的height值都不小于k。

容易看出,有希望成为最长公共前缀不小于k的两个后缀一定在同一组。然后对于每组后缀,只须判断每个后缀的sa值的最大值和最小值之差是否不小于k。如果有一组满足,则说明存在,否则不存在。整个做法的时间复杂度为O(nlogn)

技术分享技术分享

论文上写的很详细了啊,不过需要注意细节,比如二分的边界,以及字符串的长度等等。。。

Musical Theme
Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 19851 Accepted: 6779

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.
#include <algorithm>
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <iomanip>
#include <stdio.h>
#include <string>
#include <queue>
#include <cmath>
#include <stack>
#include <ctime>
#include <map>
#include <set>
#define eps 1e-9
///#define M 1000100
///#define LL __int64
#define LL long long
///#define INF 0x7ffffff
#define INF 0x3f3f3f3f
#define PI 3.1415926535898
#define zero(x) ((fabs(x)<eps)?0:x)
#define mod 1000000007
#define Read() freopen("autocomplete.in","r",stdin)
#define Write() freopen("autocomplete.out","w",stdout)
#define Cin() ios::sync_with_stdio(false)

using namespace std;



inline int read()
{
    char ch;
    bool flag = false;
    int a = 0;
    while(!((((ch = getchar()) >= '0') && (ch <= '9')) || (ch == '-')));
    if(ch != '-')
    {
        a *= 10;
        a += ch - '0';
    }
    else
    {
        flag = true;
    }
    while(((ch = getchar()) >= '0') && (ch <= '9'))
    {
        a *= 10;
        a += ch - '0';
    }
    if(flag)
    {
        a = -a;
    }
    return a;
}
void write(int a)
{
    if(a < 0)
    {
        putchar('-');
        a = -a;
    }
    if(a >= 10)
    {
        write(a / 10);
    }
    putchar(a % 10 + '0');
}

const int maxn = 20010;
int wa[maxn], wb[maxn], wv[maxn], ws1[maxn];
int sa[maxn];
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 *sa,int n,int m)
{
    int i, j, p, *x = wa,*y = wb;

    for(i = 0; i<m; i++) ws1[i]=0;
    for(i = 0; i<n; i++) ws1[x[i]=r[i]]++;
    for(i = 1; i<m; i++) ws1[i]+=ws1[i-1];
    for(i = n-1; i>=0; i--) sa[--ws1[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++) ws1[i]=0;
        for(i = 0; i<n; i++) ws1[wv[i]]++;
        for(i = 1; i<m; i++) ws1[i]+=ws1[i-1];
        for(i = n-1; i>=0; i--) sa[--ws1[wv[i]]]=y[i];
        for(swap(x,y),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++;
    }
    return;
}

int rank[maxn],height[maxn];
void calheight(int *r,int *sa,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++);
    return;
}

int sqe[maxn];


bool judge(int mid, int n)
{
    int Max, Min;
    for(int i = 2; i <= n; i++)
    {
        Max = sa[i-1];
        Min = sa[i-1];
        while(height[i] >= mid)
        {
            Max = max(sa[i], Max);
            Min = min(sa[i], Min);
            i++;
        }
        if(Max - Min > mid) return true;
    }
    return false;
}

int main()
{
    int n;
    Cin();
    while(cin >>n)
    {
        if(!n) break;
        for(int i = 0; i < n; i++) cin >>sqe[i];
        if(n <= 9)
        {
            puts("0");
            continue;
        }
        for(int i = 0; i < n-1; i++) sqe[i] = sqe[i+1]-sqe[i]+88;
        sqe[n-1] = 0;

        da(sqe, sa, n, 200);
        calheight(sqe, sa, n-1);

        int l = 4;
        int r = n / 2;
        int ans = -1;
        while(l <= r)
        {
            int mid = (l+r)>>1;
            if(judge(mid, n))
            {
                l = mid+1;
                ans = mid;
            }
            else r = mid-1;
        }
        cout<<ans+1<<endl;
    }
    return 0;
}




POJ 1743 Musical Theme(后缀数组)