POJ1743-Musical Theme

Musical Theme
Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 31286 Accepted: 10437

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



题意:用n个音符的序列来表示一首乐曲,每个音符都是1..88范围内的整数,找出一个重复的子串,满足:1.长度至少为5个音符。2.在乐曲中重复出现(就是出现过至少两次)。(可能经过转调,“转调”的意思是主题序列中每个音符都被加上或减去了同一个整数值) 3.重复出现的同一主题不能有公共部分。

解题思路:难点就是对“转调”的处理,其实很容易可以发现同加同减的话差值还是不变,所以把原串变为两两间的差值再做出后缀数组,然后二分来验证答案



#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <vector>
#include <bitset>
#include <functional>

using namespace std;

#define LL long long
const int INF = 0x3f3f3f3f;
const int N = 200010;

struct Sa
{
	int s[N];
	int rk[2][N], sa[N], h[N], w[N], now, k, n;
	int rmq[N][20], lg[N], bel[N];

	void GetS(int x)
	{
		for (int i = 1; i <= x; i++) scanf("%d", &s[i]);
		for (int i = 1; i < x; i++) s[i] = s[i + 1] - s[i] + 88;
		n = x - 1;
	}

	void getsa(int z, int &m)
	{
		int x = now, y = now ^= 1;
		for (int i = 1; i <= z; i++) rk[y][i] = n - i + 1;
		for (int i = 1, j = z; i <= n; i++)
			if (sa[i]>z) rk[y][++j] = sa[i] - z;
		for (int i = 1; i <= m; i++) w[i] = 0;
		for (int i = 1; i <= n; i++) w[rk[x][rk[y][i]]]++;
		for (int i = 1; i <= m; i++) w[i] += w[i - 1];
		for (int i = n; i >= 1; i--) sa[w[rk[x][rk[y][i]]]--] = rk[y][i];
		for (int i = m = 1; i <= n; i++)
		{
			int *a = rk[x] + sa[i], *b = rk[x] + sa[i - 1];
			rk[y][sa[i]] = *a == *b&&*(a + z) == *(b + z) ? m - 1 : m++;
		}
	}

	void getsa(int m)
	{
		now = rk[1][0] = sa[0] = s[0] = 0;
		for (int i = 1; i <= m; i++) w[i] = 0;
		for (int i = 1; i <= n; i++) w[s[i]]++;
		for (int i = 1; i <= m; i++) rk[1][i] = rk[1][i - 1] + (bool)w[i];
		for (int i = 1; i <= m; i++) w[i] += w[i - 1];
		for (int i = 1; i <= n; i++) rk[0][i] = rk[1][s[i]];
		for (int i = 1; i <= n; i++) sa[w[s[i]]--] = i;
		rk[1][n + 1] = rk[0][n + 1] = 0;
		for (int x = 1, y = rk[1][m]; x <= n&&y <= n; x <<= 1) getsa(x, y);
		for (int i = 1, j = 0; i <= n; h[rk[now][i++]] = j ? j-- : j)
		{
			if (rk[now][i] == 1) continue;
			int k = n - max(sa[rk[now][i] - 1], i);
			while (j <= k&&s[sa[rk[now][i] - 1] + j] == s[i + j]) ++j;
		}
	}

	void getrmq()
	{
		h[n + 1] = h[1] = lg[1] = 0;
		for (int i = 2; i <= n; i++)
			rmq[i][0] = h[i], lg[i] = lg[i >> 1] + 1;
		for (int i = 1; (1 << i) <= n; i++)
		{
			for (int j = 2; j <= n; j++)
			{
				if (j + (1 << i)>n + 1) break;
				rmq[j][i] = min(rmq[j][i - 1], rmq[j + (1 << i - 1)][i - 1]);
			}
		}
	}

	int lcp(int x, int y)
	{
		int l = min(rk[now][x], rk[now][y]) + 1, r = max(rk[now][x], rk[now][y]);
		return min(rmq[l][lg[r - l + 1]], rmq[r - (1 << lg[r - l + 1]) + 1][lg[r - l + 1]]);
	}

	bool check(int k)
	{
		int mi = sa[1], ma = sa[1];
		for (int i = 2; i <= n; i++)
		{
			if (h[i] >= k) ma = max(ma, sa[i]), mi = min(mi, sa[i]);
			else
			{
				if (ma - mi >= k) return 1;
				mi = ma = sa[i];
			}
		}
		if (ma - mi >= k) return 1;
		return 0;
	}

	void work()
	{
		getsa(200);
		int l = 4, r = n / 2, ans = 0;
		while (l <= r)
		{
			int mid = (l + r) >> 1;
			if (check(mid)) l = mid + 1, ans = mid + 1;
			else r = mid - 1;
		}
		printf("%d\n", ans);
	}
}sa;

int main()
{
	int n;
	while (~scanf("%d", &n) && n)
	{
		sa.GetS(n);
		sa.work();
	}
	return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值