zoukankan      html  css  js  c++  java
  • POJ1743 (Musical Theme,后缀数组)

    Links

    Musical Theme
    Time Limit: 1000MS   Memory Limit: 30000K
         

    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

     
    后缀数组裸题。。楼教主男人八题之一..  = =
    二分长度。。检测的时候按照height分组。。
     1 #include<set>
     2 #include<map>
     3 #include<cmath>
     4 #include<ctime>
     5 #include<queue>
     6 #include<vector>
     7 #include<cstdio>
     8 #include<cstring>
     9 #include<cstdlib>
    10 #include<iostream>
    11 #include<algorithm>
    12 using namespace std;
    13 const int N = 20010;
    14 #define rep(i,n) for(int i=0;i<n;i++)
    15 #define Rep(i,n) for(int i=1;i<=n;i++)
    16 #define For(i,l,r) for(int i=l;i<=r;i++)
    17 #define down(i,n) for(int i=n-1;i>=0;i--)
    18 
    19 int rank[N],height[N],UN,st[N],wa[N],wb[N],cnt[N],tx[N],sa[N];
    20 
    21 bool cmp(int *r,int a,int b,int l){
    22     return (r[a]==r[b])&&(r[a+l]==r[b+l]);
    23 }
    24 
    25 void BuildSA(int *r,int *sa,int n,int m){
    26     int i,p,d,*x=wa,*id=wb,*t;
    27     rep(i,m) cnt[i]=0;
    28     rep(i,n) cnt[x[i]=r[i]]++;
    29     rep(i,m) cnt[i+1]+=cnt[i];
    30     down(i,n) sa[--cnt[x[i]]]=i;
    31     for(p=1,d=1;p<n;d*=2,m=p){
    32         for(p=0,i=n-d;i<n;i++) id[p++]=i;
    33         rep(i,n) if(sa[i]>=d) id[p++]=sa[i]-d;
    34         rep(i,n) tx[i]=x[id[i]];
    35         rep(i,m) cnt[i]=0;
    36         rep(i,n) cnt[tx[i]]++;
    37         rep(i,m) cnt[i+1]+=cnt[i];
    38         down(i,n) sa[--cnt[tx[i]]]=id[i];
    39         swap(x,id);p=1;x[sa[0]]=0;
    40         Rep(i,n-1)
    41           x[sa[i]]=cmp(id,sa[i-1],sa[i],d)?(p-1):(p++);
    42     }
    43 }
    44 
    45 bool Check(int Lim){
    46     int Max=sa[1],Min=sa[1];
    47     For(i,2,UN)
    48       if(height[i]<Lim) Min=Max=sa[i];
    49       else{
    50           Max=max(Max,sa[i]);Min=min(Min,sa[i]);
    51           if(Max-Min>Lim) return true;
    52       }
    53     return false;
    54 }
    55 
    56 void Binary(int Left,int Right){
    57     while(Right-Left>1){
    58         int Mid=(Left+Right)>>1;
    59         if(Check(Mid)) Left=Mid;
    60         else           Right=Mid;
    61     }
    62     if(Check(Right)) Left=Right;
    63     if(Left>=4) Left++;
    64     else        Left=0;
    65     printf("%d
    ",Left);
    66 }
    67 
    68 void CalHeight(int *st,int *sa,int n){
    69     int i,j,k=0;
    70     Rep(i,n) rank[sa[i]]=i;
    71     for(i=0;i<n;height[rank[i++]]=k)
    72       for(k?(k--):(0),j=sa[rank[i]-1];st[i+k]==st[j+k];k++);
    73 }
    74 
    75 int main(){
    76     int n,Last=0,Temp;
    77     while(scanf("%d",&n),n){
    78         rep(i,n){
    79             scanf("%d",&Temp);
    80             st[i]=Temp-Last+90;
    81             Last=Temp;
    82         }
    83         st[n]=0;
    84         BuildSA(st,sa,n+1,200);
    85         UN=n+1;
    86         CalHeight(st,sa,n);
    87         Binary(1,n);
    88     }
    89     return 0;
    90 }
  • 相关阅读:
    SPRING IN ACTION 第4版笔记-第九章Securing web applications-008-使用非关系型数据库时如何验证用户(自定义UserService)
    [转]苹果商店审核规则,你触犯了哪一条?
    Xcode itunes完美打包api方法
    Swift中构造器的继承和重写
    Swift中类的两段式构造(类的构造过程)
    Swift中的便利构造器和构造器链
    iOS在UITableViewController里使用UISearchDisplayController报错"[UISearchResultsTableView dequeueReusableCellWithIdentifier:forIndexPath:]"
    Swift缩水版MJExtension
    构造器相关
    可选链
  • 原文地址:https://www.cnblogs.com/zjdx1998/p/3993566.html
Copyright © 2011-2022 走看看