Musical Theme
Time Limit: 20 Sec
Memory Limit: 256 MB
题目连接
http://poj.org/problem?id=1743
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:
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.
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
题意
让你找到最长的不相交重复子串
题解:
后缀数组,跑出height数组之后,然后直接二分然后O(n),check就好了
代码:
//qscqesze
#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
#include <map>
#include <stack>
typedef long long ll;
using namespace std;
//freopen("D.in","r",stdin);
//freopen("D.out","w",stdout);
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define maxn 25000
#define mod 10007
#define eps 1e-9
int Num;
char CH[];
//const int inf=0x7fffffff; //нчоч╢С
const int inf=0x3f3f3f3f;
inline ll read()
{
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
//************************************************************************************** int s[*maxn];
int a[maxn];
int n;
int sa[maxn], rank[maxn], height[maxn];
int wa[maxn], wb[maxn], wv[maxn], wd[maxn]; int cmp(int *r, int a, int b, int l){
return r[a] == r[b] && r[a+l] == r[b+l];
} void build_sa(int *r, int n, int m){ // 倍增算法 r为待匹配数组 n为总长度 m为字符范围
int i, j, p, *x = wa, *y = wb, *t;
for(i = ; i < m; i ++) wd[i] = ;
for(i = ; i < n; i ++) wd[x[i]=r[i]] ++;
for(i = ; i < m; i ++) wd[i] += wd[i-];
for(i = n-; i >= ; i --) sa[-- wd[x[i]]] = i;
for(j = , p = ; p < n; j *= , m = p){
for(p = , i = n-j; i < n; i ++) y[p ++] = i;
for(i = ; i < n; i ++) if(sa[i] >= j) y[p ++] = sa[i] - j;
for(i = ; i < n; i ++) wv[i] = x[y[i]];
for(i = ; i < m; i ++) wd[i] = ;
for(i = ; i < n; i ++) wd[wv[i]] ++;
for(i = ; i < m; i ++) wd[i] += wd[i-];
for(i = n-; i >= ; i --) sa[-- wd[wv[i]]] = y[i];
for(t = x, x = y, y = t, p = , x[sa[]] = , i = ; i < n; i ++){
x[sa[i]] = cmp(y, sa[i-], sa[i], j) ? p - : p ++;
}
}
} void calHeight(int *r, int n){ // 求height数组。
int i, j, k = ;
for(i = ; i <= n; i ++) rank[sa[i]] = i;
for(i = ; i < n; height[rank[i ++]] = k){
for(k ? k -- : , j = sa[rank[i]-]; r[i+k] == r[j+k]; k ++);
}
}
int check(int mid)
{
int Minn=sa[],Maxn=sa[];
for(int i=;i<=n;i++)
{
if(height[i]>=mid)
{
Minn=min(sa[i],Minn);
Maxn=max(sa[i],Maxn);
if(Maxn-Minn>mid)
return ;
}
else
Minn=Maxn=sa[i];
}
return ;
}
void init()
{
memset(a,,sizeof(a));
memset(s,,sizeof(s));
memset(sa,,sizeof(sa));
memset(rank,,sizeof(rank));
memset(height,,sizeof(height));
memset(wa,,sizeof(wa));
memset(wb,,sizeof(wb));
memset(wv,,sizeof(wv));
memset(wd,,sizeof(wd));
}
int main()
{
while(cin>>n)
{
if(n==)
break;
init();
for(int i=;i<n;i++)
a[i]=read();
n--;
for(int i=;i<n;i++)
s[i]=a[i+]-a[i]+;
s[n]=;
build_sa(s,n+,);
calHeight(s,n);
int l=,r=n,ans=;
while(l<=r)
{
int mid=(l+r)>>;
if(check(mid))
{
ans=max(ans,mid);
l=mid+;
}
else
r=mid-;
}
if(ans<)
ans=;
else
ans++;
cout<<ans<<endl;
}
}