POJ 1743 Musical Theme

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!


【题目分析】
后缀数组+二分最优转判定 思路简直不要太好。由于有转调(同时加上或者减去一个值)只能记录下变化量,输出的时候再加上一。注意的是,最终结果小于4要输出量。
复制的题解如下

先二分答案,把题目变成判定性问题:判断是否存在两个长度为k的子串是相同的,且不重叠。解决这个问题的关键还是利用height数组。把排序后的后缀分成若干组,其中每组的后缀之间的height值都不小于k。容易看出,有希望成为最长公共前缀不小于k的两个后缀一定在同一组。然后对于每组后缀,只须判断每个后缀的sa值的最大值和最小值之差是否不小于k。如果有一组满足,则说明存在,否则不存在。整个做法的时间复杂度为O(nlogn)。
思路太好,写起来水道渠成,然后WA。最后加了小于5的判断才过。


【代码】

#include <cstring>
#include <cstdio>
#define maxn 20001
#define m(a) memset(a,0,sizeof a)
int wa[maxn],wb[maxn],wv[maxn],ws[maxn],sa[maxn],rank[maxn],height[maxn],s[maxn],n,a[maxn];
inline int cmp(int *r,int a,int b,int l)
{return r[a]==r[b]&&r[a+l]==r[b+l];}
inline void getsa(int *r,int *sa,int n,int m)
{
    int i,j,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 (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) ws[i]=0;
        for (i=0;i<n;++i) ws[wv[i]]++;
        for (i=1;i<m;++i) ws[i]+=ws[i-1];
        for (i=n-1;i>=0;--i) sa[--ws[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++;
    }
}
inline void gethi(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++);
}
inline int min(int a,int b)
{return a>b?b:a;}
inline int max(int a,int b)
{return a>b?a:b;}
inline bool test(int k)
{
    int minn=0x3f3f3f3f,maxx=-0x3f3f3f3f;
    int i=2;
    while (i<=n)
    {
        if (height[i]>=k)
        {
            minn=min(minn,sa[i-1]),maxx=max(maxx,sa[i-1]);
            while (height[i+1]>=k) ++i,minn=min(minn,sa[i-1]),maxx=max(maxx,sa[i-1]);
            minn=min(minn,sa[i]),maxx=max(maxx,sa[i]);
            if (maxx-minn>=k) return true;
        }
        minn=0x3f3f3f3f,maxx=-0x3f3f3f3f;
        i++;
    }
    return false;
}
int main()
{
    while (scanf("%d",&n)&&n)
    {
        for (int i=0;i<n;++i) scanf("%d",&a[i]); if (n<10){printf("0\n");continue;}
        for (int i=0;i<n-1;++i) s[i]=a[i+1]-a[i]+89;
        n--;
        s[n]=0;
        getsa(s,sa,n+1,200);
        gethi(s,n);
        int l=4,r=(n-1)/2;
        while (l<r)
        {
            int mid=(l+r+1)/2;
            if (test(mid)) l=mid;
            else r=mid-1;
        }
        printf("%d\n",l<4?0:l+1);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值