POJ 1743 Musical Theme(字尾陣列)

畫船聽雨發表於2015-01-22

題目大意:給出n個數字。首先將這n個數前後做差,得到另一個長度是n-1的序列。求出這個序列的最長重複子串,且這些子串不能重疊。

PS:這題論文上有解析。

解題思路:先二分答案,把題目變成判斷性問題:判斷是否存在兩個長度為k的字串是否相同的,且不重疊。解決這個問題的關鍵還是利用height陣列。把排序後的字尾陣列分成若干組,其中每組的字尾之間的height值都不小於k。

容易看出,有希望成為最長公共字首不小於k的兩個字尾一定在同一組。然後對於每組字尾,只須判斷每個字尾的sa值的最大值和最小值之差是否不小於k。如果有一組滿足,則說明存在,否則不存在。整個做法的時間複雜度為O(nlogn)

論文上寫的很詳細了啊,不過需要注意細節,比如二分的邊界,以及字串的長度等等。。。

Musical Theme
Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 19851   Accepted: 6779

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.
#include <algorithm>
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <iomanip>
#include <stdio.h>
#include <string>
#include <queue>
#include <cmath>
#include <stack>
#include <ctime>
#include <map>
#include <set>
#define eps 1e-9
///#define M 1000100
///#define LL __int64
#define LL long long
///#define INF 0x7ffffff
#define INF 0x3f3f3f3f
#define PI 3.1415926535898
#define zero(x) ((fabs(x)<eps)?0:x)
#define mod 1000000007
#define Read() freopen("autocomplete.in","r",stdin)
#define Write() freopen("autocomplete.out","w",stdout)
#define Cin() ios::sync_with_stdio(false)

using namespace std;



inline int read()
{
    char ch;
    bool flag = false;
    int a = 0;
    while(!((((ch = getchar()) >= '0') && (ch <= '9')) || (ch == '-')));
    if(ch != '-')
    {
        a *= 10;
        a += ch - '0';
    }
    else
    {
        flag = true;
    }
    while(((ch = getchar()) >= '0') && (ch <= '9'))
    {
        a *= 10;
        a += ch - '0';
    }
    if(flag)
    {
        a = -a;
    }
    return a;
}
void write(int a)
{
    if(a < 0)
    {
        putchar('-');
        a = -a;
    }
    if(a >= 10)
    {
        write(a / 10);
    }
    putchar(a % 10 + '0');
}

const int maxn = 20010;
int wa[maxn], wb[maxn], wv[maxn], ws1[maxn];
int sa[maxn];
int cmp(int *r, int a, int b, int l)
{
    return r[a]==r[b]&&r[a+l]==r[b+l];
}
void da(int *r,int *sa,int n,int m)
{
    int i, j, p, *x = wa,*y = wb;

    for(i = 0; i<m; i++) ws1[i]=0;
    for(i = 0; i<n; i++) ws1[x[i]=r[i]]++;
    for(i = 1; i<m; i++) ws1[i]+=ws1[i-1];
    for(i = n-1; i>=0; i--) sa[--ws1[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++) ws1[i]=0;
        for(i = 0; i<n; i++) ws1[wv[i]]++;
        for(i = 1; i<m; i++) ws1[i]+=ws1[i-1];
        for(i = n-1; i>=0; i--) sa[--ws1[wv[i]]]=y[i];
        for(swap(x,y),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++;
    }
    return;
}

int rank[maxn],height[maxn];
void calheight(int *r,int *sa,int n)
{
    int i,j,k=0;
    for(i = 1; i<=n; i++) rank[sa[i]]=i;
    for(i = 0; i<n; height[rank[i++]]=k)
        for(k?k--:0, j=sa[rank[i]-1]; r[i+k] == r[j+k]; k++);
    return;
}

int sqe[maxn];


bool judge(int mid, int n)
{
    int Max, Min;
    for(int i = 2; i <= n; i++)
    {
        Max = sa[i-1];
        Min = sa[i-1];
        while(height[i] >= mid)
        {
            Max = max(sa[i], Max);
            Min = min(sa[i], Min);
            i++;
        }
        if(Max - Min > mid) return true;
    }
    return false;
}

int main()
{
    int n;
    Cin();
    while(cin >>n)
    {
        if(!n) break;
        for(int i = 0; i < n; i++) cin >>sqe[i];
        if(n <= 9)
        {
            puts("0");
            continue;
        }
        for(int i = 0; i < n-1; i++) sqe[i] = sqe[i+1]-sqe[i]+88;
        sqe[n-1] = 0;

        da(sqe, sa, n, 200);
        calheight(sqe, sa, n-1);

        int l = 4;
        int r = n / 2;
        int ans = -1;
        while(l <= r)
        {
            int mid = (l+r)>>1;
            if(judge(mid, n))
            {
                l = mid+1;
                ans = mid;
            }
            else r = mid-1;
        }
        cout<<ans+1<<endl;
    }
    return 0;
}




相關文章