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 all 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.
Test instructions
Find out the different substrings in the uppercase string. Dictation the suffix automaton. Today is mainly a practice suffix array.
Attention:
- Note whether it is uppercase or lowercase;
- Note that Init initialization does not have a one-time memset, so the following memset each new point. Don't forget.
Suffix automata:
#include <cstdio>#include<cstdlib>#include<iostream>#include<cstring>#include<algorithm>using namespacestd;Const intmaxn=10000;structsam{intch[maxn][ -],fa[maxn],maxlen[maxn],last,sz; voidinit () {sz=last=1; fa[1]=maxlen[1]=0; memset (ch[1],0,sizeof(ch[1])); } voidAddintx) {intNp=++sz,p=last; last=NP; memset (CH[NP],0,sizeof(CH[NP])); MAXLEN[NP]=maxlen[p]+1; while(P&&!ch[p][x]) ch[p][x]=np,p=Fa[p]; if(!p) fa[np]=1; Else { intq=Ch[p][x]; if(maxlen[p]+1==MAXLEN[Q]) fa[np]=Q; Else { intnq=++sz; memcpy (Ch[nq],ch[q],sizeof(Ch[q])); MAXLEN[NQ]=maxlen[p]+1; FA[NQ]=Fa[q]; FA[Q]=fa[np]=NQ; while(p&&ch[p][x]==q) ch[p][x]=nq,p=Fa[p]; } } }}; Sam Sam;intMain () {CharCHR[MAXN]; intt,ans,i,l; scanf ("%d",&T); while(t--) {sam.init (); Ans=0; scanf ("%s", CHR); L=strlen (CHR); for(i=0; i<l;i++) Sam.add (chr[i]-'A'); for(i=1; i<=sam.sz;i++) ans+=sam.maxlen[i]-Sam.maxlen[sam.fa[i]]; printf ("%d\n", ans); } return 0;}
View CodeSuffix array:
#include <cstdio>#include<cstdlib>#include<cstring>#include<iostream>#include<algorithm>using namespacestd;Const intmaxn=10000;CharCH[MAXN];intL;structsa{intCNTA[MAXN],CNTB[MAXN],A[MAXN],B[MAXN]; intRANK[MAXN],SA[MAXN],TSA[MAXN],HT[MAXN];voidsort () { for(inti =0; I < the; i + +) cnta[i] =0; for(inti =1; I <= L; i + +) Cnta[ch[i]] + +; for(inti =1; I < the; i + +) Cnta[i] + = cnta[i-1]; for(inti = L; I I--) sa[cnta[ch[i]]---] =i; rank[sa[1]] =1; for(inti =2; I <= L; i + +) {Rank[sa[i]]= Rank[sa[i-1]]; if(Ch[sa[i]]! = ch[sa[i-1]]) Rank[sa[i]] + +; } for(intL =1; Rank[sa[l]] < L; L <<=1){ for(inti =0; I <= L; i + +) cnta[i] =0; for(inti =0; I <= L; i + +) cntb[i] =0; for(inti =1; I <= L; i + +) {Cnta[a[i]= Rank[i]] + +; Cntb[b[i]= (i + L <= L)? Rank[i + L]:0] ++; } for(inti =1; I <= L; i + +) Cntb[i] + = cntb[i-1]; for(inti = L; I I--) tsa[cntb[b[i]]---] =i; for(inti =1; I <= L; i + +) Cnta[i] + = cnta[i-1]; for(inti = L; I I--) sa[cnta[a[tsa[i] []---] =Tsa[i]; rank[sa[1]] =1; for(inti =2; I <= L; i + +) {Rank[sa[i]]= Rank[sa[i-1]]; if(A[sa[i]]! = a[sa[i-1]] || B[sa[i]]! = b[sa[i-1]]) Rank[sa[i]] + +; } } } voidgetheight () { for(inti =1, j =0; I <= L; i + +){ if(j) J--; while(Ch[i + j] = = Ch[sa[rank[i]-1] + j]) J + +; Ht[rank[i]]=J; }}};sa SA;intMain () {intT,ans,i; scanf ("%d",&T); while(t--) {ans=0; scanf ("%s", ch+1); L=strlen (ch+1); Sa.sort (); Sa.getheight (); for(i=1; i<=l;i++) ans+=l-sa.sa[i]+1-Sa.ht[i]; printf ("%d\n", ans); } return 0;}
SPOJ705 Distinct substrings (suffix automaton & suffix array)