zoukankan      html  css  js  c++  java
  • POJ 1743 Musical Theme(后缀数组+二分答案)

    Musical Theme
    Time Limit: 1000MS   Memory Limit: 30000K
    Total Submissions: 30430   Accepted: 10183

    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

    题目链接:POJ 1743

    题意有点难懂,就是问你是否存在这样的串,是否至少在两个位置出现过且不重复覆盖的串,但是串里的数字不一定相同,只要通过整体增加K能相等即可,即1 2 3串与4 5 6串也是相同,只要前者每个数加3就变成了4 5 6,而且这样的串长度要大于等于5。

    由于整体增加K的原因,可以用原串的$arr[i+1]-arr[i]$作为新串的b[i],然后这样就解决了未知数K的问题,然后进行二分答案,由于是不重复覆盖,因此把height大于等于二分答案mid的分为同一组,由于b串是前后的差值构成,因此相邻两项对应在原串实际是有覆盖关系的,因此要用最大值-最小值>mid判断。

    代码:

    #include <stdio.h>
    #include <iostream>
    #include <algorithm>
    #include <cstdlib>
    #include <cstring>
    #include <bitset>
    #include <string>
    #include <stack>
    #include <cmath>
    #include <queue>
    #include <set>
    #include <map>
    using namespace std;
    #define INF 0x3f3f3f3f
    #define LC(x) (x<<1)
    #define RC(x) ((x<<1)+1)
    #define MID(x,y) ((x+y)>>1)
    #define fin(name) freopen(name,"r",stdin)
    #define fout(name) freopen(name,"w",stdout)
    #define CLR(arr,val) memset(arr,val,sizeof(arr))
    #define FAST_IO ios::sync_with_stdio(false);cin.tie(0);
    typedef pair<int, int> pii;
    typedef long long LL;
    const double PI = acos(-1.0);
    const int N = 20010;
    int wa[N], wb[N], cnt[N], sa[N];
    int ran[N], height[N];
    int temp[N], s[N];
    
    inline int cmp(int r[], int a, int b, int d)
    {
        return r[a] == r[b] && r[a + d] == r[b + d];
    }
    void DA(int n, int m)
    {
        int i, k;
        int *x = wa, *y = wb;
        for (i = 0; i < m; ++i)
            cnt[i] = 0;
        for (i = 0; i < n; ++i)
            ++cnt[x[i] = s[i]];
        for (i = 1; i < m; ++i)
            cnt[i] += cnt[i - 1];
        for (i = n - 1; i >= 0; --i)
            sa[--cnt[x[i]]] = i;
        for (k = 1; k <= n; k <<= 1)
        {
            int p = 0;
            for (i = n - k; i < n; ++i)
                y[p++] = i;
            for (i = 0; i < n; ++i)
                if (sa[i] >= k)
                    y[p++] = sa[i] - k;
            for (i = 0; i < m; ++i)
                cnt[i] = 0;
            for (i = 0; i < n; ++i)
                ++cnt[x[y[i]]];
            for (i = 1; i < m; ++i)
                cnt[i] += cnt[i - 1];
            for (i = n - 1; i >= 0; --i)
                sa[--cnt[x[y[i]]]] = y[i];
            swap(x, y);
            x[sa[0]] = 0;
            p = 1;
            for (i = 1; i < n; ++i)
                x[sa[i]] = cmp(y, sa[i - 1], sa[i], k) ? p - 1 : p++;
            m = p;
            if (p >= n)
                break;
        }
    }
    void gethgt(int n)
    {
        int i, k = 0;
        for (i = 1; i <= n; ++i)
            ran[sa[i]] = i;
        for (i = 0; i < n; ++i)
        {
            if (k)
                --k;
            int j = sa[ran[i] - 1];
            while (s[i + k] == s[j + k])
                ++k;
            height[ran[i]] = k;
        }
    }
    int check(int n, int mid)
    {
        int Max = -INF, Min = INF;
        for (int i = 1; i <= n; ++i)
        {
            if (height[i] >= mid)
            {
                Max = max(Max, max(sa[i], sa[i - 1]));
                Min = min(Min, min(sa[i], sa[i - 1]));
                if (Max - Min > mid)
                    return 1;
            }
            else
            {
                Max = -INF;
                Min = INF;
            }
        }
        return 0;
    }
    int main(void)
    {
        int n, i;
        while (~scanf("%d", &n) && n)
        {
            for (i = 0; i < n; ++i)
                scanf("%d", &temp[i]);
            for (i = 0; i < n - 1; ++i)
                s[i] = temp[i + 1] - temp[i] + 88 ;
            s[n - 1] = 0;
            DA(n, 190);
            gethgt(n - 1);
            int L = 0, R = n / 2;
            int ans = -1;
            while (L <= R)
            {
                int mid = MID(L, R);
                if (check(n - 1, mid))
                {
                    L = mid + 1;
                    ans = mid;
                }
                else
                    R = mid - 1;
            }
            printf("%d
    ", ans + 1 >= 5 ? ans + 1 : 0);
        }
        return 0;
    }
  • 相关阅读:
    Spring Cloud微服务实战 打造企业级优惠券系统 7-2 优惠券模块实体类相关列值枚举定义
    Spring Cloud微服务实战 打造企业级优惠券系统 7-1 创建优惠券模块微服务
    阿里云 oss (一) 工具上传图片
    Gateway 过滤器,过滤器统一异常处理
    GatewayFilterFactory 不生
    docker 安装 redis
    json 时区问题
    小程序checkbox
    小程序拍照功能
    Learning sensorimotor control with neuromorphic sensors: Toward hyperdimensional active perception
  • 原文地址:https://www.cnblogs.com/Blackops/p/7499286.html
Copyright © 2011-2022 走看看