HDU 4416 Good Article Good sentence(12年杭州网络赛-G题-SA)

题目链接:Click here~~

题意:

给一个 A 串和若干个 Bi 串,问 A 串中有多少子串不是 Bi 的子串。(na <= 1e5、Σnbi <= 1e5)

解题思路:

子串问题,很容易往 SA 的方面想。回想我们刚入门的时候,就做过 SA 统计子串个数的问题,我们可以根据那个思路继续想。

先将 Bi 的子串都接到 A 串的后面,中间用特殊字符隔开。

求出 height[] 后,对于 A 串的某个后缀 sa[i],它一共能贡献 na-sa[i] 个子串,然后我们需要减去那些不符合要求的。

不符合要求的有两种情况:1、这个子串是之前统计过的 A 的子串。2、这个子串是 Bi 的子串。

对于情况1,我们需要找到之前出现过,即 rank[ j ] < rank[ sa[i] ] 中,离 sa[i] 最近的那个且属于 A 的后缀与 sa[i] 的 lcp。

对于情况2,我们需要向两侧,找到离 sa[i] 最近的,且属于 B 的后缀的 lcp。

情况1和情况2中也会有重复,故两种情况的和,应该是两种情况的最大值。

细心的同学会发现,情况1 与 情况2 中 sa[i] 左侧最近的 B 的后缀的最大值可以化简成 heght[i] 。

而找 sa[i] 右侧最近的 B 的后缀的 lcp ,可以通过从后向前循环,不断更新 height[] 得到。(回想 lcp 的朴素求法)

p.s. 由于此题中,仅关心了 A 与 A 以及 A 与 Bi 间的 lcp,故只需将 Bi 间的分隔符与 A 与 B 的区别开即可。

如果要考虑 Bi 间的 lcp,则 Bi 间的分隔符也要互不相同。回顾分隔符的目的,是为了使求得的子串,不要跨过 2 个独立的串。

1A,爽歪歪。

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

const int N = 3e5 + 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(int)*sz);
    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(char text[],int n,int sz=128)
{
    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(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;
    }
}

char str[N],str2[N];

int main()
{
    int T,m, ncase = 0;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d",&m);
        scanf("%s",str);
        int n = strlen(str);
        int na = n;
        while(m--)
        {
            scanf("%s",str2);
            str[n++] = '#';
            for(int i=0;str2[i];i++)
                str[n++] = str2[i];
        }
        str[na] = 127;
        str[n] = '\0';
        get_sa(str,n);
        get_height(str,n);
        long long ans = 0;
        int lcp_back = 0;
        for(int i=n-1;i>=0;i--)
        {
            if(sa[i] > na)
            {
                lcp_back = height[i];
            }
            else
            {
                ans += na-sa[i]-max(height[i],lcp_back);
                lcp_back = min(lcp_back,height[i]);
            }
        }
        printf("Case %d: %I64d\n",++ncase,ans);
    }
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值