poj1743Musical Theme(不可重叠最长重复子串)

Musical Theme
Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 22324 Accepted: 7600

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.

首先由于数字可变化,但是他们之间的差是不变的,所以可将原来的数组转化为差,
其次,要不重复,那么便是要求连续名次相近的那一类中的最大值和最小值的差大于等于k;
此时可以二分查找答案,
又因为两个名次之间的最长公共前缀是这两个名次之间的height最小值,
所以可以按名次将height分类
容易看出,有希望成为最长公共前缀不小于 k 的两个后缀一定在同一组。然
后对于每组后缀,只须判断每个后缀的 sa 值的最大值和最小值之差是否不小于
k。如果有一组满足,则说明存在,否则不存在。
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <cmath>
#include <ctime>
#include <vector>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
#define INF 0x3f3f3f3f
#define inf -0x3f3f3f3f
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define mem0(a) memset(a,0,sizeof(a))
#define mem1(a) memset(a,-1,sizeof(a))
#define mem(a, b) memset(a, b, sizeof(a))
typedef long long ll;
const int maxn=20000+100;
int s[maxn];
int sa[maxn],t[maxn],t2[maxn],c[maxn];
int n;

void build_sa(int m){
    int i,*x=t,*y=t2;
    //基数排序
    for(int i=0;i<m;i++)    c[i]=0;
    for(int i=0;i<n;i++)    c[x[i]=s[i]]++;
    for(int i=1;i<m;i++)    c[i]+=c[i-1];
    for(int i=n-1;i>=0;i--)    sa[--c[x[i]]]=i;
    for(int k=1;k<=n;k<<=1){
        int p=0;
        for(int i=n-k;i<n;i++)  y[p++]=i;
        for(int i=0;i<n;i++)    if(sa[i]>=k) y[p++]=sa[i]-k;
        for(int i=0;i<m;i++)    c[i]=0;
        for(int i=0;i<n;i++)    c[x[y[i]]]++;
        for(int i=0;i<m;i++)    c[i]+=c[i-1];
        for(int i=n-1;i>=0;i--) sa[--c[x[y[i]]]]=y[i];
        swap(x,y);
        p=1;x[sa[0]]=0;
        for(int i=1;i<n;i++)
            x[sa[i]]=y[sa[i-1]]==y[sa[i]]&&y[sa[i-1]+k]==y[sa[i]+k]?p-1:p++;
        if(p>=n)    break;
        m=p;
    }
}
int rank1[maxn],height[maxn];

void getHeight(){
    int i,j,k=0;
    for(int i=0;i<=n;i++)   rank1[sa[i]]=i;
    for(int i=0;i<n;i++){
        if(k)   k--;
        j=sa[rank1[i]-1];
        while(s[i+k]==s[j+k])
            k++;
        height[rank1[i]]=k;
    }
}

bool check(int k){
    int minv,maxv;
    minv=INF;
    maxv=-INF;
    for(int i=2;i<=n;i++){  //rank1为0的已经被抛弃
        if(height[i]>=k){
            minv=min(minv,sa[i-1]);
            minv=min(minv,sa[i]);
            maxv=max(maxv,sa[i-1]);
             maxv=max(maxv,sa[i]);
            if(maxv-minv>=k)
                return true;
            continue;
        }
        else{
        	minv=INF;
    		maxv=-INF;
        }
    }
    return false;
}

int main(){
    while(scanf("%d",&n)!=EOF){
        if(n==0)
            break;
        for(int i=0;i<n;i++){
            scanf("%d",&s[i]);
            if(i!=0)
                s[i-1]=s[i]-s[i-1]+100;
        }
        s[n-1]=0;
        build_sa(200);
        n--;
        getHeight();
        int low=3,high=n;
        while(high-low>=0){
            int mid=(low+high)/2;
            if(check(mid))
                low=mid+1;
            else
                high=mid-1;
        }
        if(low<=3)
            printf("0\n");
        else
            printf("%d\n",low);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值