POJ 1743Musical Theme

Posted bzmdAndy

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了POJ 1743Musical Theme相关的知识,希望对你有一定的参考价值。

 

Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 27889   Accepted: 9413

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

题目大意:

 给定一段长度为n的序列,数值在1~88之间,请问序列中最长的两个不相交的子串,且这两个子串的每个值的差相同,长度不小于5

首先我们做差分处理,然后问题转化为对这个序列找最长相同不相交子串。

做后缀数组后,可以二分答案,对于check的过程,我们可以把后缀按排名h[i]>=k的连续区间分为一段,如果这一段的最大值减最小值>=答案a,说明a可行。

输出a+1即可

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=20005;
int wa[N<<1],wb[N<<1],to[N],sa[N],h[N],sr[N];
bool sf(int n,int a)
{
    int mx=0,mi=n;
    for(int i=1;i<n;++i)
    {
        if(h[i]>=a)
        {
            mx=max(mx,max(sa[i],sa[i-1]));
            mi=min(mi,min(sa[i],sa[i-1]));
        }
        else mx=0,mi=n;
        if(mx-mi>=a) return 1;    
    }
    return 0;
}
int main()
{
    int n;
    while(scanf("%d",&n)&&n)
    {
        int *x=wa,*y=wb,m=200;
        for(int i=0;i<n;++i) scanf("%d",&y[i]);
        --n;
        for(int i=0;i<m;++i) to[i]=0;
        for(int i=0;i<n;++i) ++to[x[i]=y[i+1]-y[i]+88];
        for(int i=1;i<m;++i) to[i]+=to[i-1];
        for(int i=n-1;i>=0;--i) sa[--to[x[i]]]=i;
        for(int i=0;i<n;++i) sr[i]=x[i];
        for(int j=1,p=0;p<n;m=p,j<<=1)
        {
            p=0;
            for(int i=n-j;i<n;++i) y[p++]=i,x[i+j]=-1;
            for(int i=0;p<n;++i) if(sa[i]>=j) y[p++]=sa[i]-j;
            for(int i=0;i<m;++i) to[i]=0;
            for(int i=0;i<n;++i) ++to[x[y[i]]];
            for(int i=1;i<m;++i) to[i]+=to[i-1];
            for(int i=n-1;i>=0;--i) sa[--to[x[y[i]]]]=y[i];
            int *t=x;x=y,y=t;
            x[sa[0]]=0;
            for(int i=p=1;i<n;++i)
                y[sa[i]]!=y[sa[i-1]]||y[sa[i]+j]!=y[sa[i-1]+j]?x[sa[i]]=p++:x[sa[i]]=p-1;
        }
        for(int i=0,j,k=0;i<n;h[x[i++]]=k)
            if(!x[i]) k=0;
            else for(k?--k:0,j=sa[x[i]-1];i+k<n&&j+k<n&&sr[i+k]==sr[j+k];++k);
        int l=0,r=n/2+1;
        while(l<r-1)
        {
            int mid=(l+r)>>1;
            if(sf(n,mid)) l=mid; 
            else r=mid;
        }
        printf("%d\n",l<4?0:l+1);
    }
    return 0;
}

以上是关于POJ 1743Musical Theme的主要内容,如果未能解决你的问题,请参考以下文章

poj1743 Musical Theme

POJ-1743 Musical Theme(后缀数组)

poj 1743 Musical Theme

POJ 1743 Musical Theme ——后缀数组

POJ 1743. Musical Theme

POJ 1743-Musical Theme-后缀数组