Musical Theme POJ - 1743(后缀数组+二分/HASH + 二分)

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.

题意: 问是否存在长度不小于5且不重叠的相同串,有的话,长度是多少。(相同串代表,串中每一个数字同时加一个数或者同时减一个数还是一样)

思路:

  • 要求重复串,可以想到LCP,于是使用后缀数组。
  • 相同串同时加一个数还是相同,可以想到差分。由于差分,所以长度-1,最后算长度的时候再+1。
  • 需要不重复。我们把h[i] ≥ k的分在一个组里面,然后组里最大下标和最小下标做个差,差的值大于k即为成立。我们要求k最大,那么二分即可。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
const int SIZE = 1000010, INF = 1 << 30;
int a[SIZE];//真实值
int sa[SIZE];//以i为起始点后缀数组的排序
int rk[SIZE];//从i开始的后缀是第j名
int fir[SIZE];//
int sec[SIZE];//
int c[SIZE];//以i为关键字的数有j个
int h[SIZE];//LCP,最长公共前缀
char str[SIZE];//字符串
int n, p;

bool comp(int i, int j, int k)
{
    return sec[i] == sec[j] && sec[i + k] == sec[j + k];
}

void sufarr(int n)
{
    int i, p, l, m = 200;
    for (i = 0;i < m;i++) c[i] = 0;
    for (i = 0;i < n;i++) c[rk[i] = a[i]]++;
    for (i = 1;i < m;i++) c[i] += c[i - 1];
    for (i = n - 1;i >= 0;i--) sa[--c[a[i]]] = i;
    
    for (l = p = 1;p < n;l *= 2, m = p)
    {
        for (p = 0, i = n - l;i < n;i++) sec[p++] = i;
        for (i = 0;i < n;i++)
            if (sa[i] >= l) sec[p++] = sa[i] - l;
        for (i = 0;i < n;i++) fir[i] = rk[sec[i]];
        
        for (i = 0;i < m;i++) c[i] = 0;
        for (i = 0;i < n;i++) c[fir[i]]++;
        for (i = 1;i < m;i++) c[i] += c[i - 1];
        for (i = n - 1;i >= 0;i--) sa[--c[fir[i]]] = sec[i];
        
        memcpy(sec, rk, sizeof(rk));
        rk[sa[0]] = 0;
        for (i = p = 1;i < n;i++)
            rk[sa[i]] = comp(sa[i], sa[i - 1], l) ? p - 1 : p++;
    }
}

void get_height()
{
    int i, j, k = 0;
    for (i = 1;i <= n;i++) rk[sa[i]] = i;
    for (i = 0;i < n;h[rk[i++]] = k)
        for (k ? k-- : 0, j = sa[rk[i] - 1];a[i + k] == a[j + k];k++);
}


int check(int n,int k)
{
    int Max = sa[1], Min = sa[1];
    for (int i = 2; i <= n; i++)
    {
        if(h[i] >= k)
        {
            if (sa[i] < Min) Min = sa[i];
            if (sa[i] > Max) Max = sa[i];
            if (Max - Min > k) return 1;
            continue;
        }
        Max = Min = sa[i];
    }
    return 0;
}

int main()
{
    while(~scanf("%d", &n) && n)
    {
        for (int i = 0;i < n;i++) scanf("%d",&a[i]);
        a[n] = 0;
        
        for (int i = n-1; i > 0; i--)
            a[i] = a[i] - a[i-1] + 90;
        n--;
        for (int i = 0; i < n; i++)
            a[i] = a[i+1];
        a[n] = 0;
        sufarr(n + 1);
        get_height();
        
        int l = 1, r = n/2,ans = 1;
        
        while(r - l >= 0)
        {
            int mid = (l + r) >> 1;
            if (check(n, mid)) {
                ans = mid;
                l = mid + 1;
            }
            else r = mid - 1;
        }
        
        if (ans < 4)
            printf("0\n");
        else printf("%d\n", ans+1);
        
    }
    return 0;
}

HASH写法

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;

typedef unsigned long long ull;
const int maxn = 2e4 + 7;
const int MOD = 10007;

struct HASHMAP
{
    int head[MOD],nex[maxn],size;
    ull state[maxn];
    int f[maxn];
    
    void init()
    {
        size = 0;
        memset(head,-1,sizeof(head));
    }
    int insert(ull val,int id)
    {
        int h = val % MOD;
        for(int i = head[h];~i;i = nex[i])
        {
            if(val == state[i])
            {
                return f[i];
            }
        }
        f[size] = id;
        state[size] = val;
        nex[size] = head[h];
        head[h] = size++;
        return f[size - 1];
    }
};

HASHMAP H;
const int SEED = 13331;
ull p[maxn];
ull s[maxn];
int a[maxn],n;

bool check(int x)
{
    H.init();
    for(int i = x;i < n;i++)
    {
        if(H.insert(s[i] - s[i - x] * p[x],i) < i - x)return true;
    }
    return false;
}

int main()
{
    p[0] = 1;
    for(int i = 1;i < maxn;i++)
        p[i] = p[i - 1] * SEED;
    while(~scanf("%d",&n) && n)
    {
        for(int i = 1;i <= n;i++)scanf("%d",&a[i]);
        for(int i = 1;i < n;i++)
            a[i] = a[i + 1] - a[i];
        s[0] = 0;
        for(int i = 1;i < n;i++)
            s[i] = s[i - 1] * SEED + a[i];
        
        int l = 1,r = n / 2,ans = -1;
        while(r - l >= 0)
        {
            int mid = (l + r) >> 1;
            if(check(mid))
            {
                ans = mid;
                l = mid + 1;
            }
            else
            {
                r = mid - 1;
            }
        }
        if(ans < 4)printf("0\n");
        else printf("%d\n",ans + 1);
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值