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:
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 |
[Submit] [Go Back] [Status] [Discuss]
题目大意:
1.长度至少为5个音符
2.在乐曲中重复出现(可能经过转调,“转调”的意思是主题序列中每个音符都被加上或减去了同一个整数值。)
3.重复出现的同一主题不能有公共部分。
题解:后缀自动机。
因为转调的问题我们需要对于相邻两项作差得到的序列进行求解。
后缀自动机上的每个状态都有一个right集合代表到达该状态的子串在原串中出现位置的右端点。所以我们只需要知道每个状态最靠左和最靠右的端点分别在哪里,如果两点直接的差不小于该点的l[i]就说明这个该位置代表的子串重复出现且没有重合,然后用l[i]更新答案即可。
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
#include<cmath>
#define N 40003
using namespace std;
int n,m,cnt,p,q,np,nq,last,root,a[N],v[N],pos[N];
int fa[N],l[N],ch[N][180],mx[N],mn[N];
void extend(int x)
{
int c=a[x];
p=last; np=++cnt; last=np;
l[np]=x;
for(;p&&!ch[p][c];p=fa[p]) ch[p][c]=np;
if (!p) fa[np]=root;
else {
q=ch[p][c];
if (l[q]==l[p]+1) fa[np]=q;
else{
nq=++cnt; l[nq]=l[p]+1;
memcpy(ch[nq],ch[q],sizeof ch[nq]);
fa[nq]=fa[q];
fa[q]=fa[np]=nq;
for (;ch[p][c]==q;p=fa[p]) ch[p][c]=nq;
}
}
}
int solve()
{
memset(v,0,sizeof(v));
for (int i=1;i<=cnt;i++) v[l[i]]++;
for (int i=1;i<=n;i++) v[i]+=v[i-1];
for (int i=1;i<=cnt;i++) pos[v[l[i]]--]=i;
p=1;
memset(mx,0,sizeof(mx));
memset(mn,127,sizeof(mn));
for (int i=1;i<=n;i++) {
p=ch[p][a[i]];
mx[p]=mn[p]=i;
}
for (int i=cnt;i;i--){
int t=pos[i];
mx[fa[t]]=max(mx[fa[t]],mx[t]);
mn[fa[t]]=min(mn[fa[t]],mn[t]);
}
int ans=0;
for (int i=1;i<=cnt;i++)
if (mx[i]-mn[i]>=l[i])
ans=max(ans,l[i]);
ans++;
if (ans<5) ans=0;
return ans;
}
int main()
{
freopen("a.in","r",stdin);
while (true) {
scanf("%d",&n);
if (!n) break;
for (int i=1;i<=n;i++) scanf("%d",&a[i]);
for (int i=1;i<n;i++) a[i]=a[i+1]-a[i]+88;
n--;
memset(l,0,sizeof(l));
memset(fa,0,sizeof(fa));
memset(ch,0,sizeof(ch)); cnt=0;
last=root=++cnt;
for (int i=1;i<=n;i++) extend(i);
printf("%d\n",solve());
}
}