【poj 1743】 Musical Theme(后缀数组)

来源:互联网 发布:python和java和php 编辑:程序博客网 时间:2024/04/29 02:20

Musical Theme

Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 24173 Accepted: 8156

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

传说中楼爷的男人八题。。。。。
题意:求数列最长等势差的长度(不重复!)
Solution:
1.n数列相减后n-1个差,对应的最长公共子串;
2.不重复!—–>二分长度算合法;
最相似的子串排名肯定是最接近的
判断(x):以height【i】(j)—1;合法即可sa[i]-sa[j])>=x
height[j]>=x
两种C()判定:o(n)搜连续合法的解

#include<iostream>#include<stdio.h> #include<string.h>using namespace std;int wa[20050],wb[20050],cnt[20050],r[20050],sa[20050];int rank[20050];int ans=0;int n;int abs(int x){    if(x<0) return -x;     return x;}void DA(int n,int m){    int *x=wa,*y=wb;    for(int i=0;i<m;i++) cnt[i]=0;    for(int i=0;i<n;i++) cnt[x[i]=r[i]]++;    for(int i=1;i<m;i++) cnt[i]+=cnt[i-1];    for(int i=n-1;i>=0;i--)sa[--cnt[x[i]]]=i;    for(int p=1,j=1;p<n;j*=2,m=p)    {        p=0;         for(int i=n-j;i<n;i++) y[p++]=i;        for(int i=0;i<n;i++) if(sa[i]>=j) y[p++]=sa[i]-j;        for(int i=0;i<m;i++) cnt[i]=0;        for(int i=0;i<n;i++) cnt[x[y[i]]]++;        for(int i=1;i<m;i++) cnt[i]+=cnt[i-1];        for(int i=n-1;i>=j;i--) sa[--cnt[x[y[i]]]]=y[i];        swap(x,y);        x[sa[0]]=0;        p=1;        for(int i=1;i<n;i++)        x[sa[i]]=((y[sa[i]]==y[sa[i-1]])&&y[sa[i]+j]==y[sa[i-1]+j])?p-1:p++;     }}int rk[20050],height[20050];void cal_h(){    for(int i=1;i<=n;i++) rank[sa[i]]=i;    int k=0;    int j=0;    for(int i=0;i<n;height[rank[i++]]=k)        for(k?k--:0,j=sa[rank[i]-1];r[i+k]==r[j+k];k++);}bool C(int x,int len){    for(int i=2;i<=len;i++)    {        if(height[i]<x) continue;        for(int j=i-1;j>=2;j--)        {            if(abs(sa[i]-sa[j])>=x) return 1;            if(height[j]<x) break;         }    }       return 0;   }//bool C(int x,int len)//{    //int ll=200005,rr=-200005;    //int flag=0;    //for(int i=2;i<=n;i++)    //{         //if(height[i]>=x)        //{            //ll=min(ll,sa[i]);            //ll=min(ll,sa[i-1]);            //rr=max(rr,sa[i]);            //rr=max(rr,sa[i-1]);                       //  }        //else         //{            //if(rr-ll>=x) return 1;            //ll=200005,rr=-200005;        //}    //}    //if(rr-ll>=x) return 1;    //return 0;  //}void solve(){           int ls=1,rs=n;        while(rs>=ls)        {            int mid=(rs+ls)>>1;            if(C(mid,n)) ls=mid+1,ans=mid;            else rs=mid-1;        }}int main(){    while(scanf("%d",&n))    {        if(n==0) return 0;        ans=0;        for(int i=0;i<n;i++)        scanf("%d",&r[i]);        if(n<10)        {            printf("0\n");            continue;        }          for(int i=0;i<n-1;i++)         r[i]=r[i+1]-r[i]+90;          r[n]=0;        DA(n+1,200);        cal_h();        solve();        if(ans<4) printf("0\n");        else printf("%d\n",ans+1);    }}
3 0
原创粉丝点击