【POJ1743】 Musical Theme (二分+后缀数组)

Musical Theme

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.

 

【题意】

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

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

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

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

 

【分析】

  二分答案,根据二分出来的答案分组,判断组内最小和最大位置的差是否大于长度即可。

 

代码如下:(以前的代码)

  1 #include<cstdio>
  2 #include<cstdlib>
  3 #include<cstring>
  4 #include<iostream>
  5 using namespace std;
  6 
  7 int sa[20010],rank[20010],y[20010],Rsort[20010];
  8 int wr[20010],a[20010],height[20010],n;
  9 
 10 bool cmp(int k1,int k2,int ln){return wr[k1]==wr[k2] && wr[k1+ln]==wr[k2+ln];}
 11 
 12 void get_sa(int m) 
 13 {
 14   int i,k,p,ln;
 15 
 16   memcpy(rank,a,sizeof(rank));
 17 
 18   memset(Rsort,0,sizeof(Rsort));
 19   for (i=1;i<=n;i++) Rsort[rank[i]]++;
 20   for (i=1;i<=m;i++) Rsort[i]+=Rsort[i-1];
 21   for (i=n;i>=1;i--) sa[Rsort[rank[i]]--]=i; 
 22   
 23   ln=1; p=0; 
 24   while (p<n)
 25   {
 26       for (k=0,i=n-ln+1;i<=n;i++) y[++k]=i;
 27       for (i=1;i<=n;i++) if (sa[i]>ln) y[++k]=sa[i]-ln;
 28       for (i=1;i<=n;i++) wr[i]=rank[y[i]];
 29       
 30       memset(Rsort,0,sizeof(Rsort));
 31       for (i=1;i<=n;i++) Rsort[wr[i]]++;
 32       for (i=1;i<=m;i++) Rsort[i]+=Rsort[i-1];
 33       for (i=n;i>=1;i--) sa[Rsort[wr[i]]--]=y[i];
 34 
 35       memcpy(wr,rank,sizeof(wr));   
 36       p=1; rank[sa[1]]=1;
 37       for (i=2;i<=n;i++)
 38       {
 39          if (!cmp(sa[i],sa[i-1],ln)) p++;
 40          rank[sa[i]]=p;
 41       }
 42       m=p; ln*=2;
 43   }
 44   a[0]=sa[0]=0;
 45 }
 46 
 47 void get_he()
 48 {
 49   int i,j,k=0;
 50   for (i=1;i<=n;i++)
 51   {
 52      j=sa[rank[i]-1];
 53      if (k) k--;
 54     
 55      while (a[j+k]==a[i+k]) k++;
 56      height[rank[i]]=k;
 57   }
 58 }
 59 
 60 bool check(int k)
 61 {
 62     int i,maxx=0,minn=20010;
 63     for(i=1;i<=n;i++)
 64     {
 65         if(height[i]<k) maxx=minn=sa[i];
 66         else
 67         {
 68             if(sa[i]>maxx) maxx=sa[i];
 69             if(sa[i]<minn) minn=sa[i];
 70             if(maxx-minn>k) return 1;
 71         }
 72     }
 73     return 0;
 74 }
 75 
 76 int hd_work()
 77 {
 78     int l,r,mid,ans=0;
 79     l=1;r=n;
 80     while(l<=r)
 81     {
 82         mid=(l+r)/2;
 83         if(check(mid)) 
 84         {
 85           l=mid+1;
 86           ans=mid;
 87         }
 88         else r=mid-1;
 89     }
 90     if(ans>=4) ans++;
 91     else ans=0;
 92     return ans;
 93 }
 94 
 95 int main()
 96 {
 97     int i,a1,a2;
 98     while(1)
 99     {
100         scanf("%d",&n);
101         if(n==0) break;
102         scanf("%d",&a1);
103         for(i=2;i<=n;i++)
104         {
105             scanf("%d",&a2);
106             a[i-1]=a2-a1+88;
107             a1=a2;//做差
108         } 
109         n--;
110         get_sa(20010);
111         get_he();
112         printf("%d\n",hd_work());
113     }
114 }
[POJ1743]

 

2016-07-20 15:31:18

posted @ 2016-07-20 15:28  konjak魔芋  阅读(388)  评论(0编辑  收藏  举报