poj1743--Musical Theme(后缀数组)

题意:求一列数字中走向相同的两个字序列,长度要求大于5

题解:相邻数字求差,原题就变成求相同的长度大于4的子串。

[存疑:在保证两个子串不相交时觉得限定条件应该是大于x,但是wa了= = 不是很理解]

/**************************************
Problem: 1743        User: G_lory
Memory: 1392K        Time: 204MS
Language: G++        Result: Accepted
**************************************/
//后缀数组
#include <stdio.h>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;

const int N = int(2e5)+10;
int cmp(int *r,int a,int b,int l){
    return (r[a]==r[b]) && (r[a+l]==r[b+l]);
}
// 用于比较第一关键字与第二关键字,
// 比较特殊的地方是,预处理的时候,r[n]=0(小于前面出现过的字符)

int wa[N],wb[N],wss[N],wv[N];
int sa[N];        // 排第几的是谁 0~n-1
int rk[N],     // 谁排第几
    height[N];    // 排名相邻的两个后缀的最长公共前缀长度:suffix(sa[i-1])和(sa[i]) 的最长公共前缀,
int a[N];

void DA(int *r,int *sa,int n,int m){                    // 此处N比输入的N要多1,为人工添加的一个字符,用于避免CMP时越界
    int i,j,p,*x=wa,*y=wb,*t;
    for(i=0;i<m;i++) wss[i]=0;
    for(i=0;i<n;i++) wss[x[i]=r[i]]++;
    for(i=1;i<m;i++) wss[i]+=wss[i-1];
    for(i=n-1;i>=0;i--) sa[--wss[x[i]]]=i;
    for(j=1,p=1;p<n;j*=2,m=p)
    {
        for(p=0,i=n-j;i<n;i++) y[p++]=i;
        for(i=0;i<n;i++) if(sa[i]>=j) y[p++]=sa[i]-j;
        for(i=0;i<n;i++) wv[i]=x[y[i]];
        for(i=0;i<m;i++) wss[i]=0;
        for(i=0;i<n;i++) wss[wv[i]]++;
        for(i=1;i<m;i++) wss[i]+=wss[i-1];
        for(i=n-1;i>=0;i--) sa[--wss[wv[i]]]=y[i];
        for(t=x,x=y,y=t,p=1,x[sa[0]]=0,i=1;i<n;i++)
            x[sa[i]]=cmp(y,sa[i-1],sa[i],j)?p-1:p++;
    }
}

void calheight(int *r,int *sa,int n){                    // 此处N为实际长度
    int i,j,k=0;
    for(i=1;i<=n;i++) rk[sa[i]]=i;
    for(i=0;i<n; height[rk[i++]] = k )
    for(k?k--:0,j=sa[rk[i]-1]; r[i+k]==r[j+k]; k++);
}

bool ok(int x, int n)
{
    int minn, maxn;
    minn = maxn = sa[1];
    for (int i = 2; i <= n; ++i)
    {
        if (height[i] >= x) {
            minn = min(minn, sa[i]);
            maxn = max(maxn, sa[i]);
        } else {
            minn = maxn = sa[i];
        }
        if (maxn - minn >= x) return true;
    }
    return false;
}

int main(int argc, char const *argv[])
{
    //freopen("in", "r", stdin);
    int n;
    while (cin >> n && n) {
        for (int i = 0; i < n; ++i) scanf("%d", a+i);
        for (int i = 0; i < n-1; ++i) a[i] = a[i+1] - a[i] + 100;
        a[--n] = 0;
        DA(a, sa, n+1, 200);
        calheight(a, sa, n);
        int l = 1, r = n;
        while (l + 1 < r) {
            int mid = (l+r) >> 1;
            if (ok(mid, n)) l = mid;
            else r = mid;
        }
        ++l;
        printf("%d\n", l < 5 ? 0 : l);
    }
    return 0;
}
时间: 2024-10-08 10:04:52

poj1743--Musical Theme(后缀数组)的相关文章

[POJ1743] Musical Theme (后缀数组)

题目概述: 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 tim

[poj1743]Musical Theme后缀数组

题意:不可重叠最长重复子串 有N(1 <= N <=20000)个音符的序列来表示一首乐曲,每个音符都是1..88范围内的整数,现在要找一个重复的主题.“主题”是整个音符序列的一个子串,它需要满足如下条件: 1.长度至少为5个音符. 2.在乐曲中重复出现.(可能经过转调,“转调”的意思是主题序列中每个音符都被加上或减去了同一个整数值) 3.重复出现的同一主题不能有公共部分. 二分答案,转化为存在性判定,后缀数组问题的套路解法 1 #include <cstdlib> 2 #incl

poj 1743 Musical Theme(后缀数组)

Musical Theme Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 30544   Accepted: 10208 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

POJ 1743 Musical Theme 后缀数组 最长重复不相交子串

Musical ThemeTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://poj.org/problem?id=1743 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

POJ - 1743 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 music

Poj 1743 Musical Theme (后缀数组+二分)

题目链接: Poj  1743 Musical Theme 题目描述: 给出一串数字(数字区间在[1,88]),要在这串数字中找出一个主题,满足: 1:主题长度大于等于5. 2:主题在文本串中重复出现(或者经过调转出现,调转是主题同时加上或者减去同一个整数) 3:重复主题不能重叠 解题思路: 求调转重复出现的子串,那么主题之间的差值一定是不变的.可以求文本串s中相邻两个数的差值,重新组成一个新的文本串S,然后找S后缀串中最长公共不重叠前缀.rank相邻的后缀串,公共前缀一定最长,但是有可能重叠.

poj 1743 Musical Theme 后缀数组

题目链接 做出公差后找出最长不重叠子序列的长度. 后缀数组的模板, 二分长度k然后将height数组分组, 判断每一组内sa的最大值-sa的最小值是否大于等于k, 如果大于等于k则满足. 1 #include <iostream> 2 #include <vector> 3 #include <cstdio> 4 #include <cstring> 5 #include <algorithm> 6 #include <cmath>

POJ 1743 Musical Theme ——后缀数组

[题目分析] 其实找最长的不重叠字串是很容易的,后缀数组+二分可以在nlogn的时间内解决. 但是转调是个棘手的事情. 其实只需要o(* ̄▽ ̄*)ブ差分就可以了. 背板题. [代码] #include <cstdio> #include <cstring> #include <cmath> #include <cstdlib> #include <map> #include <set> #include <queue> #

POJ 1743 Musical Theme 后缀数组 不可重叠最长重复子串

二分长度k 长度大于等于k的分成一组 每组sa最大的和最小的距离大于k 说明可行 #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int maxn = 20010; int s[maxn]; int sa[maxn]; int t[maxn], t2[maxn], c[maxn]; int rank[maxn], height[maxn]; void

POJ-1743 Musical Theme(最长不可重叠子串,后缀数组+二分)

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; b