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.
题目链接:https://vjudge.net/problem/SPOJ-DISUBSTR
题目大意:给一字符串s,问它有多少个不同的子串
思路:每一个子串一定是一个后缀的前缀。总共有 (n+1)*n / 2个子串,去掉重复的 h[i] 就是答案。后缀数组模板 倍增法 O(nlogn)
代码:
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define ll long long
const int N=2005;
int sa[N],rak[N],h[N],tax[N],tp[N],n,m;
char s[N];
bool cmp(int *f,int x,int y,int w){return f[x]==f[y]&&f[x+w]==f[y+w]; }
void Rsort()
{
for(int i=0;i<=m;i++) tax[i]=0;
for(int i=1;i<=n;i++) tax[rak[i]]++;
for(int i=1;i<=m;i++) tax[i]+=tax[i-1];
for(int i=n;i>=1;i--) sa[tax[rak[tp[i]]]--]=tp[i];
}
void suffix()
{
for(int i=1;i<=n;i++) rak[i]=s[i],tp[i]=i;
m=127,Rsort();
for(int w=1,p=0,i;p<n;w+=w,m=p)
{
for(p=0,i=n-w+1;i<=n;i++) tp[++p]=i;
for(i=1;i<=n;i++) if(sa[i]>w)tp[++p]=sa[i]-w;
Rsort();
swap(rak,tp); rak[sa[1]]=p=1;
for(i=2;i<=n;i++) rak[sa[i]]=cmp(tp,sa[i],sa[i-1],w)?p:++p;
}
int j,k=0;
for(int i=1;i<=n;h[rak[i++]]=k)
for(k=k?k-1:k,j=sa[rak[i]-1];s[i+k]==s[j+k];++k);
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%s",s+1);
n=strlen(s+1);
suffix();
int ans=n*(n+1)/2;
for(int i=2;i<=n;i++)
ans-=h[i];
printf("%d\n",ans);
}
return 0;
}