poj 1743 Musical Theme(后缀自动机)

Musical Theme
Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 27471 Accepted: 9278

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

[Submit]   [Go Back]   [Status]   [Discuss]


题目大意:

    有N(1 <= N <=20000)个音符的序列来表示一首乐曲,每个音符都是1..88范围内的整数,现在要找一个重复的主题。“主题”是整个音符序列的一个子串,它需要满足如下条件:

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());
	}
}




  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值