• poj1743 Musical Theme【后缀数组】【二分】


    Musical Theme
    Time Limit: 1000MS   Memory Limit: 30000K
    Total Submissions: 35044   Accepted: 11628

    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

    题意:

    在一个序列中找这样一个连续的子序列,他的长度大于等于5, 他在总序列中重复出现。【可以是加减一个定值之后的重复出现】。问这样的子序列的最长长度。

    不重叠的重复子串。

    思路:

    首先由于他可以变调,所以其实我们找的是一个序列,序列中的相邻数之间的差值是一个定值。所以我们先把输入的序列预处理成相邻数之间的差值。

    然后在求出SA和height。

    总思路类似于https://www.cnblogs.com/wyboooo/p/9865584.html,二分答案,不同的是那道题可以重叠,这道题不行。

    区别就在于求序列长度的不同。如果height[i]>=mid, 说明存在这样一个长度大于等于mid的串。但是他们可能是重叠的。

    找到这个区间里后缀下标的最大最小值,相减就是答案的这个串,如果这个串大于k【没有等于,因为存的是相邻的差,比实际的个数要小1】,说明可行。

    还要注意n=1的情况,RE了一次。

      1 #include <iostream>
      2 #include <set>
      3 #include <cmath>
      4 #include <stdio.h>
      5 #include <cstring>
      6 #include <algorithm>
      7 #include <vector>
      8 #include <queue>
      9 #include <map>
     10 using namespace std;
     11 typedef long long LL;
     12 #define inf 0x7f7f7f7f
     13 
     14 const int maxn = 2e4 + 5;
     15 int n, k;
     16 char str[maxn];
     17 int s[maxn];
     18 int sa[maxn];
     19 int t1[maxn], t2[maxn], c[maxn];
     20 int rnk[maxn], height[maxn];
     21 
     22 
     23 
     24 void build_sa(int s[], int n, int m)
     25 {
     26     int i, j, p, *x = t1, *y = t2;
     27     for(i = 0; i < m; i++)c[i] = 0;
     28     for(i = 0; i < n; i++)c[x[i] = s[i]]++;
     29     for(i = 1; i < m; i++)c[i] += c[i - 1];
     30     for(i = n - 1; i >= 0; i--)sa[--c[x[i]]] = i;
     31     for(j = 1; j <= n; j <<= 1){
     32         p = 0;
     33         for(i = n - j; i < n; i++)y[p++] = i;
     34         for(i = 0; i < n; i++)if(sa[i] >= j)y[p++] = sa[i] - j;
     35         for(i = 0; i < m; i++)c[i] = 0;
     36         for(i = 0; i < n; i++)c[x[y[i]]]++;
     37         for(i = 1; i < m; i++)c[i] += c[i - 1];
     38         for(i = n - 1; i >= 0; i--)sa[--c[x[y[i]]]] = y[i];
     39         swap(x, y);
     40         p = 1;
     41         x[sa[0]] = 0;
     42         for(i = 1; i < n; i++)
     43             x[sa[i]] = y[sa[i - 1]] == y[sa[i]] && y[sa[i - 1] + j] == y[sa[i] + j] ? p - 1:p++;
     44         if(p >= n)break;
     45         m = p;
     46     }
     47 }
     48 
     49 void get_height(int s[], int n)
     50 {
     51     int i, j, k = 0;
     52     //cout<<"SA:"<<endl;
     53     for(i = 0; i <= n; i++){
     54         //cout<<sa[i]<<endl;
     55         rnk[sa[i]] = i;
     56     }
     57     for(i = 1; i <= n; i++){
     58         if(k) k--;
     59         j = sa[rnk[i] - 1];
     60         while(s[i + k] == s[j + k])k++;
     61         height[rnk[i]] = k;
     62     }
     63 }
     64 
     65 bool check(int t)
     66 {
     67     int mmin = sa[1], mmax = sa[1];
     68     for(int i = 2; i <= n; i++){
     69         if(height[i] < t){
     70             mmin = mmax = sa[i];
     71         }
     72         else{
     73             mmin = min(mmin, sa[i]);
     74             mmax = max(mmax, sa[i]);
     75             if(mmax - mmin > t)return true;
     76         }
     77     }
     78     return false;
     79 }
     80 
     81 
     82 int main()
     83 {
     84 
     85     while(scanf("%d", &n) != EOF && n){
     86         for(int i = 1; i <= n; i++){
     87             scanf("%d", &s[i]);
     88         }
     89         for(int i = n; i >= 1; i--){
     90             s[i] = s[i] - s[i - 1] + 90;
     91         }
     92         for(int i = 1; i < n; i++){
     93             s[i] = s[i + 1];
     94         }
     95 
     96         if(n != 1)s[n] = 0;
     97         //n--;
     98         build_sa(s, n + 1, 200);
     99         get_height(s, n);
    100 
    101         int st = 0, ed = n, ans = -1;
    102         while(st <= ed){
    103             int mid = (st + ed) / 2;
    104             //cout<<mid<<endl;
    105             if(check(mid)){
    106                 st = mid + 1;
    107                 ans = mid;
    108             }
    109             else{
    110                 ed = mid - 1;
    111             }
    112         }
    113 
    114         if(ans < 4){
    115             printf("0
    ");
    116         }
    117         else{
    118             printf("%d
    ", ans + 1);
    119         }
    120 
    121 
    122     }
    123     return 0;
    124 }
    125 
    126 /*
    127 22
    128 1 2 3 2 1 2 3 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1
    129 */
  • 相关阅读:
    win7下配置pfn

    转(每天淘汰你自己)
    [asp]替换连续多个回车换行
    安装windows组件 提示 无法加载安装安装程序:Wbemupgd.dll
    asp.net三层结构例子
    sql2005下读取字段属性
    discuz数据表结构
    Ajax2.0 'Sys'未定义的解决办法
    .net(c#)读取flash(swf)文件的尺寸
  • 原文地址:https://www.cnblogs.com/wyboooo/p/9865919.html
Copyright © 2020-2023  润新知