zoukankan      html  css  js  c++  java
  • poj 1743 后缀数组 最长不重叠子串

    Musical Theme
    Time Limit: 1000MS   Memory Limit: 30000K
    Total Submissions: 30941   Accepted: 10336

    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

    题意:

    给出长度是n的数组表示音符,求最长的相同音乐段长度,相同音乐段是指两段的变化是一样的(即差值数组都一样)。

    代码:

    //求最长不重叠子串,把给出的数组转换成差值的形式。论文题就不多说了。
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    using namespace std;
    const int MAXN=20000;
    const int INF=0x7fffffff;
    int he[MAXN+9],ra[MAXN+9],sa[MAXN+9],xx[MAXN+9],yy[MAXN+9],buc[MAXN+9];
    int s[MAXN+9],a[MAXN+9];
    int len,m;
    void get_suf()
    {
        int *x=xx,*y=yy;
        for(int i=0;i<m;i++) buc[i]=0;
        for(int i=0;i<len;i++) buc[x[i]=s[i]]++;
        for(int i=1;i<m;i++) buc[i]+=buc[i-1];
        for(int i=len-1;i>=0;i--) sa[--buc[x[i]]]=i;
        for(int k=1;k<=len;k<<=1){
            int p=0;
            for(int i=len-1;i>=len-k;i--) y[p++]=i;
            for(int i=0;i<len;i++) if(sa[i]>=k) y[p++]=sa[i]-k;
            for(int i=0;i<m;i++) buc[i]=0;
            for(int i=0;i<len;i++) buc[x[y[i]]]++;
            for(int i=1;i<m;i++) buc[i]+=buc[i-1];
            for(int i=len-1;i>=0;i--) sa[--buc[x[y[i]]]]=y[i];
            swap(x,y);
            p=1;x[sa[0]]=0;
            for(int i=1;i<len;i++){
                if(y[sa[i-1]]==y[sa[i]]&&y[sa[i-1]+k]==y[sa[i]+k])
                    x[sa[i]]=p-1;
                else x[sa[i]]=p++;
            }
            if(p>=len) break;
            m=p;
        }
        for(int i=0;i<len;i++) ra[sa[i]]=i;
        int k=0;
        for(int i=0;i<len;i++){
            if(ra[i]==0) { he[0]=0; continue; }
            if(k) k--;
            int j=sa[ra[i]-1];
            while(s[i+k]==s[j+k]&&i+k<len&&j+k<len) k++;
            he[ra[i]]=k;
        }
    }
    bool solve(int mid)
    {
        int min_sa=INF,max_sa=-INF;
        for(int i=1;i<len;i++){
            if(he[i]>=mid){
                min_sa=min(min_sa,min(sa[i],sa[i-1]));
                max_sa=max(max_sa,max(sa[i],sa[i-1]));
                if(min_sa+mid-1<max_sa) return 1;
            }else{
                min_sa=INF;max_sa=-INF;
            }
        }
        return 0;
    }
    int main()
    {
        int n;
        while(scanf("%d",&n)&&n){
            for(int i=0;i<n;i++) scanf("%d",&a[i]);
            for(int i=1;i<n;i++) s[i-1]=a[i]-a[i-1]+89;
            len=n-1;
            m=200;
            get_suf();
            int l=0,r=len,ans=0;
            while(l<=r){
                int mid=(l+r)>>1;
                if(solve(mid)) { ans=mid;l=mid+1; }
                else r=mid-1;
            }
            if(++ans<5) ans=0;
            printf("%d
    ",ans);
        }
        return 0;
    }
  • 相关阅读:
    AccessControlAllowOrigin 跨域设置多域名
    C#基于LibUsbDotNet实现USB通信(一)
    移动端设置行高等于高度的时候文本不居中???
    阿里云OSS设置跨域访问还是不行。
    没错,就是AccessControlAllowOrigin,跨域
    移动端 lineheight 文字不居中问题解决方案
    Chromium.org team 改进vs编译速度的一些建议
    isapi x86 在win7x64下的安装问题
    Entity Framwork one to one problem
    Google Talk使用技巧
  • 原文地址:https://www.cnblogs.com/--ZHIYUAN/p/7598216.html
Copyright © 2011-2022 走看看