SPOJ - SUBST1 New Distinct Substrings【后缀数组】【本质不同的子串数】

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/niiick/article/details/84758099

Time limit 280 ms
Memory limit1572864 kB

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 <= 50000

Output

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


题目分析

a n s = i = 1 n ( n s a [ i ] + 1 h e i g h t [ i ] ) ans=\sum_{i=1}^n(n-sa[i]+1-height[i]) (默认字符串从下标1开始储存)

一个字符串的所有子串可以表示为 其所有后缀的所有前缀
对于排名为 i i 的后缀,他有 n s a [ i ] + 1 n-sa[i]+1 个前缀
其中有 h e i g h t [ i ] height[i] 个前缀排名为 i 1 i-1 的后缀包含的前缀相同,故减去


#include<iostream>
#include<cmath>
#include<algorithm>
#include<queue>
#include<cstring>
#include<cstdio>
#include<map>
using namespace std;
typedef long long lt;

int read()
{
    int x=0,f=1;
    char ss=getchar();
    while(ss<'0'||ss>'9'){if(ss=='-')f=-1;ss=getchar();}
    while(ss>='0'&&ss<='9'){x=x*10+ss-'0';ss=getchar();}
    return x*f;
} 

const int maxn=100010;
int T;
int n,m,a[maxn];
int rak[maxn],sa[maxn],tp[maxn];
int tax[maxn],height[maxn];
char ss[maxn];

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 ssort()
{
    m=1024;
    for(int i=1;i<=n;++i)
    rak[i]=a[i],tp[i]=i;
    
    rsort();
    for(int k=1;k<=n;k<<=1)
    {
        int p=0;
        for(int i=n-k+1;i<=n;++i) tp[++p]=i;
        for(int i=1;i<=n;++i) if(sa[i]>k) tp[++p]=sa[i]-k;
        
        rsort();
        swap(rak,tp);//这里如果spoj评测CE就改成memcpy
        rak[sa[1]]=p=1;
        for(int i=2;i<=n;++i)
        rak[sa[i]]=(tp[sa[i]]==tp[sa[i-1]]&&tp[sa[i]+k]==tp[sa[i-1]+k])?p:++p;
        if(p>=n) break;
        m=p;
    }
}

void getH()
{
    int k=0;
    for(int i=1;i<=n;++i)
    {
        if(k) k--;
        int j=sa[rak[i]-1];
        while(a[i+k]==a[j+k]) k++;
        height[rak[i]]=k;
    }
}

int main()
{
    T=read();
    while(T--)
    {
    	lt ans=0;
        scanf("%s",&ss); n=strlen(ss);
    	for(int i=0;i<n;++i) a[i+1]=ss[i];
    	
        ssort(); getH();
    	
        for(int i=1;i<=n;++i)
    	ans+=n-sa[i]+1-height[i];
    	printf("%lld\n",ans);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/niiick/article/details/84758099