`
20386053
  • 浏览: 432217 次
文章分类
社区版块
存档分类
最新评论

POJ 1743 Musical Theme

 
阅读更多

最长不相交重复子串,转换成判定性问题,二分枚举长度再判断。。。。。


Time Limit:1000MS Memory Limit:30000KB 64bit IO Format:%I64d & %I64u

[] [Go Back] [Status]

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

[] [Go Back] [Status]


#include <iostream>
#include <cstring>
#include <cstdio>

using namespace std;

const int maxn=2e4+200;

int sa[maxn],rank[maxn],rank2[maxn],height[maxn],c[maxn],*x,*y,s[maxn],n;

void radix_sort(int n,int sz)
{
    memset(c,0,sizeof(c));
    for(int i=0;i<n;i++) c[x[y[i]]]++;
    for(int i=1;i<sz;i++) c[i]+=c[i-1];
    for(int i=n-1;i>=0;i--) sa[--c[x[y[i]]]]=y[i];
}

void get_sa(int *s,int n,int sz=222)
{
    x=rank,y=rank2;
    for(int i=0;i<n;i++)
        x[i]=s[i],y[i]=i;
    radix_sort(n,sz);
    for(int len=1;len<n;len<<=1)
    {
        int yid=0;
        for(int i=n-len;i<n;i++) y[yid++]=i;
        for(int i=0;i<n;i++) if(sa[i]>=len) y[yid++]=sa[i]-len;

        radix_sort(n,sz);

        swap(x,y);
        x[sa[0]]=yid=0;

        for(int i=1;i<n;i++)
        {
            if(y[sa[i]]==y[sa[i-1]]&&sa[i-1]+len<n&&sa[i]+len<n&&y[sa[i]+len]==y[sa[i-1]+len])
                x[sa[i]]=yid;
            else x[sa[i]]=++yid;
        }

        sz=yid+1;
        if(sz>=n) break;
    }

    for(int i=0;i<n;i++)
        rank[i]=x[i];
}

void get_height(int * s,int n)
{
    int k=0;height[0]=0;
    for(int i=0;i<n;i++)
    {
        if(rank[i]==0) continue;
        k=max(0,k-1);
        int j=sa[rank[i]-1];
        while(i+k<n&&j+k<n&&s[i+k]==s[j+k]) k++;
        height[rank[i]]=k;
    }
}

bool solve(int x)
{
    int mmx=0,mmi=0;
    for(int i=1;i<n;i++)
    {
        if(height[i]<x) mmx=mmi=sa[i];
        else
        {
            mmx=max(mmx,sa[i]);
            mmi=min(mmi,sa[i]);
            if(mmx-mmi>=x) return true;
        }
    }
    return false;
}

int main()
{
    while(scanf("%d",&n)!=EOF&&n)
    {
        int last,now;
        scanf("%d",&last);
        n--;
        for(int i=0;i<n;i++,last=now)
        {
            scanf("%d",&now);
            s[i]=now-last+100;
        }
        get_sa(s,n); get_height(s,n);
        int low=0,height=n/2,mid,ans;
        while(low<=height)
        {
            mid=(low+height)>>1;
            if(solve(mid)) ans=mid,low=mid+1;
            else height=mid-1;
        }
        ans=(ans<4||n<10)?0:ans+1;
        printf("%d\n",ans);
    }
    return 0;
}





分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics