705. New Distinct Substrings

Problem code: SUBST1


 

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 <= 50000

Output

For each test case output one number saying the number of distinct substrings.

Example

Input:
2
CCCCC
ABABA

Output:
5
9

Added by: Hoang Hong Quan
Date: 2006-01-18
Time limit: 2s
Source limit: 50000B
Memory limit: 256MB
Cluster: Pyramid (Intel Pentium III 733 MHz)
Languages: All except: NODEJS PERL 6
Resource: Base on a problem in ByteCode06

 

 
 
 
 
 
 
 
 
 1 #include <iostream>
 2 #include <stdio.h>
 3 #include <string.h>
 4 using namespace std;
 5 #define N 51000
 6 char a[N];
 7 int c[N],d[N],e[N],sa[N],height[N],n,b[N],m;
 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 void da()
13 {
14     int i,j,p,*x=c,*y=d,*t;
15     for(i=0;i<m;i++)b[i]=0;
16     for(i=0; i<n; i++)b[x[i]=a[i]]++;
17     for(i=1; i<m; i++)b[i]+=b[i-1];
18     for(i=n-1; i>=0; i--)sa[--b[x[i]]]=i;
19     for(j=1,p=1; p<n; j*=2,m=p)
20     {
21         for(p=0,i=n-j; i<n; i++)y[p++]=i;
22         for(i=0; i<n; i++)if(sa[i]>=j)y[p++]=sa[i]-j;
23         for(i=0; i<n; i++)e[i]=x[y[i]];
24         for(i=0;i<m;i++)b[i]=0;
25         for(i=0; i<n; i++)b[e[i]]++;
26         for(i=1; i<m; i++)b[i]+=b[i-1];
27         for(i=n-1; i>=0; i--)sa[--b[e[i]]]=y[i];
28         for(t=x,x=y,y=t,p=1,x[sa[0]]=0,i=1; i<n; i++)
29             x[sa[i]]=cmp(y,sa[i-1],sa[i],j)?p-1:p++;
30     }
31 }
32 void callheight()
33 {
34     int i,j,k=0;
35     for(i=1; i<n; i++)b[sa[i]]=i;
36     n--;
37     for(i=0; i<n; height[b[i++]]=k)
38         for(k?k--:0,j=sa[b[i]-1]; a[i+k]==a[j+k]; k++);
39 }
40 
41 int main()
42 {
43     int t,i;
44     cin>>t;
45     for(i=0;i<t;i++)
46     {
47         scanf("%s",a);
48         n=strlen(a);
49         n++;
50         m=200;
51         da();
52         callheight();
53         int sum=0;
54         for(int j=1;j<=n;j++)sum+=n-sa[j]-height[j];
55         cout<<sum<<endl;
56     }
57 }
View Code

 

posted on 2014-03-11 23:55  ERKE  阅读(239)  评论(0编辑  收藏  举报