前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >POJ1743 Musical Theme(后缀数组 二分)

POJ1743 Musical Theme(后缀数组 二分)

作者头像
attack
发布2018-07-05 10:45:10
3130
发布2018-07-05 10:45:10
举报
文章被收录于专栏:数据结构与算法

Time Limit: 1000MS

Memory Limit: 30000K

Total Submissions: 33462

Accepted: 11124

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

代码语言:javascript
复制
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

代码语言:javascript
复制
5

Hint

Use scanf instead of cin to reduce the read time.

Source

题目大意:

给出一个长度为$n$的序列,让你找出最长的相似子串。这里相似定义为两个串每次字符对应的差值相同

Sol: 很显然,我们可以对原序列进行差分,这样如果在原序列中长度为$n$的互不相交的相同的字符串,那么答案为$n + 1$

这是一个经典的问题。首先二分答案,然后对$height$数组分组,若$sa[i] - sa[j] > ans$那么可以更新答案

代码语言:javascript
复制
#include<cstdio>
#include<algorithm>
using namespace std;
const int MAXN = 1e5 + 10;
inline int read() {
    char c = getchar(); int x = 0, f = 1;
    while(c < '0' || c > '9'){if(c == '-') f = -1; c = getchar();}
    while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
    return x * f;
}
int N;
int s[MAXN], sa[MAXN], rak[MAXN], tp[MAXN], tax[MAXN], height[MAXN], P, M;
void Qsort() {
    for(int i = 0; i <= M; i++) tax[i] = 0;
    for(int i = 1; i <= N; i++) tax[rak[i]]++;
    for(int i = 1; i <= M; i++) tax[i] += tax[i - 1];
    for(int i = N; i >= 1; i--) sa[ tax[rak[tp[i]]]-- ] = tp[i];
}
void SuffixSort() {
    M = 233;
    for(int i = 1; i <= N; i++) rak[i] = s[i], tp[i] = i;
    Qsort();
    for(int w = 1, p = 0; p < N; M = p, w <<= 1) {
        p = 0;
        for(int i = 1; i <= w; i++) tp[++p] = N - i + 1;
        for(int i = 1; i <= N; i++) if(sa[i] > w) tp[++p] = sa[i] - w;
        Qsort(); swap(tp, rak);
        rak[sa[1]] = p = 1;
        for(int i = 2; i <= N; i++) 
            rak[sa[i]] = (tp[sa[i]] == tp[sa[i - 1]] && tp[sa[i] + w] == tp[sa[i - 1] + w]) ? p : ++p;
        
    }
    int j = 0, k = 0;
    for(int i = 1; i <= N; i++) {
        if(k) k--;
        int j = sa[rak[i] - 1];
        while(s[i + k] == s[j + k]) k++;
        height[rak[i]] = k;
    }
    //for(int i = 1; i <= N; i++) printf("%d ", sa[i]); puts("");
}
bool check(int len) {
    int mx = sa[1],  mi = sa[1];
    for(int i = 2; i <= N; i++) {
        if(height[i] >= len - 1) 
            mx = max(sa[i], mx),
            mi = min(sa[i], mi);
         else 
            mx = mi = sa[i];
        if(mx - mi >= len) return 1;
    }
    return 0;
}
int solve() {
    int l = 0, r = N, ans = 0;
    while(l <= r) {
        int mid = l + r >> 1;
        if(check(mid)) l = mid + 1, ans = mid;
        else r = mid - 1;
    }
    return ans;
}
int main() {
    while(scanf("%d", &N) && N != 0) {
        for(int i = 1; i <= N; i++) s[i] = read();
        for(int i = N; i >= 1; i--) s[i] -= s[i - 1] - 100;
        SuffixSort();
        int ans = solve();
        if(ans >= 5) 
            printf("%d\n", ans);
        else 
            printf("%d\n", 0);        
    }

    return 0;
}
/*
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
*/
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2018-07-04 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档