• POJ-1743 Musical Theme(最长不可重叠子串,后缀数组+二分)


    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.
     

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

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

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

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

    思路:后缀数组。求出任意相邻音符的差值,然后把问题转化为不可重叠最长重复子串,用后缀数组来做。先二分答案,把题目变成判定性问题:判断是否存在两个长度为k的子串是相同的,且不重叠。

    先不考虑重叠,重复子串的长度要大于等于k,也就是一个区间内的height值都大于等于k,当出现height小于k则重新定位。

    再来考虑重叠,我们知道了一个区间的height都大于等于k,如果存在两个后缀距离大于k,那么可以肯定存在两个长度为k的子串是相同的,且不重叠。

    参考代码:

      1 //#include<bits/stdc++.h>
      2 #include<iostream>
      3 #include<cstdio>
      4 #include<cstring>
      5 #include<cmath>
      6 #include<cstdlib>
      7 #include<string>
      8 using namespace std;
      9 #define clr(a,val) memset(a,val,sizeof a)
     10 const int maxm=20010;
     11 int N;
     12 struct SuffixArray{
     13     int s[maxm];
     14     int sa[maxm],height[maxm],rank[maxm],n;
     15     int t[maxm*2],t2[maxm*2];
     16     long long cnt[maxm];
     17     void build_sa(int m){
     18         int i,*x=t,*y=t2;
     19         for(i=0;i<m;i++) cnt[i]=0;
     20         for(i=0;i<n;i++) cnt[x[i]=s[i]]++;
     21         for(i=1;i<m;i++) cnt[i]+=cnt[i-1];
     22         for(i=n-1;i>=0;i--) sa[--cnt[x[i]]]=i; 
     23         for(int k=1,p=0;k<n;k <<=1)
     24         {
     25             p=0;
     26             for(i=n-k;i<n;i++) y[p++]=i;
     27             for(i=0;i<n;i++) if(sa[i]>=k) y[p++]=sa[i]-k;
     28             for(i=0;i<m;i++) cnt[i]=0;
     29             for(i=0;i<n;i++) cnt[x[y[i]]]++;
     30             for(i=1;i<m;i++) cnt[i]+=cnt[i-1];
     31             for(i=n-1;i>=0;i--) sa[--cnt[x[y[i]]]]=y[i];
     32             swap(x,y);
     33             p=1;x[sa[0]]=0;
     34             for(i=1;i<n;i++)
     35                    if(y[sa[i-1]]==y[sa[i]]&&y[sa[i-1]+k]==y[sa[i]+k])
     36                     x[sa[i]]=p-1;
     37                   else x[sa[i]]=p++;
     38             if(p>=n) 
     39               break;
     40             m=p;
     41         }
     42     }
     43     void build_height()
     44     {
     45         int k=0;
     46         for(int i=0;i<n;i++) rank[sa[i]]=i;
     47         for(int i=0;i<n;i++)
     48         {
     49             if(k) k--;
     50             if(!rank[i]) continue;
     51             int j=sa[rank[i]-1];
     52             while(s[i+k]==s[j+k]) k++;
     53             height[rank[i]]=k;
     54         }
     55     }
     56 } SA;
     57 
     58 bool check(int key)
     59 {
     60     int tMax = SA.sa[1];
     61     int tMin = SA.sa[1];
     62     for (int i = 2; i<=N; ++i)
     63     {
     64         if (SA.height[i] < key) tMax = tMin = SA.sa[i];
     65         else
     66         {
     67             if(SA.sa[i] < tMin) tMin = SA.sa[i];
     68             if(SA.sa[i] > tMax) tMax = SA.sa[i];
     69             if(tMax - tMin > key) return true;
     70         }
     71     }
     72     return false;
     73 }
     74 int main()
     75 {
     76     while(~scanf("%d",&N)&&N)
     77     {
     78         SA.n=N;
     79         int t,k;N--;
     80         scanf("%d",&t);
     81         for(int i=0;i<N;++i)
     82         {
     83             scanf("%d",&k);
     84             SA.s[i]=k-t+100;
     85             t=k;
     86         }
     87         SA.s[N]=0;
     88         SA.build_sa(200);
     89         SA.build_height();
     90         int L=4,R=N/2,ans=0;
     91         while(L<=R)
     92         {
     93             int mid=L+R>>1;
     94             if(check(mid)) ans=mid,L=mid+1;
     95             else R=mid-1;
     96         }
     97         printf("%d
    ",(ans>=4? ans+1:0));
     98     }
     99     
    100     return 0;
    101 }
    View Code
  • 相关阅读:
    javascript通用函数库
    Nginx的负载均衡的那点事
    四种多服务器共享session的方法
    Nginx 配置文件nginx.conf的完整配置说明<转>
    Struts2标签问题-using Struts tags without the associat
    转:ibatis的cacheModel
    大型互联网网站架构心得之一
    nginx的upstream(解决session问题)
    rails3项目解析之1——系统架构
    使用 Nginx 提升网站访问速度
  • 原文地址:https://www.cnblogs.com/csushl/p/10035552.html
Copyright © 2020-2023  润新知