POJ 1743 Musical Theme 后缀数组+二分

来源:互联网 发布:mac电脑怎么卸载软件 编辑:程序博客网 时间:2024/05/17 04:39

Musical Theme
Time Limit: 1000MS Memory Limit: 30000KTotal Submissions: 30527 Accepted: 10205

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.

Source

LouTiancheng@POJ


终于开始学习拖了很久的后缀数组,原因是沈阳在线赛的A题没做出。


给一段在1到88之间的数字,求两段不重叠的相邻元素差值相同的元素的最长长度。要求长度大于5.


先把原数列的元素变为相邻元素差值,把数字当做字符串,这题就是要找两段不重叠的最长相同子串。

那么就可以二分答案。

对于每个答案k,用后缀数组的height分组,将所有两两之间height大于等于k的分为一组,看每一组有没有长度相差大于k的,有则成功找到长度为k的答案。


#include <cstdio>#include <string.h>#include <string> #include <map>#include <queue>#include <vector>#include <set>#include <algorithm>#include <math.h>#include <cmath>#include <stack>#define mem0(a) memset(a,0,sizeof(a))#define meminf(a) memset(a,0x3f,sizeof(a))using namespace std;typedef long long ll;typedef long double ld;const int maxn=100005,inf=0x3f3f3f3f;  const ll llinf=0x3f3f3f3f3f3f3f3f;   const ld pi=acos(-1.0L);  int wa[maxn],wb[maxn],wv[maxn],ws[maxn],sa[maxn],ranki[maxn],height[maxn];int s[maxn],a[maxn];int cmp(int *r,int a,int b,int l) {return r[a]==r[b]&&r[a+l]==r[b+l];}void build(int *r,int *sa,int n,int m) {int i,j,k,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=0;i<m;i++) ws[i]+=ws[i-1];for (i=n-1;i>=0;i--)     sa[--ws[x[i]]]=i;//sa[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;// w[i]表示第二关键字排名第i位的位置 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];t=x;x=y;y=t;p=1;x[sa[0]]=0;for (i=1;i<n;i++)     x[sa[i]]=cmp(y,sa[i-1],sa[i],j)?p-1:p++;//前后两个排名相近的字符串是否完全相同//x[]记录排名 }for (i=1;i<n;i++) ranki[sa[i]]=i;k=0; for (i=0;i<n-1;height[ranki[i++]]=k) {if (k) k--;for (j=sa[ranki[i]-1];r[i+k]==r[j+k];k++);}}bool check(int l,int n) {int i,mi,ma;mi=ma=sa[1];for (i=2;i<=n;i++) {if (height[i]<l) {if (ma-mi>l) return true;ma=mi=sa[i];} else {ma=max(ma,sa[i]);mi=min(mi,sa[i]);}}if (ma-mi>l) return true;return false;}int main() {int len;    scanf("%d",&len);    while (len) {    for (int i=0;i<len;i++) {    scanf("%d",&a[i]);    if (i!=0) s[i-1]=a[i]-a[i-1]+88;    }    s[len-1]=0;    build(s,sa,len,176);    int l=1,r=len,mid,ans=-1;    while (l<=r) {    mid=(l+r)/2;    if (check(mid,len-1)) ans=mid,l=mid+1; else r=mid-1;    }    ans++;    if (ans<5) ans=0;    printf("%d\n",ans);    scanf("%d",&len);    }return 0;}


原创粉丝点击