[SPOJ 705] New Distinct Substrings

本文介绍了一种利用后缀数组和LCP数组计算字符串中不同子串数量的方法。通过按后缀数组顺序加入新后缀,计算产生的新子串数量,并减去与已有子串重复的部分。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


题目链接,求长度为 n 的字符串中不同子串个数



maya,一不小心就看到题解了, ppfish大爷的题解

按照 sa[i] 的顺序考虑,加入一个新的 sa[i] ,会产生新的 nsa[i]+1 个子串

而这些子串中与前面所有子串中重复的个数为 height[i]

因此每一个新的后缀对答案的贡献为 nsa[i]+1height[i]



真是很有意思的题目啊~!


// SPOJ 1705

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <vector>
#include <utility>
#include <stack>
#include <queue>
#include <iostream>
#include <algorithm>

template<class Num>void read(Num &x)
{
    char c; int flag = 1;
    while((c = getchar()) < '0' || c > '9')
        if(c == '-') flag *= -1;
    x = c - '0';
    while((c = getchar()) >= '0' && c <= '9')
        x = (x<<3) + (x<<1) + (c-'0');
    x *= flag;
    return;
}
template<class Num>void write(Num x)
{
    if(x < 0) putchar('-'), x = -x;
    static char s[20];int sl = 0;
    while(x) s[sl++] = x%10 + '0',x /= 10;
    if(!sl) {putchar('0');return;}
    while(sl) putchar(s[--sl]);
}

const int maxn = 50020;
char s[maxn];
int n, sa[maxn], rank[maxn];
int c[maxn], height[maxn];
long long ans;

void build_sa(int m)
{
    static int t0[maxn], t1[maxn];
    int *x = t0, *y = t1;

    for(int i = 1; i <= m; i++) c[i] = 0;
    for(int i = 1; i <= n; i++) c[x[i] = s[i]]++;
    for(int i = 1; i <= m; i++) c[i] += c[i - 1];
    for(int i = n; i >= 1; i--) sa[c[x[i]]--] = i;

    for(int k = 1; k <= n; k <<= 1)
    {
        int p = 0;
        for(int i = 0; i < k; i++) y[++p] = n - i;
        for(int i = 1; i <= n; i++)
            if(sa[i] > k) y[++p] = sa[i] - k;
        for(int i = 1; i <= m; i++) c[i] = 0;
        for(int i = 1; i <= n; i++) c[x[y[i]]]++;
        for(int i = 1; i <= m; i++) c[i] += c[i - 1];
        for(int i = n; i >= 1; i--) sa[c[x[y[i]]]--] = y[i];
        std::swap(x, y), x[sa[p = 1]] = 1;
        for(int i = 2; i <= n; i++)
            x[sa[i]] = y[sa[i]] == y[sa[i - 1]] && sa[i] + k <= n && sa[i - 1] + k <= n && y[sa[i] + k] == y[sa[i - 1] + k] ? p : ++p;
        if(p == n) break;
        m = p;  
    }
}
void build_height()
{
    int k = 0;
    for(int i = 1; i <= n; i++) rank[sa[i]] = i;
    for(int i = 1; i <= n; i++)
    {
        if(k != 0) k--;
        if(rank[i] == 1) continue;
        int j = sa[rank[i] - 1];
        while(s[i + k] == s[j + k]) k++;
        height[rank[i]] = k;
    }
}
int main()
{
#ifndef ONLINE_JUDGE
    freopen("subst1.in","r",stdin);
    freopen("subst1.out","w",stdout);
#endif

    scanf("%s", s + 1);
    n = strlen(s + 1);
    build_sa(256);
    build_height();

    ans = (long long)n*(n + 1)>>1;
    for(int i = 1; i <= n; i++)
        ans -= height[i];
    write(ans);

#ifndef ONLINE_JUDGE
    fclose(stdin);
    fclose(stdout);
#endif
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值