poj 1743 Musical Theme(后缀数组)

Musical Theme
Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 38494   Accepted: 12654

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

思路:

题目要我们求最长的不重叠子串 两个字符串相同则长度相同 且 数字之间的差相同 那么我们考虑做差构建一个新数组 那么在这个数组上求lcp就转化为求相同子串 然后由于需要不重叠 我们可以维护一个最左最右且公共长度大于二分的答案的边界 判断是否大于二分的答案 如果大于就返回true

#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
const double pi = acos(-1.0);
const int N = 1e5+1000;
const int inf = 0x3f3f3f3f;
const double eps = 1e-6;
typedef long long ll;
const ll mod = 1e9+7;
struct S_array{
    int s[N],sa[N],t[N],t2[N],c[N],n;
    //int f[N][20];
    void build_sa(int m){ //字符个数 
        int i,*x=t,*y=t2;
        for(i=0;i<m;i++)c[i]=0;
        for(i=0;i<n;i++)c[x[i]=s[i]]++;
        for(i=1;i<m;i++)c[i]+=c[i-1];
        for(i=n-1;i>=0;i--)sa[--c[x[i]]]=i;
        for(int k=1;k<=n;k<<=1){
            int p=0;
            for(i=n-k;i<n;i++)y[p++]=i;
            for(i=0;i<n;i++)if(sa[i]>=k)y[p++]=sa[i]-k;
            for(i=0;i<m;i++)c[i]=0;
            for(i=0;i<n;i++)c[x[y[i]]]++;
            for(i=0;i<m;i++)c[i]+=c[i-1];
            for(i=n-1;i>=0;i--)sa[--c[x[y[i]]]]=y[i];
            swap(x,y);
            p=1;x[sa[0]]=0;
            for(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 rank[N],height[N];
    void getHeight(){
        int i,j,k=0;
        for(i=0;i<n;i++)rank[sa[i]]=i;
        for(i=0;i<n;i++){
            if(k)k--;
            int j=sa[rank[i]-1];
            while(s[i+k]==s[j+k])k++;
            height[rank[i]]=k;
        }
    }
    bool check(int n,int len){
        int mxl,mxr;
        mxl=inf; mxr=-inf;
        for(int i=1;i<n;i++){
            //cout<<mxl<<" "<<mxr<<endl;
            //cout<<height[i]<<" "<<len<<endl;
            if(height[i]>=len){
                mxl=min(mxl,min(sa[i-1],sa[i]));
                mxr=max(mxr,max(sa[i-1],sa[i]));
                if(mxr-mxl>len) return true;
            }else{
                mxl=inf,mxr=-inf;
            }
        }
        return false;
    }
}sa;
int s[N];
int main(){
//    ios::sync_with_stdio(false);
//    cin.tie(0); cout.tie(0);
    int n;
    while(~scanf("%d",&n)&&n){
        for(int i=0;i<n;i++){
            scanf("%d",s+i);
        }
        for(int i=1;i<n;i++)
            sa.s[i-1]=s[i]-s[i-1]+88;
        if(n!=1){
            sa.s[n-1]=0;
            sa.n=n;
        }
        sa.build_sa(176); sa.getHeight();
        int l,r,ans=-1; l=0; r=n;
        while(l<=r){
            int mid=(l+r)>>1;
            if(sa.check(n,mid)){
                ans=mid;
                l=mid+1;
            }else{
                r=mid-1;
            }
        }
        if(ans>=4) printf("%d\n",ans+1);
        else printf("0\n");
    }
}
View Code

 

猜你喜欢

转载自www.cnblogs.com/wmj6/p/11426250.html