Musical Theme(后缀数组 求不可重叠最长重复子串)

Musical Theme

Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 36270 Accepted: 11994

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

题意:有N(1 <= N <=20000)个音符的序列来表示一首乐曲,每个音符都是1..88范围内的整数,现在要找一个重复的主题。“主题”是整个音符序列的一个子串,它需要满足如下条件:

    1.长度至少为5个音符。

    2.在乐曲中重复出现。(可能经过转调,“转调”的意思是主题序列中每个音符都被加上或减去了同一个整数值)

    3.重复出现的同一主题不能有公共部分。

sa[i]:表示排名第i个的首字母位置
Rank[i]:第i个数的排名。。后缀i在sa数组里的下标(以i位置开始的后缀称为后缀i)
Height[i]:sa[i]和sa[i-1]的最长公共前缀

suffix(j) 和suffix(k) 的最长公共前缀为height[rank[j]+1],
height[rank[j]+2], height[rank[j]+3], … ,height[rank[k]]中的最小值。

由于是求区间最小值,所以还可以往上面套一个RMQ

思路:后缀数组。求出任意相邻音符的差值,最后一个填充0,然后把问题转化为 不可重叠最长重复子串,用后缀数组来做。先二分答案,把题目变成判定性问题:判断是否存在两个长度为k的子串是相同的,且不重叠。解决这个问题的关键还是利用 height数组。把排序后的后缀分成若干组,其中每组的后缀之间的height值都不小于k。

https://www.cnblogs.com/chen9510/p/5487060.html

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#define rep(i,n) for(int i = 0;i < n; i++)
using namespace std;
const int size=20205,INF=1<<30;
int rk[size],sa[size],height[size],w[size],wa[size],res[size];
int N;
void getSa (int len,int up) {
    int *k = rk,*id = height,*r = res, *cnt = wa;
    for(int i=0;i<up;i++) cnt[i] = 0;
    for(int i=0;i<len;i++) cnt[k[i] = w[i]]++;
    for(int i=0;i<up;i++) cnt[i+1] += cnt[i];
    for(int i = len - 1; i >= 0; i--) {
        sa[--cnt[k[i]]] = i;
    }
    int d = 1,p = 0;
    while(p < len){
        for(int i = len - d; i < len; i++) id[p++] = i;
        for(int i=0;i<len;i++)  if(sa[i] >= d) id[p++] = sa[i] - d;
        for(int i=0;i<len;i++) r[i] = k[id[i]];
        for(int i=0;i<up;i++) cnt[i] = 0;
        for(int i=0;i<len;i++) cnt[r[i]]++;
        for(int i=0;i<up;i++) cnt[i+1] += cnt[i];
        for(int i = len - 1; i >= 0; i--) {
            sa[--cnt[r[i]]] = id[i];
        }
        swap(k,r);
        p = 0;
        k[sa[0]] = p++;
        for(int i=0;i<len-1;i++) {
            if(sa[i]+d < len && sa[i+1]+d <len &&r[sa[i]] == r[sa[i+1]]&& r[sa[i]+d] == r[sa[i+1]+d])
                k[sa[i+1]] = p - 1;
            else k[sa[i+1]] = p++;
        }
        if(p >= len) return ;
        d *= 2,up = p, p = 0;
    }
}

void getHeight(int len) {
    for(int i=0;i<len;i++) rk[sa[i]] = i;
    height[0] =  0;
    for(int i = 0,p = 0; i < len - 1; i++) {
        int j = sa[rk[i]-1];
        while(i+p < len&& j+p < len&& w[i+p] == w[j+p]) {
            p++;
        }
        height[rk[i]] = p;
        p = max(0,p - 1);
    }
}

int getSuffix(int s[]) {//这个函数是干嘛的? 
    int len =N,up = 0;
    for(int i = 0; i < len; i++) {
        w[i] = s[i];
        up = max(up,w[i]);
    }
    //up为这个数组中最大的数 
    w[len++] = 0;
    getSa(len,up+1);
    getHeight(len);
    return len;
}

bool valid(int len)
{
    int i = 2, ma, mi;
    while(1)
    {
        while(i <= N && height[i] < len) i ++;
        if(i > N) break;
        ma = sa[i-1];
        mi = sa[i-1];
        while(i <= N && height[i] >= len)
        {
            ma = max(ma, sa[i]);
            mi = min(mi, sa[i]);
            i ++;
        }
        if(ma - mi >= len) return true;
    }
    return false;
}
int main()
{
    int s[size];
    while(scanf("%d",&N)!=EOF)
    {
        if(!N) return 0;
        for(int i=0;i<N;i++)
        {
            scanf("%d",&s[i]);
        }
        for(int i=0;i<N-1;i++)
        {
            s[i]=s[i+1]-s[i]+88;
        }
        s[N-1]=0;
        getSuffix(s);
        
        int l = 4, r = (N-1)/2, mid;
        int ans=4;
        while(l <=r)
        {
            mid = (l+ r + 1) / 2;
            if(valid(mid)) {
                l = mid+1;
                ans=mid;
            }else {
               r = mid - 1;
            }
        }
        
        ans = ans< 4 ? 0 : ans+ 1;
        if(N<10) ans=0;
        printf("%d\n", ans);
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值