POJ1743 Musical Theme

来源:互联网 发布:python中文手册 编辑:程序博客网 时间:2024/05/21 05:57

Musical Theme

Time Limit: 1000MS Memory Limit: 30000KTotal Submissions: 17813 Accepted: 6102

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

3025 27 30 34 39 45 52 60 69 79 69 60 52 45 39 34 30 26 22 1882 78 74 70 66 67 64 60 65 800

Sample Output

5

Hint

Use scanf instead of cin to reduce the read time.

题意:给一串含有n个音符的乐谱,找到变化相同长度大于5的最长的主旋律(就是找最长不重复子串)。
分析:09年论文的例题,因为题目中重复不一定要完全一致,一串数加上1个数等于另一个串也可以,所以要找出变化相同的串,变化相同用前一个减去后一个就行,然后再套上后缀数组的模板,这里要注意的是在da中传入的n是串的长度+1,求height数组要传入的n就是长度,我们把串相减处理了,所以我们要处理的那个串的长度是n-1,这也是后面结果为什么要加1的原因,最后把长度进行二分,变成判断性问题(就是论文上说的了),把排序好的后缀进行分组(本来就排序好了的),找出每组最大和最小sa,如果有一组满足最大和最小只差大于等于当前二分的长度(没写等于WA了几次),就说明存在。

#include<cstdio>#include<cstring>#include<algorithm>using namespace std;const int MAXN=20010;int ws[MAXN],wv[MAXN],wa[MAXN],wb[MAXN];int r[MAXN],sa[MAXN],s[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 *x=wa,*y=wb,i,j,p,*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=1,j=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++;    }    return;}int rank[MAXN],height[MAXN];void callheight(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;}bool judge(int len,int n){    int ll=sa[0];    int rr=sa[0];    for(int i=1;i<n;i++)    {        if(height[i]<len)        {            ll=sa[i];            rr=sa[i];            continue;        }        if(sa[i]<ll)            ll=sa[i];        if(sa[i]>rr)            rr=sa[i];        if(rr-ll>=len)            return 1;    }    return 0;}int main(){    int n,i,j;    while(scanf("%d",&n)==1&&n)    {        for(i=0;i<n;i++)            scanf("%d",&s[i]);        for(i=0;i<n-1;i++)            r[i]=s[i+1]-s[i]+100;        r[n-1]=0;        da(r,sa,n,189);        callheight(r,sa,n-1);        int ans=0;        int ll=0,rr=n+1;        while(ll<rr)        {            int mid=(ll+rr+1)>>1;            if(judge(mid,n))                ll=mid;            else                rr=mid-1;        }        if(ll>=4)        printf("%d\n",ll+1);        else            printf("0\n");    }    return 0;}


0 0
原创粉丝点击