distinct substrings

12 篇文章 0 订阅
11 篇文章 0 订阅

//本质不同的子串个数



#include <bits/stdc++.h>

using namespace std;

typedef long long LL;

typedef int lint;

const lint maxn = 1000 + 10;

typedef int lint;

struct suffix{



    lint c[maxn],sa[maxn],t1[maxn],t2[maxn],m,n,h[maxn],height[maxn],rk[maxn],s[maxn];

    void Build_SA(lint len){

        m = 0;

        lint* x = t1,*y = t2;

        n = len;

        for( lint i = 0;i < n;i++ ) m = max( m,s[i] );

        for( lint i = 0;i <= m;i++ ) c[i] = 0;

        for( lint i = 0;i < n;i++ ) c[ x[i] = s[i] ]++;

        for( lint i = 1;i <= m;i++ ) c[i] += c[i-1];

        for( lint i = n-1;i >= 0;i-- ) {

            sa[ --c[ x[i] ] ] = i;

        }

        for( lint k = 1;k < n;k <<= 1 ){

            lint cnt = 0;

            for( lint i = n - 1;i >= n-k;i-- ) y[cnt++] = i;

            for( lint i = 0;i < n;i++ ) if( sa[i] >= k ) y[cnt++] = sa[i]-k;

            for( lint i = 0;i <= m;i++ ) c[i] = 0;

            for( lint i = 0;i < n;i++ ) c[ x[i] ]++;

            for( lint i = 1;i <= m;i++ ) c[i] += c[i-1];

            for( lint i = cnt-1;i >= 0;i-- ) sa[ --c[ x[ y[i] ] ] ] = y[i];

            swap( x,y );

            lint num = 0;

            x[ sa[0] ] = 0;

            for( lint i = 1;i < n;i++ ){

                if( y[ sa[i-1] ] != y[ sa[i] ] || y[ sa[i-1]+k ] != y[ sa[i]+k ] ){

                    x[ sa[i] ] = ++num;

                }else{

                    x[ sa[i] ] = num;

                }

            }

            if( num == n-1 ) return;

            m = num;

        }

    }

    void getheight(){

        for( lint i = 0;i < n;i++ ){

            rk[ sa[i] ] = i;

        }

        lint cnt = 1;

        h[ sa[0] ] = 0;

        height[ 0 ] = 0;

        for( lint i = 0;i < n;i++ ){

            if(cnt)cnt--;

            while(  rk[i] >= 1 &&  sa[rk[i]-1]+cnt  < n && i+cnt < n && s[ sa[rk[i]-1]+cnt ] == s[ i+cnt ] ) cnt++;

            h[ i ] = cnt;

            height[ rk[i] ] = cnt;

        }

        //cout << "debug" << endl;

    }



}g;

char s[maxn];

lint solve(){

    lint n = strlen( s );

    lint re = 0;

    for( lint x = 0;x < n;x++ ){

        lint i = g.sa[x];

        lint len = n - i;

        lint t = g.h[i];

        re += len - g.height[i];

    }

    return re;

}

int main()

{

    lint T;

    scanf("%d",&T);

    while( T-- ){

        scanf("%s",s);

        lint len = strlen(s);

        for( lint i = 0;i < len;i++ ) g.s[i] = s[i] ;

        g.Build_SA(len);

        g.getheight();

        lint ans = solve();

        printf("%d\n",ans);

    }

}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值