POJ 1743 Musical Theme(SA 求最长不可重叠重复子串)

题目链接:Click here~~

题意:

题意是直接搜的。将数列相邻两项作差,得到一个新的数列,求这个数列的最长不重叠子串,最后输出这个最长长度+1。

解题思路:

竟然是传说中的 楼爷男人8题 之一。先 mark 一下。

首先,如果求的是 最长可重叠重复子串,答案便是 height[] 中的最大值。

证明:求最长重复子串,等价于求任意两个后缀 lcp 的最大值,而任意两个后缀 lcp 等于 height[] 中某一段的最小值,故结果不会大于 height[] 的最大值。

回到本题,求的是 最长不可重叠重复子串。做法是二分,将题目变成如下判定性问题:是否存在两个长度为 k 的不重叠子串完全相同。

然后此问题的做法是,将排序后的后缀分成若干组,每组相邻后缀之间的 height[] 都不小于k。

直观上看,这样分组等价于使每组的这些后缀都拥有某个长度不小于 k 的共同的前缀,即重复子串。再判断下是否能满足不重叠的条件即可。

Trick:1、数列有可能为负,故需要向右平移。2、平移后会溢出 128,故不能用 char[],sz 也不能取128。3、最后要的是音符长度,即数列长度+1。

#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;

const int N = 2e4 + 5;

int sa[N],rank[N],rank2[N],height[N],cnt[N],*x,*y;

/*
    * a radix_sort which is based on the y[].
    * how ? ahhhh, the last reverse for is the solution.
    * and the adjacant value of sa[] might have the same rank.
*/
void radix_sort(int n,int sz)
{
    memset(cnt,0,sizeof(cnt));
    for(int i=0;i<n;i++)
        cnt[ x[ y[i] ] ]++;
    for(int i=1;i<sz;i++)
        cnt[i] += cnt[i-1];
    for(int i=n-1;i>=0;i--)
        sa[ --cnt[ x[ y[i] ] ] ] = y[i];
}

/*
    * sa[i] represents the ith suffix string is which one.
    * rank[i] represents the suffix string [i,n]'s rank.
    * sz is the max_rank of text in that time.
    * x[] represents the true pointer of rank[] in that time and it may be not unique.
    * y[] is the location of text[] which is sorted by 2nd key in that time before swap(x,y).
*/
void get_sa(unsigned char text[],int n,int sz=176)
{
    x = rank, y = rank2;
    for(int i=0;i<n;i++)
        x[i] = text[i], y[i] = i;
    radix_sort(n,sz);
    for(int len=1;len<n;len<<=1)
    {
        int yid = 0;
        for(int i=n-len;i<n;i++)
            y[yid++] = i;
        for(int i=0;i<n;i++)
            if(sa[i] >= len)
                y[yid++] = sa[i] - len;
        radix_sort(n,sz);
        swap(x,y);
        x[ sa[0] ] = yid = 0;
        for(int i=1;i<n;i++)
        {
            if(y[ sa[i-1] ]==y[ sa[i] ] && sa[i-1]+len<n && sa[i]+len<n && y[ sa[i-1]+len ]==y[ sa[i]+len ])
                x[ sa[i] ] = yid;
            else
                x[ sa[i] ] = ++yid;
        }
        sz = yid + 1;
        if(sz >= n)
            break;
    }
    for(int i=0;i<n;i++)
        rank[i] = x[i];
}

/*
    * height[] represents the longest common prefix of suffix [i-1,n] and [i,n].
    * height[ rank[i] ] >= height[ rank[i-1] ] - 1.
    ..... let's call [k,n] is the suffix which rank[k] = rank[i-1] - 1,
    ...=> [k+1,n] is a suffix which rank[k+1] < rank[i]
    ..... and the lcp of [k+1,n] and [i,n] is height[ rank[i] ] - 1.
    ..... still unknow ? height[ rank[i] ] is the max lcp of rank[k] and rank[i] which rank[k] < rank[i].
*/
void get_height(unsigned char text[],int n)
{
    int k = 0;
    for(int i=0;i<n;i++)
    {
        if(rank[i] == 0)
            continue;
        k = max(0,k-1);
        int j = sa[ rank[i]-1 ];
        while(i+k<n && j+k<n && text[i+k]==text[j+k])
            k++;
        height[ rank[i] ] = k;
    }
}

bool can(int k,int n)
{
    for(int i=0;i<n;i++)
    {
        int loc_min = sa[i] , loc_max = sa[i];
        while(i+1 < n && height[i+1] >= k)
        {
            loc_min = min(loc_min,sa[i+1]);
            loc_max = max(loc_max,sa[i+1]);
            i++;
        }
        if(loc_max - loc_min >= k)
            return true;
    }
    return false;
}

unsigned char str[N];

int a[N];

int main()
{
    //freopen("in.ads","r",stdin);
    //freopen("out.ads","w",stdout);
    int n;
    while(scanf("%d",&n),n)
    {
        for(int i=0;i<n;i++)
            scanf("%d",&a[i]);
        for(int i=0;i<n-1;i++)
            str[i] = a[i+1] - a[i] + 87;
        str[--n] = '\0';
        get_sa(str,n);
        get_height(str,n);
        int l = 0 , r = n;
        while(l < r)
        {
            int mid = l+r >> 1;
            if(can(mid,n))
                l = mid + 1;
            else
                r = mid;
        }
        printf("%d\n",r<5?0:r);
    }
    return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值