现在的位置: 首页 > 综合 > 正文

PKU1743 Musical Theme 楼教主男人八题之一

2013年05月22日 ⁄ 综合 ⁄ 共 2745字 ⁄ 字号 评论关闭

 

Musical Theme
Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 6021   Accepted: 2127

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.
后缀数组+二分=轻松加愉快!
#include<iostream>
#include<string.h>
#include<cmath>
#include<memory.h>
#include<algorithm>
using namespace std;
#define N 20010
int s[N]; // N > 256
int n, ranks[N], sa[N], height[N],tmp[N], top[N];
void makesa(){ // O(N * log N)
int i, j, len, na;
na = (n < 256 ? 256 : n);
memset(top, 0, na * sizeof(int));
for (i = 0; i < n ; i++) top[ ranks[i] = s[i] & 0xff ]++;
for (i = 1; i < na; i++) top[i] += top[i - 1];
for (i = 0; i < n ; i++) sa[ --top[ ranks[i] ] ] = i;
for (len = 1; len < n; len <<= 1) {
for (i = 0; i < n; i++) {
j = sa[i] - len; if (j < 0) j += n;
tmp[ top[ ranks[j] ]++ ] = j;
}
sa[ tmp[ top[0] = 0 ] ] = j = 0;
for (i = 1; i < n; i++) {
if (ranks[ tmp[i] ] != ranks[ tmp[i-1] ] ||
ranks[ tmp[i]+len ]!=ranks[ tmp[i-1]+len ])
top[++j] = i;
sa[ tmp[i] ] = j;
}
memcpy(ranks, sa , n * sizeof(int));
memcpy(sa , tmp, n * sizeof(int));
if (j >= n - 1) break;
}
}
void lcp(){ // O(4 * N)
int i, j, k;
for (j = ranks[height[i=k=0]=0]; i < n - 1; i++, k++)
while (k >= 0 && s[i] != s[ sa[j-1] + k ])
height[j] = (k--), j = ranks[ sa[j] + 1 ];
}
bool check(int mid)
{
int mi=0xfffffff,ma=0;
int i;
for(i=1;i<n;i++)
if(height[i]>=mid)
{
do
{
ma=max(ma,max(sa[i],sa[i-1]));
mi=min(mi,min(sa[i],sa[i-1]));
i ++;
}while(i<n&&height[i]>=mid);
if(ma-mi>=mid)
return true;
}
return false;
}
int main()
{
int i,j;
//char ss[N];
while(scanf("%d",&n),n)
{
for(i=0;i<n;i++)
scanf("%d",&s[i]);
//s[0]=s[i]+88;
for(i=1;i<n;i++)
s[i-1]=s[i]-s[i-1]+88;
s[n-1]=255;
//n++;
//s[0]=0;
s[n++]=0;
makesa();
lcp();
int ans=-1;
int l=4,r=n-1,mid;
while(l <= r)
{
mid=(l+r)>>1;
if(check(mid))
{
ans=mid;
l=mid+1;
}
else
r=mid-1;
}
printf("%d/n",ans+1);
}
return 0;
}

 

抱歉!评论已关闭.