POJ-1743-Musical Theme (后缀数组)

Musical Theme
Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 32721   Accepted: 10894

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

LouTiancheng@POJ


题意:找到最长不重叠的字串,两个字串不必完全相同,满足一个字串的每个数加上或减去同一个数和另一个字串就行。
思路:后缀数组的基本应用,但是需要处理一下,我们不对原数组操作,而是对每两个数的之间的差值跑后缀数组,这样就不用考虑加上或减去一个数,因为满足题意的两个子串吗每两个数之间的差值一定相等。处理出heigth和sa数组后二分结果,注意二分时的左端点从4开始,因为是跑的差值的后缀数组。
#include <cstdio>  
#include <cstring>  
#include <algorithm>  
#include<iostream>  
using namespace std;

const int MAXN = 20005;

int SA[MAXN], rank[MAXN], Height[MAXN], tax[MAXN], tp[MAXN], a[MAXN], len, m;
int str[MAXN];
///rank[i] 第i个后缀的排名; SA[i] 排名为i的后缀位置; Height[i] 排名为i的后缀与排名为(i-1)的后缀的LCP  
///tax[i] 计数排序辅助数组; tp[i] rank的辅助数组(计数排序中的第二关键字),与SA意义一样。  
///a为原串  
void RSort() {

    for (int i = 0; i <= m; i++) tax[i] = 0;
    for (int i = 1; i <= len; i++) tax[rank[tp[i]]] ++;
    for (int i = 1; i <= m; i++) tax[i] += tax[i - 1];
    for (int i = len; i >= 1; i--) SA[tax[rank[tp[i]]] --] = tp[i];
}

int cmp(int *f, int x, int y, int w) { return (f[x] == f[y]) && (f[x + w] == f[y + w]); }

void Suffix() {
    for (int i = 1; i <= len; i++) rank[i] = a[i], tp[i] = i;
    m = 200, RSort();
    for (int w = 1, p = 1, i; p < len; w += w, m = p) {
        for (p = 0, i = len - w + 1; i <= len; i++) tp[++p] = i;
        for (i = 1; i <= len; i++) if (SA[i] > w) tp[++p] = SA[i] - w;
        RSort(), swap(rank, tp), rank[SA[1]] = p = 1;

        for (i = 2; i <= len; i++) rank[SA[i]] = cmp(tp, SA[i], SA[i - 1], w) ? p : ++p;

    }
    for (int i = 1; i <= len; i++) rank[SA[i]] = i;
    int j, k = 0;
    for (int i = 1; i <= len; Height[rank[i++]] = k)
        for (k = k ? k - 1 : k, j = SA[rank[i] - 1]; a[i + k] == a[j + k]; ++k);
}

bool check(int k)
{
    int mi = SA[1], mx = SA[1];
    for (int i = 2; i <= len; i++) {
        if (Height[i]<k) mi = mx = SA[i];
        else {
            mi = min(mi, SA[i]);
            mx = max(mx, SA[i]);
            if (mx - mi >= k) return true;
        }
    }
    return false;
}
int sol()
{
    int l = 4, r = len / 2;
    int ans = 0;
    while (l <= r) {
        int mid = (l + r) >> 1;
        if (check(mid)) l = mid + 1, ans = mid;
        else r = mid - 1;
    }
    return ans >= 4 ? ans + 1 : 0;
}
void Init() {
    for (int i = 1; i < len; i++) a[i] = str[i + 1] - str[i] + 90;///用a数组存相邻两个数的差值,为了保证是正数,加上90  
    a[len] = 0;
}
int main()
{
    int n;
    while (scanf("%d", &n) && n) {
        for (int i = 1; i <= n; i++)
            scanf("%d", &str[i]);
        len = n;
        Init();
        Suffix();
        int ans = sol();
        printf("%d\n", ans);
    }
    return 0;
}



猜你喜欢

转载自blog.csdn.net/qq_37171272/article/details/79859066