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

Musical Theme
Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 21913   Accepted: 7494

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-88之间。现在让你求出串中最长的不重复的子串(出现次数大于1),满足要求的子串的定义是长度不短于5,其两个子串可以通过分别加上某一值或减去某一值得到且没有公共部分。


思路:求出相邻两个数字的差值,并以差值构建新串。求出新串的【不可重叠最长重复子串】。


后缀数组倍增算法实现过程:sa——后缀数组,rank——名次数组,height——排名相邻的两个后缀的最长公共前缀

1,先用倍增算法求出sa数组和height数组的值;

2,二分枚举可能的长度k;

3,按height值对后缀进行分组,保证每一组中相邻后缀的height值不小于k;(可以在判断是否可行的过程中实现)

4,判断长度k是否可行。用两个变量Max和Min代表同一组后缀间的最大sa值和最小sa值。 从前向后遍历,若碰到不同组的后缀,更新Max和Min。对同一组的后缀,维护后缀的最大sa值和最小sa值,若有一个满足Max-Min >= k,则长度k可行,否则不可行。



AC代码:倍增法的实现大致理解了, 很多细节还是理解的不够透彻o(╯□╰)o


#include <cstdio>
#include <cstring>
#include <algorithm>
#define MAXN 40000+10
using namespace std;
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 rank[MAXN], height[MAXN];
int a[MAXN], b[MAXN];
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 calheight(int *r, int *sa, int n)//求height值
{
    int i, j, k = 0;
    for(i = 1; i <= n; i++) rank[sa[i]] = i;//根据sa 求 rank
    for(i = 0; i < n; height[rank[i++]] = k)
    for(k ? k-- : 0, j = sa[rank[i]-1]; r[i+k] == r[j+k]; k++);
}
int sa[MAXN];//下标从1开始
bool judge(int k, int n)//判断当前长度k是否可行
{
    int Max = sa[1], Min = sa[1];//最值
    for(int i = 2; i <= n; i++)
    {
        if(height[i] < k)//不在一组
            Max = Min = sa[i];//更新
        else//在一组 维护最大值 最小值
        {
            Max = max(Max, sa[i]);//维护
            Min = min(Min, sa[i]);
            if(Max - Min >= k)//差值大于或者等于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(i)
            b[i-1] = a[i] - a[i-1] + 90;
        }
        N--;
        b[N] = 0;
        DA(b, sa, N+1, 200);//注意长度
        calheight(b, sa, N);
        int left = 0, right = N;
        int ans = 0;
        while(right >= left)
        {
            int mid = (left + right) >> 1;
            if(judge(mid, N))
            {
                ans = max(ans, mid);
                left = mid + 1;
            }
            else
                right = mid - 1;
        }
        if(ans < 4)
            printf("0\n");
        else
            printf("%d\n", ans+1);
    }
    return 0;
}


你可能感兴趣的:(poj 1743 Musical Theme 【后缀数组】【求不可重叠最长重复子串】)