zoukankan      html  css  js  c++  java
  • POJ1743 Musical Theme —— 后缀数组 重复出现且不重叠的最长子串

    题目链接:https://vjudge.net/problem/POJ-1743

    Musical Theme
    Time Limit: 1000MS   Memory Limit: 30000K
    Total Submissions: 32402   Accepted: 10808

    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

    题意:

    给出一串数字,定义theme为:长度不小于5,从左到右以相同的变化规律出现了不止一次,并且不能重叠。求最长的theme。实际上是求:字符串的重复出现且不重叠的最长子串。

    题解:

    1.由于求的是变化规律,所以要求出相邻两个数的差值,得到新的一串数字。然后求出新串的后缀数组。

    2.二分答案,即“重复出现且不重叠的最长子串”的长度k。然后根据是否存在这样的子串来缩小k的范围,最终得到答案。那么怎样判断是否存在“重复出现且不重叠的长度为k的子串”呢?

    2.1 把后缀按名次排成一列,如果前m个后缀(第一名除外)与它的前一名的最长公共前缀都大于等于k(二分时的mid),即height[2~m]>=k,则可以说明这m个后缀的最长公共前缀大于等于k。所以可以得出结论:k把所有后缀分成若干组,并且每一组的最长公共前缀大于等于k(可以单独一个后缀作为一组)。那么,我们只需要判断:是否存在一组后缀,使得max(sa[i]) - min(sa[i]) >= k。

    2.2 视图更加直观:

    2.3 参考:http://blog.csdn.net/huangzhengdoc/article/details/53573198

    代码如下:

      1 #include <iostream>
      2 #include <cstdio>
      3 #include <cstring>
      4 #include <algorithm>
      5 #include <vector>
      6 #include <cmath>
      7 #include <queue>
      8 #include <stack>
      9 #include <map>
     10 #include <string>
     11 #include <set>
     12 using namespace std;
     13 typedef long long LL;
     14 const double EPS = 1e-6;
     15 const int INF = 2e9;
     16 const LL LNF = 9e18;
     17 const int MOD = 1e5;
     18 const int MAXN = 20000+10;
     19 
     20 bool cmp(int *r, int a, int b, int l)
     21 {
     22     return r[a]==r[b] && r[a+l]==r[b+l];
     23 }
     24 
     25 int r[MAXN], sa[MAXN], Rank[MAXN], height[MAXN];
     26 int t1[MAXN], t2[MAXN], c[MAXN];
     27 void DA(int str[], int sa[], int Rank[], int height[], int n, int m)
     28 {
     29     n++;
     30     int i, j, p, *x = t1, *y = t2;
     31     for(i = 0; i<m; i++) c[i] = 0;
     32     for(i = 0; i<n; i++) c[x[i] = str[i]]++;
     33     for(i = 1; i<m; i++) c[i] += c[i-1];
     34     for(i = n-1; i>=0; i--) sa[--c[x[i]]] = i;
     35     for(j = 1; j<=n; j <<= 1)
     36     {
     37         p = 0;
     38         for(i = n-j; i<n; i++) y[p++] = i;
     39         for(i = 0; i<n; i++) if(sa[i]>=j) y[p++] = sa[i]-j;
     40 
     41         for(i = 0; i<m; i++) c[i] = 0;
     42         for(i = 0; i<n; i++) c[x[y[i]]]++;
     43         for(i = 1; i<m; i++) c[i] += c[i-1];
     44         for(i = n-1; i>=0; i--) sa[--c[x[y[i]]]] = y[i];
     45 
     46         swap(x, y);
     47         p = 1; x[sa[0]] = 0;
     48         for(i = 1; i<n; i++)
     49             x[sa[i]] = cmp(y, sa[i-1], sa[i], j)?p-1:p++;
     50         if(p>=n) break;
     51         m = p;
     52     }
     53 
     54     int k = 0;
     55     n--;
     56     for(i = 0; i<=n; i++) Rank[sa[i]] = i;
     57     for(i = 0; i<n; i++)
     58     {
     59         if(k) k--;
     60         j = sa[Rank[i]-1];
     61         while(str[i+k]==str[j+k]) k++;
     62         height[Rank[i]] = k;
     63     }
     64 }
     65 
     66 bool test(int mid, int n)
     67 {
     68     int minn = sa[1], maxx = sa[1];
     69     for(int i = 2; i<=n; i++)
     70     {
     71         if(height[i]<mid)
     72             minn = maxx = sa[i];
     73         else
     74         {
     75             maxx = max(maxx, sa[i]);
     76             minn = min(minn, sa[i]);
     77             if(maxx-minn>=mid)
     78                 return true;
     79         }
     80     }
     81     return false;
     82 }
     83 
     84 int main()
     85 {
     86     int n;
     87     while(scanf("%d",&n)&&n)
     88     {
     89         for(int i = 0; i<n; i++) scanf("%d", &r[i]);
     90         for(int i = 0; i<n-1; i++) r[i] = r[i+1]-r[i]+100;
     91         r[--n] = 0;
     92         DA(r, sa, Rank, height, n, 200);
     93         int l = 0, r = n/2;
     94         while(l<=r)
     95         {
     96             int mid = (l+r)>>1;
     97             if(test(mid, n))
     98                 l = mid + 1;
     99             else
    100                 r = mid - 1;
    101         }
    102         if(r<4) printf("0
    ");
    103         else printf("%d
    ", r+1);
    104     }
    105 }
    View Code
  • 相关阅读:
    array and ram
    char as int
    pointer of 2d array and address
    Install SAP HANA EXPRESS on Google Cloud Platform
    Ubuntu remount hard drive
    Compile OpenSSL with Visual Studio 2019
    Install Jupyter notebook and tensorflow on Ubuntu 18.04
    Build OpenCV text(OCR) module on windows with Visual Studio 2019
    Reinstall VirtualBox 6.0 on Ubuntu 18.04
    Pitfall in std::vector<cv::Mat>
  • 原文地址:https://www.cnblogs.com/DOLFAMINGO/p/8466841.html
Copyright © 2011-2022 走看看