首页 > 其他 > 详细

poj1743

时间:2014-07-23 16:15:21      阅读:310      评论:0      收藏:0      [点我收藏+]
Musical Theme
Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 18091   Accepted: 6203

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.
 
 
把题目变形一下。要音律相等即两个音符间的差相等。把a[i]:=a[i+1]-a[i];后,直接用sa找最长不重复字串即可,
但是要加个特判,因为变形后的字串不重复并不代表原串不重复
eg.变形后的串为1 1 1 1 1 1 1 1
你会找到1-4和5-8
但这两个串代表原串的1-5和5-9出现了重复
 
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<cstring>

using namespace std;

int x,n,i,xzq,mm;
int a[20011],rank[20011],sa[20011],h[20011],b[20011];
int co[20011],nr[20011],s[20011];

void sort(int *a)
{
    int i,mn;
    if(mm>n)mn=mm;
    else mn=n;
    for(i=0;i<=mn;i++)co[i]=0;
    for(i=1;i<=n;i++)co[a[i]+1]++;
    for(i=1;i<=mn;i++)co[i]+=co[i-1];
    for(i=1;i<=n;i++)nr[i]=0;
    for(i=1;i<=n;i++){
        co[a[sa[i]]]++;
        nr[co[a[sa[i]]]]=sa[i];
    }
    for(i=1;i<=n;i++)sa[i]=nr[i];
}

void getrank()
{
    int i;
    x=0;
    for(i=1;i<=n;i++){
        if(i==1||a[sa[i]]!=a[sa[i-1]]||b[sa[i]]!=b[sa[i-1]])x++;
        rank[sa[i]]=x;
    }
}

void Suffix()
{
    int i,l,last,j,k;
    for(i=1;i<=n;i++){
        sa[i]=i;
        b[i]=0;
    }
    sort(a);
    getrank();
    l=1;
    while(x!=n){
        for(i=1;i<=n;i++){
            a[i]=rank[i];
            if(i+l<=n)b[i]=rank[i+l];
            else b[i]=0;
        }
        sort(b);
        sort(a);
        getrank();
        l*=2;
    }
    last=0;
    for(i=1;i<=n;i++){
        if(last)last--;
        if(rank[i]==1)continue;
        j=i;
        k=sa[rank[i]-1];
        while(j+last<=n&&k+last<=n&&s[j+last]==s[k+last])last++;
        h[rank[i]]=last;
    }
}

bool check(int x)
{
    int i,minn,maxn;
    minn=2147483647;
    maxn=0;
    for(i=2;i<=n+1;i++){
        if(sa[i-1]<minn)minn=sa[i-1];
        if(sa[i-1]>maxn)maxn=sa[i-1];
        if(h[i]<x){
            if(maxn-minn>x)return true;//(必须是严格大于,这就是之前说的那个特判)
            maxn=0;
            minn=2147483647;
        }
    }
    return false;
}

void Work()
{
    int l,r,mid;
    l=1;
    r=n;
    while(l<=r){
        mid=(l+r)/2;
        if(check(mid))l=mid+1;
        else r=mid-1;
    }
    xzq=r;
}

int main()
{
    while(true){
        scanf("%d",&n);
        if(n==0)break;
        memset(sa,0,sizeof(sa));
        memset(h,0,sizeof(h));
        memset(rank,0,sizeof(rank));
        for(i=1;i<=n;i++)scanf("%d",&a[i]);
        mm=0;
        for(i=1;i<n;i++){
            a[i]=a[i+1]-a[i]+100;
            if(a[i]>mm)mm=a[i];
            s[i]=a[i];
        }
        mm++;
        n--;
        a[n]=0;
        Suffix();
        Work();
        xzq++;
        if(xzq<5)printf("0\n");
        else printf("%d\n",xzq);
    }
}

 

poj1743,布布扣,bubuko.com

poj1743

原文:http://www.cnblogs.com/applejxt/p/3863091.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!