POJ 1743 Musical Theme (后缀树组)

来源:互联网 发布:安广网络客服电话 编辑:程序博客网 时间:2024/06/03 04:43

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

3025 27 30 34 39 45 52 60 69 79 69 60 52 45 39 34 30 26 22 1882 78 74 70 66 67 64 60 65 800

Sample Output

5
题目大意:将数组前后相减,求最长的重复子串,这两个子串不能重叠。
思路:这是论文上的一道题。首先二分答案,判断是否有一个长度为k的重复且不重叠的子串。然后将排好序的后缀分成若干组,每个组的height值都不小于k,如果该组sa值的最大值减最小值大于等于k,则说明有一个长度为k的不重叠子串。
#include<cstdio>#include<cstring>#include<vector>#include<set>#include<queue>#include<map>#include<iostream>#include<stack>#include<cctype>#include<algorithm>using namespace std;const int maxn = 20000 + 10;int a[maxn];int t1[maxn], t2[maxn], c[maxn];int Rank[maxn], height[maxn];int r[maxn], sa[maxn];bool cmp(int *r, int a, int b, int l){    return r[a] == r[b] && r[a+l] == r[b+l];}void da(int str[], int sa[], int Rank[], int height[], int n, int m){    n++;    int i, j, p, *x=t1, *y=t2;    for(i = 0; i < m; i++) c[i] = 0;    for(i = 0; i < n; i++) c[ x[i] = str[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(j = 1; j <= n; j <<= 1)    {        p = 0;        for(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(int i = 0; i < m; i++) c[i] = 0;        for(int i = 0; i < n; i++) c[x[y[i]]]++;        for(int i = 1; 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]] = cmp(y, sa[i-1], sa[i], j) ? p-1 : p++;        if(p >= n) break;        m = p;    }    int k = 0;    n--;    for(i = 0; i <= n; i++) Rank[sa[i]] = i;    for(i = 0; i < n; i++)    {        if(k) k--;        j = sa[Rank[i]-1];        while(str[i+k] == str[j+k]) k++;        height[Rank[i]] = k;    }}int RMQ[maxn], mm[maxn], best[20][maxn];void initRMQ(int n){    mm[0] = -1;    for(int i = 1; i <= n; i++)        mm[i] = ((i&(i-1))==0) ? mm[i-1]+1 : mm[i-1];    for(int i = 1; i <= n; i++) best[0][i] = 1;    for(int i = 1; i <= mm[n]; i++)        for(int j = 1; j+(1<<i)-1 <= n; j++)        {            int a = best[i-1][j];            int b = best[i-1][j + (1<<(i-1))];            if(RMQ[a] < RMQ[b]) best[i][j] = a;            else best[i][j] = b;        }}int askRMQ(int a, int b){    int t;    t = mm[b-a+1];    b -= (1<<t)-1;    a = best[t][a]; b = best[t][b];    return RMQ[a] < RMQ[b] ? a : b;}int lcp(int a, int b){    a = Rank[a], b = Rank[b];    if(a > b) swap(a, b);    return height[askRMQ(a+1, b)];}bool judge(int k, int n){int mi, mx;mi = mx = sa[1];for(int i = 2; i <= n; i++) {if(height[i] < k) mx = mi = sa[i];else {mx = max(mx, sa[i]);mi = min(mi, sa[i]);if(mx - mi >= k) return true;}}return false;}int main(){    int n;    while(scanf("%d", &n) && n)    {    for(int i = 0; i < n; i++) scanf("%d", a+i);    if(n < 5) { printf("0\n"); continue; }        for(int i = 0; i < n-1; i++) a[i] = a[i+1] - a[i] + 100;        a[n-1] = 0; n--;        da(a, sa, Rank, height, n, 190);        int L = 0, R = n, ans = 0;        while(L <= R)        {        int mid = (L + R) >> 1;        if(judge(mid, n)) { ans = mid; L = mid+1; }        else R = mid - 1;        }        ++ans;        if(ans < 5) printf("0\n");        else printf("%d\n", ans);    }    return 0;}