SPOJ - DISUBSTR 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.

  • 题意:求不同字串的数量
  • 思路:长度为n的字符串有n*(n+1)/2个子串,再减去相同的子串就行了
    对于子串,它肯定是一个后缀的前缀,如果height[i]==k,说明后缀i-1和后缀i有k个子串相同,这样减去它即可,即减去height数组的后n-1个即可
    参考题解
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 1e4+5;
char str[maxn];
int _rank[maxn], sa[maxn], height[maxn], _temp[maxn], bucket[maxn];
int n, m;   //m是自己定的,其实就是字母字符所占ascii码最大值,即桶的个数

void RadixSort()
{
    for(int i = 0; i <= m; i++) bucket[i] = 0;  //clear the buckets
    for(int i = 1; i <= n; i++) bucket[_rank[_temp[i]]]++;  //statistics the number of each position's rank
    for(int i = 1; i <= m; i++) bucket[i] += bucket[i-1];   //solve the sum of prefixes
    for(int i = n; i >= 1; i--) sa[bucket[_rank[_temp[i]]]--] = _temp[i];
}

void GetSa()
{
    for(int i = 1; i <= n; i++) _rank[i] = str[i], _temp[i] = i;
    RadixSort();
    for(int _size = 1; _size <= n; _size <<= 1)
    {
        int num = 0;
        for(int i = n-_size+1; i <= n; i++) _temp[++num] = i;
        for(int i = 1; i <= n; i++)    if(sa[i] > _size)   _temp[++num] = sa[i]-_size;
        RadixSort();    swap(_rank, _temp);
        _rank[sa[1]] = num = 1;
        for(int i = 2; i <= n; i++)
            _rank[sa[i]] = (_temp[sa[i]] == _temp[sa[i-1]] && _temp[sa[i]+_size] == _temp[sa[i-1]+_size]) ? num : ++num;
        if(num == n)    break ;
        m = n;
    }
}

void GetHeight()
{
    for(int i = 1; i <= n; i++) _rank[sa[i]] = i;
    int prefix_len = 0;
    for(int i = 1; i <= n; i++)
    {
        if(_rank[i] == 1)   continue ;
        if(prefix_len)  --prefix_len;
        int pre_pos = sa[_rank[i]-1];
        while(i+prefix_len <= n && pre_pos+prefix_len <= n && str[i+prefix_len] == str[pre_pos+prefix_len]) ++prefix_len;
        height[_rank[i]] = prefix_len;
    }
}

int main()
{
	int T;
	cin >> T;
	while(T--)
	{
		scanf("%s", str+1);
		n = strlen(str+1);
		m = 'z';
		GetSa();
		GetHeight();
		int sum = ((n+1)*n)>>1;
		for(int i = 1; i <= n; i++)	sum -= height[i];
		printf("%d\n", sum);
	}
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值