Spoj-DISUBSTR - Distinct Substrings (后缀数组)

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.

题意:求一个字符出串里面不同子串的个数;

思路:分析一下,一个字符出串一共有sum=(lenth+1)*length/2和字串,答案就是sum减去重复的子串的个数,二对于每个字串,一定是某个后缀的前缀,所以重复的子串的个数就是heigth数组的和。抛出后缀数组的heigth,sum减去heigth的和就行。

#include <cstdio>
#include <cstring>
#include <algorithm>
#include<iostream>
using namespace std;

const int MAXN = 20005;

int SA[MAXN], Rank[MAXN], Height[MAXN], tp[MAXN], a[MAXN], len, m, k;
int tax[MAXN];
char str[MAXN];
///Rank[i] 第i个后缀的排名; SA[i] 排名为i的后缀位置; Height[i] 排名为i的后缀与排名为(i-1)的后缀的LCP
///tax[i] 计数排序辅助数组; tp[i] Rank的辅助数组(计数排序中的第二关键字),与SA意义一样。
///a为原串
void RSort() {

	for (int i = 0; i <= m; i++) tax[i] = 0;
	for (int i = 1; i <= len; i++) tax[Rank[tp[i]]] ++;
	for (int i = 1; i <= m; i++) tax[i] += tax[i - 1];
	for (int i = len; i >= 1; i--) SA[tax[Rank[tp[i]]] --] = tp[i];
}

int cmp(int *f, int x, int y, int w) { return (f[x] == f[y]) && (f[x + w] == f[y + w]); }

void Suffix() {
	for (int i = 1; i <= len; i++) Rank[i] = a[i], tp[i] = i;
	m = 200, RSort();
	for (int w = 1, p = 1, i; p < len; w += w, m = p) {
		for (p = 0, i = len - w + 1; i <= len; i++) tp[++p] = i;
		for (i = 1; i <= len; i++) if (SA[i] > w) tp[++p] = SA[i] - w;
		RSort(), swap(Rank, tp), Rank[SA[1]] = p = 1;

		for (i = 2; i <= len; i++) Rank[SA[i]] = cmp(tp, SA[i], SA[i - 1], w) ? p : ++p;

	}
	for (int i = 1; i <= len; i++) Rank[SA[i]] = i;
	int j, k = 0;
	for (int i = 1; i <= len; Height[Rank[i++]] = k)
		for (k = k ? k - 1 : k, j = SA[Rank[i] - 1]; a[i + k] == a[j + k]; ++k);
}

void Init() {
	len = strlen(str);
	for (int i = 0; i < len; i++) a[i + 1] = str[i];
}
int main()
{
	int t;
	scanf("%d", &t);
	while (t--) {
		scanf("%s", str);
		Init();
		Suffix();
		int ans = len*(len + 1) / 2;
		for (int i = 2; i <= len; i++)
			ans -= Height[i];
		printf("%d\n", ans);
	}
	return 0;
}


猜你喜欢

转载自blog.csdn.net/qq_37171272/article/details/79859637