您的位置:首页 > 其它

poj 1743 Musical Theme(不可重叠最长重复子串)

2014-11-18 18:31 393 查看
Musical Theme

Time Limit: 1000MSMemory Limit: 30000K
Total Submissions: 19435Accepted: 6650
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.

题意:给定一个字符串,求最长重复子串,这两个子串不能重叠。

思路:先二分答案,把题目变成判定性问题:判断是否存在两个长度为k 的子串是相同的,且不重叠。解决这个问题的关键还是利用

height 数组。把排序后的后缀分成若干组,其中每组的后缀之间的height 值都不小于k。

容易看出,有希望成为最长公共前缀不小于k 的两个后缀一定在同一组。然后对于每组后缀,只须判断每个后缀的sa 值的最大值和最小值之差是否不小于k。如果有一组满足,则说明存在,否则不存在。整个做法的时间复杂度为O(nlogn)。

AC代码:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <cmath>
#include <algorithm>
#include <queue>
#include <map>
#define ll long long
using namespace std;

const int maxn = 200005;
const int INF = 1e9;

int s[maxn];
int sa[maxn], t[maxn], t2[maxn], c[maxn], n;
int rank[maxn], height[maxn];
int f[maxn];
void build_sa(int n, int m){
int i, *x = t, *y = t2;
for(i = 0; i < m; i++) c[i] = 0;
for(i = 0; i < n; i++) c[x[i] = s[i]]++;
for(i = 1; i < m; i++) c[i] += c[i - 1];
for(i = n - 1; i >= 0; i--) sa[--c[x[i]]] = i;
for(int k = 1; k <= n; k <<= 1)
{
int p = 0;
for(i = n - k; i < n; i++) y[p++] = i;
for(i = 0; i < n; i++) if(sa[i] >= k) y[p++] = sa[i] - k;
for(i = 0; i < m; i++) c[i] = 0;
for(i = 0; i < n; i++) c[x[y[i]]]++;
for(i = 0; i < m; i++) c[i] += c[i - 1];
for(i = n - 1; i >= 0; i--) sa[--c[x[y[i]]]] = y[i];
swap(x, y);
p = 1; x[sa[0]] = 0;
for(i = 1; i < n; i++)
x[sa[i]] = y[sa[i - 1]] == y[sa[i]] && y[sa[i - 1] + k] == y[sa[i] + k] ? p - 1 : p++;
if(p >= n) break;
m = p;
}
}
void getHeight(){
int i, j, k = 0;
for(i = 1; i <= n; i++) rank[sa[i]] = i;
for(i = 0; i < n; i++)
{
if(k) k--;
j = sa[rank[i] - 1];
while(s[i + k] == s[j + k]) k++;
height[rank[i]] = k;
}
}
bool ok(int d){
int Min = sa[0], Max = sa[0];
for(int i = 0; i < n; i++)
{
if(height[i] < d)
{
Max = Min = sa[i];
continue;
}
if(sa[i] < Min) Min = sa[i];
if(sa[i] > Max) Max = sa[i];
if(Max - Min >= d) return true;
}
return false;
}
int solve(){
int low = 0, high = n, ans = 0;
while(low <= high)
{
int mid = (low + high) >> 1;
if(ok(mid))
{
low = mid + 1;
ans = mid;
}
else high = mid - 1;
}
return ans;
}
int main()
{
while(scanf("%d", &n), n)
{
for(int i = 0; i < n; i++)
scanf("%d", &f[i]);
for(int i = 0; i < n - 1; i++)
s[i] = f[i + 1] - f[i] + 90;
n--;
s
= 0;
build_sa(n + 1, 256);
getHeight();
int ans = solve();
if(ans >= 4) printf("%d\n", ans + 1);
else printf("0\n");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: