【poj1743】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 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

LouTiancheng@POJ

又是后缀数组…

求差分数列的最长重复子串,使得重复的串在原数列中不互相重复,若小于5则输出0。

差分后后缀数组,然后二分答案len,在高度数组中找到连续的一段使得它们的高度都大于len,小于len的可以作为分割线把lcp数组分组,然后记录每组sa的最大和最小值,然后作查看看是否大于len,若大于len则证明存在。是大于而不是大于等于是因为是差分数列。

对拍百万组数据没拍出错,交上去就WA,我放弃了…求解

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

const int SZ = 1000010;
const int INF = 1000000010;

int n,lcp[SZ],sa[SZ],rank[SZ],k = 1,tmp[SZ];

bool cmp_sa(int i,int j)
{
    if(rank[i] != rank[j]) return rank[i] < rank[j];
    else
    {
        int x = i + k <= n ? rank[i + k] : -1;
        int y = j + k <= n ? rank[j + k] : -1;
        return x < y;
    }
}

void get_sa(int s[])
{
    for(int i = 0;i <= n;i ++)
    {
        sa[i] = i;
        rank[i] = i == n ? -INF : s[i];
    }
    for(k = 1;k <= n;k <<= 1)
    {
        sort(sa,sa + 1 + n,cmp_sa);

        tmp[sa[0]] = 0;
        for(int i = 1;i <= n;i ++)
            tmp[sa[i]] = tmp[sa[i - 1]] + (cmp_sa(sa[i - 1],sa[i]) ? 1 : 0);
        for(int i = 0;i <= n;i ++)
            rank[i] = tmp[i];
    }
}

void get_lcp(int s[])
{
    int h = 0;
    for(int i = 0;i <= n;i ++)
        rank[sa[i]] = i;
    lcp[0] = 0;
    for(int i = 0;i < n;i ++)
    {
        int j = sa[rank[i] - 1];
        if(h) h --;
        while(i + h < n && j + h < n)
        {
            if(s[i + h] == s[j + h]) h ++;
            else break;
        }
        lcp[rank[i] - 1] = h;
    }
}

bool check(int len)
{
    int minsa = sa[0],maxsa = sa[0];
    for(int i = 1;i < n - 1;i ++)
    {
        if(lcp[i] >= len)
        {
            minsa = min(minsa,sa[i]);           
            maxsa = max(maxsa,sa[i]);           
    // minsa = min(minsa,sa[i + 1]); 
    // maxsa = max(maxsa,sa[i + 1]);
            if(maxsa - minsa > len) {return true;}          
        }
        else
            minsa = maxsa = 0;
    }
    return false;
}

int div()
{
    int l = 0,r = n,ans;
    while(l <= r)
    {
        int mid = (l + r) >> 1;
        if(check(mid)) ans = mid,l = mid + 1;
        else r = mid - 1;
    }
    return ans;
}


int s[SZ];

int main()
{
    freopen("poj1743.in","r",stdin);    
    freopen("poj1743.out","w",stdout);  
    while(~scanf("%d",&n) && n)
    {
    // init();
        for(int i = 0;i < n;i ++)
            scanf("%d",&s[i]);
        if(n <= 5) { puts("0"); continue; }
        for(int i = 0;i < n - 1;i ++)
            s[i] = s[i + 1] - s[i] + 100;
        n --;
        s[n] = 0;
        get_sa(s);
        get_lcp(s);
/* for(int i = 0;i <= n;i ++) printf("%d ",s[i]); puts(""); for(int i = 0;i <= n;i ++) printf("%d ",sa[i]); puts(""); for(int i = 0;i <= n;i ++) printf("%d ",rank[i]); puts(""); for(int i = 0;i <= n;i ++) printf("%d ",lcp[i]); puts("");*/
        int ans = div();
        printf("%d\n",ans < 4 ? 0 : ans + 1);
    }
    return 0;
}

/* 8 1 1 2 1 1 1 1 2 11 1 1 1 1 1 1 1 1 1 1 1 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 */

你可能感兴趣的:(【poj1743】Musical Theme 后缀数组+二分)