1. 程式人生 > >POJ-1743 Musical Theme(最長不可重疊子串,後綴數組+二分)

POJ-1743 Musical Theme(最長不可重疊子串,後綴數組+二分)

single 圖片 判斷 span rom 意思 file for each nbsp

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\n",(ans>=4? ans+1:0));
 98     }
 99     
100     return 0;
101 }
View Code

POJ-1743 Musical Theme(最長不可重疊子串,後綴數組+二分)