poj 1743 Musical Theme(后缀数组+二分答案+height数组分组)

poj 1743 Musical Theme(后缀数组+二分答案+height数组分组)
Time Limit: 1000ms Memory Limit: 300000kB

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.


首先本题由于旋律可以升调降调,所以大致思路是应该做差,然后取出差序列中不重叠的子字符串长度最大值。但是这样一来就有两个细节没有考虑,一是不重叠子字符串对应做差后应该是不重叠不相邻而非不重叠,二是做差会出现负数不利于基数排序,所以统一加一个数,那么数据范围处理必须谨慎一点。
接下来先用后缀数组求出sa,rank,height等信息,再二分答案进行判定,判定依据为每一个互相之间公共前缀大于等于l的组内元素是否存在两个不相邻不重叠。
刚刚学树状数组不是很熟悉,所以调试了一下午。以后希望能越来越熟练少犯低级错误。


Accepted    2440kB  108ms   1803 B  G++ 
#include<stdio.h>

const int MAX_N=20000;
const int RANGE=88;
const int SMALL_SIZE=5;

typedef int array_type[MAX_N];

array_type r,wa,wb,ws,sa,rank,h;

bool valid(int n,int l)
{ 
    int min=n,max=0;
    for (int i=1;i<=n;i++)
    {
        if (h[i]>=l)
        {
            if (sa[i-1]<min) min=sa[i-1];
            if (sa[i-1]>max) max=sa[i-1];
            if (sa[i]<min) min=sa[i];
            if (sa[i]>max) max=sa[i];
        }
        else
        {
            if (max-min>l) 
                return true;
            max=0;
            min=n;
        }
    }   
    return (max-min>l);
}

void findh(int n)
{
    int i,*x,*y,k=0;
    for (i=1;i<=n;i++) rank[sa[i]]=i;
    for (i=0;i<n;i++)
    {
        x=r+sa[rank[i]-1];
        y=r+i;
        if (k>0) k--;
        while (x[k]==y[k]) k++;
        h[rank[i]]=k;
    }
    return;
}

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

void da(int n,int m)
{
    int i,l,p,*x=wa,*y=wb,*t;
    for (i=0;i<m;i++) ws[i]=0;
    for (i=0;i<n;i++) ws[x[i]=r[i]]++;
    for (i=1;i<m;i++) ws[i]+=ws[i-1];
    for (i=n-1;i>=0;i--) sa[--ws[x[i]]]=i;
    for (p=0,l=1;p<n;l*=2,m=p)
    {
        for (p=0,i=n-l;i<n;i++) y[p++]=i;
        for (i=0;i<n;i++) if (sa[i]>=l) y[p++]=sa[i]-l;
        for (i=0;i<m;i++) ws[i]=0;
        for (i=0;i<n;i++) ws[x[y[i]]]++;
        for (i=1;i<m;i++) ws[i]+=ws[i-1];
        for (i=n-1;i>=0;i--) sa[--ws[x[y[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],sa[i-1],l)?p-1:p++;
    }
    return;
}

int main()
{
    int n,L,R,mid;
    while (scanf("%d",&n)&&n)
    {
        for (int i=0;i<n;i++)
            scanf("%d",&r[i]);
        if (n<10)
        {
            printf("0\n");
            continue;
        }
        r[n]=r[n-1];
        for (int i=0;i<n;i++)
            r[i-1]=r[i]-r[i-1]+RANGE;
        n--;
        da(n+1,RANGE*2);
        findh(n);
        if (!valid(n,4))
        {
            printf("0\n");
            continue;
        }
        R=n/2;
        L=4;
        while (R-L>SMALL_SIZE)
        {
            mid=(L+R)/2;
            if (valid(n,mid))
                L=mid;
            else
                R=mid;
        }
        for (;valid(n,L);L++);
            printf("%d\n",L);
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值