后缀数组---New Distinct Substrings

时间:2021-05-15 04:45:51

Description

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.

Example

Input:
2
CCCCC
ABABA Output:
5
9 题意:给一个字符串长小于50000,求这个字符串的不同子串的个数; 思路:使用后缀数组算法先求出sa[],sa[i]表示排第i的后缀的开始位置下标,然后求出height[]数组,height[i]表示排名第i的后缀和排名第i-1的后缀的最大公共前缀的长度,容易知道排名i和i-1的串的子串重复个数为height[i]个,而原始串的子串个数为sum=n*(n+1)/2,故用sum减去所有的henght[]即为结果;
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#define rep(i,n) for(int i = 0;i < n; i++)
using namespace std;
const int size=,INF=<<;
int rk[size],sa[size],height[size],w[size],wa[size],res[size];
void getSa (int len,int up) {
int *k = rk,*id = height,*r = res, *cnt = wa;
rep(i,up) cnt[i] = ;
rep(i,len) cnt[k[i] = w[i]]++;
rep(i,up) cnt[i+] += cnt[i];
for(int i = len - ; i >= ; i--) {
sa[--cnt[k[i]]] = i;
}
int d = ,p = ;
while(p < len){
for(int i = len - d; i < len; i++) id[p++] = i;
rep(i,len) if(sa[i] >= d) id[p++] = sa[i] - d;
rep(i,len) r[i] = k[id[i]];
rep(i,up) cnt[i] = ;
rep(i,len) cnt[r[i]]++;
rep(i,up) cnt[i+] += cnt[i];
for(int i = len - ; i >= ; i--) {
sa[--cnt[r[i]]] = id[i];
}
swap(k,r);
p = ;
k[sa[]] = p++;
rep(i,len-) {
if(sa[i]+d < len && sa[i+]+d <len &&r[sa[i]] == r[sa[i+]]&& r[sa[i]+d] == r[sa[i+]+d])
k[sa[i+]] = p - ;
else k[sa[i+]] = p++;
}
if(p >= len) return ;
d *= ,up = p, p = ;
}
} void getHeight(int len) {
rep(i,len) rk[sa[i]] = i;
height[] = ;
for(int i = ,p = ; i < len - ; i++) {
int j = sa[rk[i]-];
while(i+p < len&& j+p < len&& w[i+p] == w[j+p]) {
p++;
}
height[rk[i]] = p;
p = max(,p - );
}
} int getSuffix(char s[]) {
int len = strlen(s),up = ;
for(int i = ; i < len; i++) {
w[i] = s[i];
up = max(up,w[i]);
}
w[len++] = ;
getSa(len,up+);
getHeight(len);
return len;
} int main()
{
int T;
long long sum;
char s[size];
scanf("%d",&T);
while(T--)
{
scanf("%s",s);
sum=;
getSuffix(s);
long long len=strlen(s);
for(int i=;i<=len;i++)
sum+=height[i];
sum=len*(len+)/-sum;
printf("%lld\n",sum);
}
}