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.
后缀数组的简单运用,统计字符串中的所有不同子字符串个数。对于一个后缀sa[i],从他的第一个字符开始,后面有多长就有多少个子字符串,所有后缀的开头都不一样,所以所有后缀的子字符串个数之和就是总数。在height数组中,当前后缀和上一个后缀的共同前缀有多长,就有几个子字符串是重复的,所以公式就是len-sa[i]-height[i](sa从0开始)
#include <iostream>
#include <stdio.h>
#include <map>
#include <set>
#include <queue>
#include <algorithm>
#include <math.h>
#include <string.h>
using namespace std;
int mo[4][2]={0,1,1,0,0,-1,-1,0};
const int MAXN=0x3f3f3f3f;
const int sz=100005;
int n;
int t1[sz],t2[sz],c[sz],sa[sz],tsa[sz],rk[sz],height[sz],r[sz],RMQ[sz],mm[sz];
int best[20][sz];
char str[sz];
bool cmp(int *r,int a,int b,int l){
return r[a]==r[b]&&r[a+l]==r[b+l];
//hash,判断两个关键字是否完全一样
}
void da(int str[],int sa[],int rk[],int height[],int n,int m){
n++;
int i,j,p,*x=t1,*y=t2;
for(i=0;i<m;i++) c[i]=0;
for(i=0;i<n;i++) c[x[i]=str[i]]++;
for(i=1;i<m;i++) c[i]+=c[i-1];
//统计这个字符或数字包括他排在前面的一共有几个
for(i=n-1;i>=0;i--) sa[--c[x[i]]]=i;
//这个字符所在的排名
//第一次基数排序,求单独字符的排名
for(j=1;j<=n;j<<=1){//排序的子字符串长度以2^n速度增长,所以只要排序logn次
p=0;
for(i=n-j;i<n;i++) y[p++]=i;
//后面j个数没有第二关键字,可以理解为第二关键字为0,排名放在最前
for(i=0;i<n;i++) if(sa[i]>=j) y[p++]=sa[i]-j;
//sa[i]-j的第二关键字是j,记录了第二关键字排名
for(i=0;i<m;i++) c[i]=0;
for(i=0;i<n;i++) c[x[y[i]]]++;
//第二关键字排名由前到后的第一关键字的字符,进行统计
for(i=1;i<m;i++) c[i]+=c[i-1];
for(i=n-1;i>=0;i--) sa[--c[x[y[i]]]]=y[i];
//保证了取数时第二关键字靠后的依然排在后面
swap(x,y);
p=1;x[sa[0]]=0;
for(i=1;i<n;i++)
x[sa[i]]=cmp(y,sa[i-1],sa[i],j)?p-1:p++;
//这里的y是之前的x,记录数组编号对应的具体字符
//统计数字种类,缩小范围
if(p>=n) break;
//每个位置都单独对应了一个排名,可以不用排序了
m=p;
}
int k=0;
n--;
for(i=0;i<=n;i++) rk[sa[i]]=i;
for(i=0;i<n;i++){
if(k)k--;
j=sa[rk[i]-1];
while(str[i+k]==str[j+k])k++;
height[rk[i]]=k;
}
}
void initRMQ(int n){
mm[0]=-1;
int i,j;
for(i=1;i<=n;i++){
mm[i]=((i&(i-1))==0)?mm[i-1]+1:mm[i-1];
//当i是2^n时i&(i-1)为0,则此时查询的长度上限要增加一倍
}
for(i=1;i<=n;i++) best[0][i]=i;
for(i=1;i<=mm[n];i++){
for(j=1;j+(1<<i)-1<=n;j++){//j是起点,i是长度
int a=best[i-1][j];
int b=best[i-1][j+(1<<(i-1))];
if(RMQ[a]<RMQ[b]) best[i][j]=a;
else best[i][j]=b;
}
}
}
int askRMQ(int a,int b){
int t;
t=mm[b-a+1];
b-=(1<<t)-1;
a=best[t][a];b=best[t][b];
return RMQ[a]<RMQ[b]?a:b;
}
int lcp(int a,int b){
a=rk[a];b=rk[b];
if(a>b) swap(a,b);
return height[askRMQ(a+1,b)];
//a为什么+1?因为hight记录的是i和i-1之间的前缀长度,求a到b就要从a+1开始
}
int main()
{
int i,T;
//freopen("in.txt","r",stdin);
scanf("%d",&T);
while(T--){
scanf("%s",str);
int len=strlen(str);
n=len;
for(i=0;i<len;i++) r[i]=str[i];
r[len]=1;
r[n]=0;
da(r,sa,rk,height,n,128);
/*for(i=1;i<=n;i++) RMQ[i]=height[i];
initRMQ(n);*/
int ans=0;
/*for(i=1;i<=len;i++){
cout<<sa[i]<<' ';
}
cout<<endl;
for(i=1;i<=len;i++){
cout<<height[i]<<' ';
}
cout<<endl;
sa从0开始
rk从1开始
*/
for(i=1;i<=len;i++){
//printf("i:%d sa:%d h:%d\n",i,sa[i],height[i]);
ans+=len-sa[i]-height[i];
}
cout<<ans<<endl;
}
return 0;
}