SPOJ - DISUBSTR Distinct Substrings (后缀数组 不同子串个数)

Distinct Substrings

 

Given a string, we need to find the total number of its distinct substrings.

Input

T- number of test cases. T<=20;
Each test case consists of one string, whose length is <= 1000

Output

For each test case output one number saying the number of distinct substrings.

Example

Sample Input:
2
CCCCC
ABABA

Sample Output:
5
9

Explanation for the testcase with string ABABA: 
len=1 : A,B
len=2 : AB,BA
len=3 : ABA,BAB
len=4 : ABAB,BABA
len=5 : ABABA
Thus, total number of distinct substrings is 9.

题目链接:https://vjudge.net/problem/SPOJ-DISUBSTR

题目大意:给一字符串s,问它有多少个不同的子串

思路:每一个子串一定是一个后缀的前缀。总共有 (n+1)*n / 2个子串,去掉重复的 h[i] 就是答案。后缀数组模板 倍增法 O(nlogn)

后缀数组详解

代码:

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define ll long long
const int N=2005;
int sa[N],rak[N],h[N],tax[N],tp[N],n,m;
char s[N];
bool cmp(int *f,int x,int y,int w){return f[x]==f[y]&&f[x+w]==f[y+w]; }
void Rsort()
{
    for(int i=0;i<=m;i++) tax[i]=0;
    for(int i=1;i<=n;i++) tax[rak[i]]++;
    for(int i=1;i<=m;i++) tax[i]+=tax[i-1];
    for(int i=n;i>=1;i--) sa[tax[rak[tp[i]]]--]=tp[i];
}
void suffix()
{
    for(int i=1;i<=n;i++) rak[i]=s[i],tp[i]=i;
    m=127,Rsort();
    for(int w=1,p=0,i;p<n;w+=w,m=p)
    {
        for(p=0,i=n-w+1;i<=n;i++) tp[++p]=i;
        for(i=1;i<=n;i++) if(sa[i]>w)tp[++p]=sa[i]-w;
        Rsort();
        swap(rak,tp); rak[sa[1]]=p=1;
        for(i=2;i<=n;i++) rak[sa[i]]=cmp(tp,sa[i],sa[i-1],w)?p:++p;
    }

    int j,k=0;
    for(int i=1;i<=n;h[rak[i++]]=k)
        for(k=k?k-1:k,j=sa[rak[i]-1];s[i+k]==s[j+k];++k);
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%s",s+1);
        n=strlen(s+1);
        suffix();
        int ans=n*(n+1)/2;
        for(int i=2;i<=n;i++)
            ans-=h[i];
        printf("%d\n",ans);
    }
    return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值