POJ1743 Musical Theme

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.

Source

 
 
正解:后缀数组
解题报告:
  首先声明,这道题是一道模板题(水题)。
  罗穗骞的论文的例题。中午的时候,我满心欢喜的打算下午要切掉至少5道后缀数组题,然后被这道题卡了一个下午加一个晚上。
  思想确实简单,但我就是无限wa,迷之wa。。。几个小时之后,各种努力之后,最后发现,特判的时候没有读数。。。
  多么痛的领悟。半天时间就这么报废了。。。我很不开心,不讲题解了,直接看代码吧。
 
 //It is made by jump~
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <ctime>
#include <vector>
#include <queue>
#include <map>
#include <set>
#ifdef WIN32
#define OT "%I64d"
#else
#define OT "%lld"
#endif
using namespace std;
typedef long long LL;
const int MAXN = ;
int ch[MAXN];
int n,m;
int wa[MAXN],wb[MAXN],c[MAXN];
int rank[MAXN],height[MAXN];
int sa[MAXN];
int ans; inline int getint()
{
int w=,q=;
char c=getchar();
while((c<'' || c>'') && c!='-') c=getchar();
if (c=='-') q=, c=getchar();
while (c>='' && c<='') w=w*+c-'', c=getchar();
return q ? -w : w;
} inline void da(int m,int n){
int i,*x=wa,*y=wb;
for(i=;i<=m;i++) c[i]=;
for(i=;i<=n;i++) c[x[i]=ch[i]]++;
for(i=;i<=m;i++) c[i]+=c[i-];
for(i=n;i>=;i--) sa[c[x[i]]--]=i;
for(int k=,p;k<=n;k=k*) {
p=;
for(i=n-k+;i<=n;i++) y[++p]=i;
for(i=;i<=n;i++) if(sa[i]>k) y[++p]=sa[i]-k;
for(i=;i<=m;i++) c[i]=;
for(i=;i<=n;i++) c[x[y[i]]]++;
for(i=;i<=m;i++) c[i]+=c[i-];
for(i=n;i>=;i--) sa[c[x[y[i]]]--]=y[i];
swap(x,y); x[sa[]]=; p=;
for(i=;i<=n;i++) x[sa[i]]=(y[sa[i-]]==y[sa[i]] && y[sa[i-]+k]==y[sa[i]+k])?p:++p;
if(p==n) break; m=p;
}
} inline void calheight(){
int i,j,k=;
for(i=;i<=n;i++) {
if(k) k--;
j=sa[rank[i]-];
while(ch[i+k]==ch[j+k]) k++;
height[rank[i]]=k;//是rank[i]!!!
}
} inline bool check(int x){
int i=,minl,maxl;
while() {
while(i<=n && height[i]<x) i++;
if(i>n) break;
minl=maxl=sa[i-];
while(i<=n && height[i]>=x) minl=min(minl,sa[i]),maxl=max(maxl,sa[i]),i++;
if(maxl-minl>x) return true;//不能等号!!!记录的是差
}
return false;
} inline void work(){
while() {
n=getint(); if(n==) break;
memset(ch,,sizeof(ch)); memset(sa,,sizeof(sa));
memset(wa,,sizeof(wa)); memset(wb,,sizeof(wb));
memset(height,,sizeof(height)); memset(rank,,sizeof(rank));
for(int i=;i<=n;i++) ch[i]=getint();
if(n<) { printf("0\n"); continue; }
for(int i=;i<n;i++) ch[i]=ch[i+]-ch[i],ch[i]+=;
ch[n]=;
da(,n);
for(int i=;i<=n;i++) rank[sa[i]]=i;
calheight();
int l=,r=n,mid;
while(l<r) {
mid=(l+r)/;
if(check(mid)) l=mid+;
else r=mid;
}
if(l<=) printf("0\n");
else printf("%d\n",l);
}
} int main()
{
work();
return ;
}
上一篇:Ubuntu下ssh连接在服务端显示图形界面


下一篇:Unity使用C#实现简单Scoket连接及服务端与客户端通讯