SPOJ 705 SUBST1 New Distinct Substrings 后缀数组

题目大意:

给出一个字符串(长度 <= 50000), 求其不同的子串的数目


大致思路:

和SPOJ 694. Distinct Substrings 一样....买一送一....


代码如下:

Result  :  Accepted     Memory  :  4813 KB     Time  :  20 ms

/*
 * Author: Gatevin
 * Created Time:  2015/2/9 13:25:03
 * File Name: Iris_Freyja.cpp
 */
#include<iostream>
#include<sstream>
#include<fstream>
#include<vector>
#include<list>
#include<deque>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<bitset>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cctype>
#include<cmath>
#include<ctime>
#include<iomanip>
using namespace std;
const double eps(1e-8);
typedef long long lint;

#define maxn 52333

/*
 * Doubling Algorithm 求后缀数组
 */
int wa[maxn], wb[maxn], wv[maxn], Ws[maxn];

int cmp(int *r, int a, int b, int l)
{
    return r[a] == r[b] && r[a + l] == r[b + l];
}

void da(int *r, int *sa, int n, int m)
{
    int *x = wa, *y = wb, *t, i, j, p;
    for(int i = 0; i < m; i++) Ws[i] = 0;
    for(int i = 0; i < n; i++) Ws[x[i] = r[i]]++;
    for(int i = 1; i < m; i++) Ws[i] += Ws[i - 1];
    for(int i = n - 1; i >= 0; i--) sa[--Ws[x[i]]] = i;
    for(j = 1, p = 1; p < n; j *= 2, m = p)
    {
        for(p = 0, i = n - j; i < n; i++) y[p++] = i;
        for(i = 0; i < n; i++) if(sa[i] >= j) y[p++] = sa[i] - j;
        for(i = 0; i < n; i++) wv[i] = x[y[i]];
        for(i = 0; i < m; i++) Ws[i] = 0;
        for(i = 0; i < n; i++) Ws[wv[i]]++;
        for(i = 1; i < m; i++) Ws[i] += Ws[i - 1];
        for(i = n - 1; i >= 0; i--) sa[--Ws[wv[i]]] = y[i];
        for(t = x, x = y, y = t, p = 1, x[sa[0]] = 0, i = 1; i < n; i++)
            x[sa[i]] = cmp(y, sa[i - 1], sa[i], j) ? p - 1 : p++;
    }
    return;
}

int rank[maxn], height[maxn];
void calheight(int *r, int *sa, int n)
{
    int i, j, k = 0;
    for(i = 1; i <= n; i++) rank[sa[i]] = i;
    for(i = 0; i < n; height[rank[i++]] = k)
        for(k ? k-- : 0, j = sa[rank[i] - 1]; r[i + k] == r[j + k]; k++);
    return;
}

char in[maxn];
int s[maxn];
int sa[maxn];

int main()
{
    int t;
    scanf("%d", &t);
    while(t-->0)
    {
        scanf("%s", in);
        int n = strlen(in);
        for(int i = 0; i < n; i++)
            s[i] = in[i];
        s[n] = 0;
        da(s, sa, n + 1, 130);
        calheight(s, sa, n);
        lint ans = (lint)n*((lint)n + 1LL)/2LL;//所有的子串数C[n][1] + C[n][2]
        /*
         * 利用height数组的性质
         * 由于每个子串我们用从位置j开始且长度为k来表示不同的子串的话
         * 则一个height[i]值表示
         * 从位置sa[i - 1]开始的和sa[i]开始的长度为k <= height[i]的子串相同
         * 所以将总数减去这个值即可
         * 遍历一遍height数组去重就可以得到答案
         */
        for(int i = 1; i <= n; i++)
            ans -= height[i];
        printf("%lld\n", ans);
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值