Musical Theme

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
   

题意:给一些数字串,当做韵律,相同的韵律必须满足:

    1,最少5个数字长;

    2,重复出现的时候,必须相对差值一样,比如{ 1 ,2, 3, 22, 10, 11, 12  },1,2,3与10 11 12是一样的韵律。

    3,韵律不可重叠。

其实就是求最长不重叠重复子串的长度(好绕~~)
求出height数组的值,然后二分……
将height 数组分成若干组,每组内height值都不小于x, 然后判断有没有组最长和最短的后缀相差不小于x
(貌似利用height数组分组的方式非常非常地常用)
NOI09年的论文里讲得很详细,就这样吧……
  
   
#include <iostream>
#include <cstdio>
#include <string>
#define N 20100
long n, m ;
long a[N], sa[N], rank[N], height[N], wa[N], wb[N], wv[N], ws[N];
bool cmp(long *r, long a, long b, long l)
{
    return ((r[a] == r[b]) && (r[a + l] == r[b + l]));
}

void calcsa(long *r, long *sa, long n, long m)
{
    long 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];
        t = x, x = y, y = t;
        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++;
        }
    }
}

void calcheight(long *r, long *sa, long n)
{

    long i, j, p = 0;
    for(i = 1; i <= n; i++)
        rank[sa[i]] = i;
    for(i = 0; i < n; i++)
    {
        j = sa[rank[i] - 1];
        while (r[i + p] == r[j + p])
        {
           // printf("%d %d\n", i, sa[i]);
            p++;
        }
        height[rank[i]] = p;
        if (p > 0) p--;
    }
    return;
}

bool check(long x)
{
    long minn = sa[1], maxn = sa[1];
    for (long i = 2; i <= n; i++)
    {
        if (height[i] >= x)
        {
            if (sa[i] < minn) minn = sa[i];
            if (sa[i] > maxn) maxn = sa[i];
            if ((maxn - minn) > x) return 1;
        }
        else minn = maxn = sa[i];
    }
    return 0;
}
void deal()
{
    long l = 0, r = n;
    while (l <= r)
    {
        long mid = (l + r) / 2;
        if (check(mid)) l = mid + 1;
        else r = mid - 1;
    }
    if(r >= 4) printf("%d\n", r+1);
      else printf("0\n");
}
int main()
{
   // freopen("poj1743.in", "r", stdin);
    while ((scanf("%d", &n) != EOF) && (n != 0))
    {
        long j, k;
        scanf("%d",&j);
        n--;

        for (long i = 0; i < n; i++)
        {

            scanf("%d", &k);
            a[i] = k - j + 100;
            j = k;
        }

        a[n] = 0 ;
        calcsa(a, sa, n + 1, 200);
        calcheight(a, sa, n);
        deal();
    }

    return 0;
}