首页 > 代码库 > poj 1743 Musical Theme(男人八题&后缀数组第一题)

poj 1743 Musical Theme(男人八题&后缀数组第一题)

Musical Theme
Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 17298 Accepted: 5939

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)的数字串。如果一个串在母串出现的次数大于一次那么这个串就是母串的重复子串。子串的每个位置同时加上一个数字重复出现在另一个位置也算重复。先在问这个母串最长的不相交即没有重复元素的重复子串的最大长度。

思路:

对于可相交的最长重复子串。可以狠轻松的想到后缀数组。对于题目所给的加上一个数字重复的情况。可以令s[i]=s[i+1]-s[i]。这样就可以直接转换成求字符串最长公共前缀的问题了。现在要解决的就是不相交的问题了。

我们可以二分最大长度k。然后再用后缀数组判定。这样我们就可以将height分组。,其中每组的后缀之间的height 值都不小于k。容易看出,有希望成为最长公共前缀不小于k 的两个后缀一定在同一组。然后对于每组后缀,只须判断每个后缀的sa 值的最大值和最小值之差是否不小于k。如果有一组满足,则说明存在,否则不存在。

详细见代码:

#include<algorithm>
#include<iostream>
#include<string.h>
#include<sstream>
#include<stdio.h>
#include<math.h>
#include<vector>
#include<string>
#include<queue>
#include<set>
#include<map>
#include<bitset>
using namespace std;
const int INF=0x3f3f3f3f;
const double eps=1e-8;
const double PI=acos(-1.0);
const int maxn=100010;
//typedef __int64 ll;
int txt[maxn];
int sa[maxn],T1[maxn],T2[maxn],ct[maxn],he[maxn],rk[maxn],ans,n,m;//sa[i]表示排名第i的后缀的起始位置。
void getsa(int *st)//注意m为ASCII码的范围
{
    int i,k,p,*x=T1,*y=T2;
    for(i=0; i<m; i++) ct[i]=0;
    for(i=0; i<n; i++) ct[x[i]=st[i]]++;
    for(i=1; i<m; i++) ct[i]+=ct[i-1];
    for(i=n-1; i>=0; i--)//倒着枚举保证相对顺序
        sa[--ct[x[i]]]=i;
    for(k=1,p=1; p<n; k<<=1,m=p)//枚举长度
    {
        for(p=0,i=n-k; i<n; i++) y[p++]=i;
        for(i=0; i<n; i++) if(sa[i]>=k) y[p++]=sa[i]-k;//按第二关键字排序.y[i]表示第二关键字排名第i的后缀起始位置
        for(i=0; i<m; i++) ct[i]=0;
        for(i=0; i<n; i++) ct[x[y[i]]]++;//x[i]表示起始位置为i的后缀的第一关键字排序
        for(i=1; i<m; i++) ct[i]+=ct[i-1];
        for(i=n-1; i>=0; i--) sa[--ct[x[y[i]]]]=y[i];//接着按第一关键字排序
        for(swap(x,y),p=1,x[sa[0]]=0,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++;//x[i]存排名第i后缀的排名
    }
}
void gethe(int *st)//求height数组
{
    int i,j,k=0;
    for(i=0;i<n;i++) rk[sa[i]]=i;
    for(i=0;i<n;i++)
    {
        if(k) k--;
        if(!rk[i]) continue;//如果i=0,j=0就惨了。。。
        j=sa[rk[i]-1];
        while(st[i+k]==st[j+k]) k++;
        he[rk[i]]=k;
    }
}
bool ok(int x)
{
    int i,sp,bp;//区间下界和上界
    sp=bp=sa[0];
    for(i=1;i<n;i++)
    {
       if(he[i]<x)
        sp=bp=sa[i];
       else
        sp=min(sa[i],sp),bp=max(sa[i],bp);
       if(bp-sp>x)
        return true;
    }
    return false;
}
void solve()
{
    int low=4,hi=n,mid;
    getsa(txt),gethe(txt),ans=0;
    while(low<=hi)//开始把hi写成mid调了一下午。。。真是逗比!
    {
        mid=(low+hi)>>1;
        if(ok(mid))
            ans=mid,low=mid+1;
        else
            hi=mid-1;
    }
}
int main()
{
    int i;

    while(scanf("%d",&n),n)
    {
        for(i=0;i<n;i++)
            scanf("%d",&txt[i]);
        for(i=m=0;i<n-1;i++)
            txt[i]=txt[i+1]-txt[i]+90,m=max(m,txt[i]);
        m++,txt[i]=0;
        if(n>=10)
            solve();
        else
            ans=0;
        ans++;
        if(ans<5)
            ans=0;
        printf("%d\n",ans);
    }
    return 0;
}