SPOJ Problem Set (classical)694. Distinct SubstringsProblem code: DISUBSTR |
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.
Added by: | Prasanna |
Date: | 2006-01-13 |
Time limit: | 1s |
Source limit: | 50000B |
Memory limit: | 256MB |
Cluster: | Pyramid (Intel Pentium III 733 MHz) |
Languages: | All except: NODEJS PERL 6 |
Resource: | ByteCode '06 |
题意:给定一个字符串,求不相同的子串的个数。
思路: 每个子串一定是某个后缀的前缀,那么原问题等价于求所有后缀之前的不相同的前缀的个数。如果所有的后缀按照suffix(sa[1]),suffix(sa[2]),...suffix(sa[n])的顺序计算,不难发现,对于每次新加的后缀suffix(sa[k]),若n为不包含0的字符串的长度,则产生n-sa[k]个新的前缀,但其中height[k]和前面的字符串的前缀是相同的,锁以suffix(sa[k])将贡献n-sa[k]-height[k]个不同的子串,累加后是原问题的答案。时间复杂度为O(n)。详见代码:
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int MAXN=1000+100;
int n;
char s[MAXN];
int t1[MAXN],t2[MAXN],c[MAXN],rank[MAXN],height[MAXN],sa[MAXN];
int cmp(int *r,int a,int b,int l)
{
return r[a]==r[b] && r[a+l]==r[b+l];
}
void build_sa(int m)
{
int i,k,p=0;
int *x=t1,*y=t2;
for(i=0;i<m;i++) c[i]=0;
for(i=0;i<n;i++) c[x[i]=s[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(k=1;p<n;k<<=1,m=p)
{
for(p=0,i=n-k;i<n;i++) y[p++]=i;
for(i=0;i<n;i++) if(sa[i]>=k) y[p++]=sa[i]-k;
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);
for(p=1,x[sa[0]]=0,i=1;i<n;i++)
x[sa[i]]=cmp(y,sa[i-1],sa[i],k)? p-1:p++;
}
}
void calheight(int n)
{
int k=0;
for(int i=1;i<=n;i++) rank[sa[i]]=i;
for(int i=0;i<n;i++)
{
if(k) k--;
int j=sa[rank[i]-1];
while(s[i+k]==s[j+k]) k++;
height[rank[i]]=k;
}
}
int main()
{
freopen("text.txt","r",stdin);
int T;
scanf("%d",&T);
while(T--)
{
scanf("%s",s);
n=strlen(s);
s[n++]=0;
build_sa(200);
calheight(n-1);
int len=n-1;
int ans=len-sa[1];
for(int i=2;i<n;i++)
ans+=len-sa[i]-height[i];
printf("%d\n",ans);
}
return 0;
}