现在的位置: 首页 > 综合 > 正文

求字符串中不重复的子串数

2018年02月21日 ⁄ 综合 ⁄ 共 2540字 ⁄ 字号 评论关闭

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.


   

  1. #include<iostream>  
  2. #include<cstdio>  
  3. #include<cstring>  
  4. using namespace std;  
  5. const int maxn=1010;  
  6. char str[maxn];  
  7. int wa[maxn],wb[maxn],wv[maxn],wn[maxn],a[maxn],sa[maxn];  
  8. int cmp(int* r,int a,int b,int l)  
  9. {  
  10.     return r[a]==r[b]&&r[a+l]==r[b+l];  
  11. }  
  12. //n为字符串长度,m为字符的取值范围,r为字符串。后面的j为每次排序时子串的长度  
  13. void DA(int* r,int* sa,int n,int m)  
  14. {  
  15.     int i,j,p,*x=wa,*y=wb,*t;  
  16.     ///对R中长度为1的子串进行基数排序  
  17.     for(i=0; i<m; i++)wn[i]=0;  
  18.     for(i=0; i<n; i++)wn[x[i]=r[i]]++;  
  19.     for(i=1; i<m; i++)wn[i]+=wn[i-1];  
  20.     for(i=n-1; i>=0; i--)sa[--wn[x[i]]]=i;  
  21.     for(j=1,p=1; p<n; j*=2,m=p)  
  22.     {  
  23.         //利用了上一次基数排序的结果,对待排序的子串的第二关键字进行了一次高效地基数排序  
  24.         for(p=0,i=n-j; i<n; i++)y[p++]=i;  
  25.         for(i=0; i<n; i++)if(sa[i]>=j)y[p++]=sa[i]-j;  
  26.         ///基数排序  
  27.         for(i=0; i<n; i++)wv[i]=x[y[i]];  
  28.         for(i=0; i<m; i++)wn[i]=0;  
  29.         for(i=0; i<n; i++)wn[wv[i]]++;  
  30.         for(i=1; i<m; i++)wn[i]+=wn[i-1];  
  31.         for(i=n-1; i>=0; i--)sa[--wn[wv[i]]]=y[i];  
  32.         ///当p=n的时候,说明所有串都已经排好序了  
  33.         ///在第一次排序以后,rank数组中的最大值小于p,所以让m=p  
  34.         for(t=x,x=y,y=t,p=1,x[sa[0]]=0,i=1; i<n; i++)  
  35.             x[sa[i]]=cmp(y,sa[i-1],sa[i],j)?p-1:p++;  
  36.     }  
  37.     return;  
  38. }  
  39. ///后缀数组  计算height数组  
  40. /** 
  41. height数组的值应该是从height[1]开始的,而且height[1]应该是等于0的。 
  42. 原因是,+因为我们在字符串后面添加了一个0号字符,所以它必然是最小的 
  43. 一个后缀。而字符串中的其他字符都应该是大于0的(前面有提到,使用倍 
  44. 算法前需要确保这点),所以排名第二的字符串和0号字符的公共前缀 
  45. (即height[1])应当为0.在调用calheight函数时,要注意height数组的范 
  46. 围应该是[1..n]。所以调用时应该是calheight(r,sa,n) 
  47. 而不是calheight(r,sa,n+1)。*/  
  48. int rank[maxn],height[maxn];  
  49. void calheight(int* r,int* sa,int n)  
  50. {  
  51.     int i,j,k=0;  
  52.     for(i=1; i<=n; i++)rank[sa[i]]=i;  
  53.     for(i=0; i<n; height[rank[i++]]=k)  
  54.         for(k?k--:0,j=sa[rank[i]-1]; r[i+k]==r[j+k]; k++);  
  55.     return;  
  56. }  
  57. int main()  
  58. {  
  59.     int t;  
  60.     scanf("%d",&t);  
  61.     while(t--)  
  62.     {  
  63.         char str[1010];  
  64.         scanf("%s",str);  
  65.         int n=strlen(str);  
  66.         for(int i=0;i<n;i++) a[i]=(int)str[i];  
  67.         a[n]=0;  
  68.         DA(a,sa,n+1,260);  
  69.         calheight(a,sa,n);  
  70.         int sum=0;  
  71.         for(int i=1;i<=n;i++)  
  72.         sum+=n-sa[i]-height[i];  
  73.         printf("%d/n",sum);  
  74.     }  
  75.     return 0;  
  76. }  

抱歉!评论已关闭.