您的位置:首页 > 其它

poj 1743 Musical Theme 【后缀数组 最长不重叠重复子串】

2016-04-04 16:27 519 查看
题目链接:poj 1743 Musical Theme

Musical Theme

Time Limit: 1000MS Memory Limit: 30000K

Total Submissions: 24442 Accepted: 8233

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.

以前看过后缀数组,太难就草草了事。现在下狠心好好学。

题意:给定n个音符,我们把相邻音符间的差值当做旋律的序列。让你找到最长的旋律序列1、长度大于4;2、重复出现至少2次;3、不能重叠。

思路:求出R、H、sa后。二分答案k,根据k对H分组,在满足H>=k的条件下同一组里面的任意两个后缀均满足LCP >= k,为了保证不重叠,我们需要判定同一组里面距离最远的两个后缀之间的距离是否 >= k。

AC代码:

//#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <map>
#include <stack>
#define PI acos(-1.0)
#define CLR(a, b) memset(a, (b), sizeof(a))
#define fi first
#define se second
#define ll o<<1
#define rr o<<1|1
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
const int MAXN = 2*1e4 + 10;
const int pN = 1e6;// <= 10^7
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
void add(LL &x, LL y) { x += y; x %= MOD; }
int cmp(int *r, int a, int b, int l) {
return (r[a] == r[b]) && (r[a+l] == r[b+l]);
}
int wa[MAXN], wb[MAXN], ws[MAXN], wv[MAXN];
int R[MAXN];//下标0->n-1 存储的是1->n之间的数
int H[MAXN];//下标2->n   H[i] >= H[i-1]-1
void DA(int *r, int *sa, int n, int m)
{
int i, j, p, *x = wa, *y = wb, *t;
for(i = 0; i < m; i++) ws[i] = 0;
for(i = 0; i < n; i++) ws[x[i]=r[i]]++;
for(i = 1; i < m; i++) ws[i] += ws[i-1];
for(i = n-1; i >= 0; i--) sa[--ws[x[i]]] = i;
for(j = 1, p = 1; p < n; j *= 2, m = p)
{
for(p = 0, i = n-j; i < n; i++) y[p++] = i;
for(i = 0; i < n; i++) if(sa[i] >= j) y[p++] = sa[i] - j;
for(i = 0; i < n; i++) wv[i] = x[y[i]];
for(i = 0; i < m; i++) ws[i] = 0;
for(i = 0; i < n; i++) ws[wv[i]]++;
for(i = 1; i < m; i++) ws[i] += ws[i-1];
for(i = n-1; i >= 0; i--) sa[--ws[wv[i]]] = y[i];
for(t = x, x = y, y = t, p = 1, x[sa[0]] = 0, i = 1; i < n; i++)
x[sa[i]] = cmp(y, sa[i-1], sa[i], j) ? p-1 : p++;
}
}
void calh(int *r, int *sa, int n)
{
int i, j, k = 0;
for(i = 1; i <= n; i++) R[sa[i]] = i;
for(i = 0; i < n; H[R[i++]] = k)
for(k ? k-- : 0, j = sa[R[i]-1]; r[i+k] == r[j+k]; k++);
}
int sa[MAXN];//下标从1->n,存储的是0->n-1之间的数
bool judge(int k, int n) {
int mx = sa[1], mi = sa[1];
int ans = 0;
for(int i = 2; i <= n; i++) {
if(H[i] < k) {
mx = mi = sa[i];
}
else {
mx = max(mx, sa[i]);
mi = min(mi, sa[i]);
ans = max(ans, mx - mi);
if(ans >= k) return true;
}
}
return false;
}
int a[MAXN], b[MAXN];
int main()
{
int n;
while(scanf("%d", &n), n) {
for(int i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
for(int i = 0; i < n-1; i++) {
b[i] = a[i+1] - a[i] + 90;
}
n--; b
= 0;
DA(b, sa, n+1, 200);//<n+1
calh(b, sa, n);//<=n
int ans = 0; int l = 0, r = n;
while(r >= l) {
int mid = (l + r) >> 1;
if(judge(mid, n)) {
ans = mid;
l = mid + 1;
}
else {
r = mid - 1;
}
}
printf(ans < 4 ? "0\n" : "%d\n", ans + 1);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: