[POJ1743]Musical Theme

[POJ1743]Musical Theme

试题描述

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! 

输入

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. 

输出

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.

输入示例

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

输出示例

5

数据规模及约定

见“试题描述

题解

差分后二分 + 后缀数组。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cctype>
#include <algorithm>
using namespace std;

int read() {
	int x = 0, f = 1; char c = getchar();
	while(!isdigit(c)){ if(c == '-') f = -1; c = getchar(); }
	while(isdigit(c)){ x = x * 10 + c - '0'; c = getchar(); }
	return x * f;
}

#define maxn 20010

int n, S[maxn], rank[maxn], height[maxn], sa[maxn], Ws[maxn];

bool cmp(int* a, int p1, int p2, int l) {
	if(p1 + l > n && p2 + l > n) return a[p1] == a[p2];
	if(p1 + l > n || p2 + l > n) return 0;
	return a[p1] == a[p2] && a[p1+l] == a[p2+l];
}
void ssort() {
	int *x = rank, *y = height;
	int m = 0;
	memset(Ws, 0, sizeof(Ws));
	for(int i = 1; i <= n; i++) Ws[x[i] = S[i]]++, m = max(m, x[i]);
	for(int i = 1; i <= m; i++) Ws[i] += Ws[i-1];
	for(int i = n; i; i--) sa[Ws[x[i]]--] = i;
	for(int j = 1, pos = 0; pos < n; j <<= 1, m = pos) {
		pos = 0;
		for(int i = n - j + 1; i <= n; i++) y[++pos] = i;
		for(int i = 1; i <= n; i++) if(sa[i] > j) y[++pos] = sa[i] - j;
		for(int i = 1; i <= m; i++) Ws[i] = 0;
		for(int i = 1; i <= n; i++) Ws[x[i]]++;
		for(int i = 1; i <= m; i++) Ws[i] += Ws[i-1];
		for(int i = n; i; i--) sa[Ws[x[y[i]]]--] = y[i];
		swap(x, y); pos = 1; x[sa[1]] = 1;
		for(int i = 2; i <= n; i++) x[sa[i]] = cmp(y, sa[i], sa[i-1], j) ? pos : ++pos;
	}
	return ;
}
void calch() {
	for(int i = 1; i <= n; i++) rank[sa[i]] = i;
	for(int i = 1, j, k = 0; i <= n; height[rank[i++]] = k)
		for(k ? k-- : 0, j = sa[rank[i]-1]; S[j+k] == S[i+k]; k++);
	return ;
}

bool check(int x) {
	int mn = sa[1], mx = sa[1];
	for(int i = 2; i <= n; i++)
		if(height[i] < x) mn = mx = sa[i];
		else {
			mn = min(mn, sa[i]); mx = max(mx, sa[i]);
			if(mx - mn >= x + 1) return 1;
		}
	return 0;
}

int main() {
	while(1) {
		n = read();
		if(!n) break;
		
		for(int i = 1; i <= n; i++) S[i] = read();
		for(int i = 1; i <= n; i++) S[i] = S[i+1] - S[i] + 88;
		n--;
		ssort();
		calch();
		int l = 0, r = n + 1;
		while(r - l > 1) {
			int mid = l + r >> 1;
			if(check(mid)) l = mid; else r = mid;
		}
		l++;
		
		if(l >= 5) printf("%d\n", l);
		else puts("0");
	}
	
	return 0;
}

后缀自动机建出来后在 parent 树上 dp 一下,每个节点 i 记录 mxr[i] 和 mnr[i] 分别表示该节点最大和最小的 right 值,那么最后这个节点的答案就是 min(mxr[i] - mnr[i], Max[i])。

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <algorithm>
using namespace std;

int read() {
	int x = 0, f = 1; char c = getchar();
	while(!isdigit(c)){ if(c == '-') f = -1; c = getchar(); }
	while(isdigit(c)){ x = x * 10 + c - '0'; c = getchar(); }
	return x * f;
}

#define maxn 40010
#define maxa 175
#define oo 2147483647

int n, S[maxn];

int rt, last, ToT, to[maxn][maxa], par[maxn], Max[maxn], rgt[maxn];
void extend(int x, int pos) {
	int p = last, np = ++ToT; Max[np] = Max[p] + 1; last = np; rgt[np] = pos;
	memset(to[np], 0, sizeof(to[np]));
	while(p && !to[p][x]) to[p][x] = np, p = par[p];
	if(!p){ par[np] = rt; return ; }
	int q = to[p][x];
	if(Max[q] == Max[p] + 1){ par[np] = q; return ; }
	int nq = ++ToT; Max[nq] = Max[p] + 1; rgt[nq] = 0;
	memcpy(to[nq], to[q], sizeof(to[q]));
	par[nq] = par[q];
	par[q] = par[np] = nq;
	while(p && to[p][x] == q) to[p][x] = nq, p = par[p];
	return ;
}

int sa[maxn], Ws[maxn], mxr[maxn], mnr[maxn];
void build() {
	memset(Ws, 0, sizeof(Ws));
	for(int i = 1; i <= ToT; i++) Ws[n-Max[i]]++;
	for(int i = 1; i <= n; i++) Ws[i] += Ws[i-1];
	for(int i = ToT; i; i--) sa[Ws[n-Max[i]]--] = i;
	for(int i = 1; i <= ToT; i++)
		if(!rgt[i]) mxr[i] = -1, mnr[i] = oo;
		else mxr[i] = mnr[i] = rgt[i];
	return ;
}

int main() {
	while(1) {
		n = read(); if(!n) break;
		for(int i = 1; i <= n; i++) S[i] = read(); n--;
		for(int i = 1; i <= n; i++) S[i] = S[i+1] - S[i] + 87;
//		for(int i = 1; i <= n; i++) printf("%d%c", S[i], i < n ? ' ' : '\n');
		rt = last = ToT = 1; memset(to[1], 0, sizeof(to[1]));
		for(int i = 1; i <= n; i++) extend(S[i], i);
//		for(int i = 1; i <= ToT; i++) printf("%d%c", par[i], i < ToT ? ' ' : '\n');
		build();
		int ans = 0;
		for(int i = 1; i <= ToT; i++) {
			int u = sa[i];
			mxr[par[u]] = max(mxr[par[u]], mxr[u]);
			mnr[par[u]] = min(mnr[par[u]], mnr[u]);
			ans = max(ans, min(mxr[u] - mnr[u] - 1, Max[u]));
		}
		ans++;
		printf("%d\n", ans >= 5 ? ans : 0);
	}
	
	return 0;
}

 

转载于:https://www.cnblogs.com/xiao-ju-ruo-xjr/p/6525576.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值