SPOJ 694 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.
Sample Input:
2
CCCCC
ABABA
Sample Output:
5
9
Hint
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.
题意
给出一个字符串,输出其不同子串的个数。
解题思路
如果直接枚举用set一个一个存下它的子串极有可能会超时,所以这里我们用后缀数组来求解。
后缀数组有一条性质——对于原字符串的任意子串,该子串一定是某一个后缀的前缀,而height数组存下了相邻(按字典序排列过)两个后缀的LCP,就是两个后缀重复的子串,则不重复的子串的个数即为L[i]+L[i+1]-LCP[i+1],其中L为后缀的长度,通过这个式子可以扩展到全部后缀。最后可以得出公式ans=(len+1)*len/2- height[i],即为解。
代码
#include <cstdio>
#include <iostream>
#include <cstring>
#define LL long long
#define ULL unsigned long long
using namespace std;
const int maxn= 1e3+50;
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(char r[],int sa[],int n,int m)
{
int i,j,p,*x=wa,*y=wb,*t;
for(i=0; i<m; i++) Ws[i]=0;
for(i=0; i<n; i++) Ws[x[i]=r[i]]++;
for(i=1; i<m; i++) Ws[i]+=Ws[i-1];
for(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 sa[maxn],rankk[maxn],height[maxn],n;
void getheight(char *r,int *sa,int n)
{
int i,j,k=0;
for(i=1; i<=n; i++) rankk[sa[i]]=i;
for(i=0; i<n; height[rankk[i++]]=k)
for(k?k--:0,j=sa[rankk[i]-1]; r[i+k]==r[j+k]; k++);
for(int i=n; i>=1; --i) ++sa[i],rankk[i]=rankk[i-1];
}
char arr[maxn];
int main()
{
#ifdef DEBUG
freopen("in.txt","r",stdin);
#endif // DEBUG
int t;
scanf("%d",&t);
getchar();
while(t--)
{
scanf("%s",arr);
int len=strlen(arr);
da(arr,sa,len+1,1000);
getheight(arr,sa,len);
long long ans=(len+1)*len/2;
for(int i=1; i<=len-1; i++)
ans-=height[i+1];
printf("%lld\n",ans);
}
return 0;
}