後綴數組求不同的子串數。。
每一個子串都是某個後綴的前綴,每加入一個後綴都會增加 n-sa[ i ] 個子串,但是有h[ i ]個子串會是重復的,所以對每增加的一個後綴會產生 n-sa[ i ]-h[ i ]個不同的子串
Distinct Substrings Time Limit: 1000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu[Submit] [Go Back] [Status]
Description
Given a string, we need to find the total number of its distinct substrings.
T- number of test cases. T<=20;
Each test case consists of one string, whose length is <= 1000
For each test case output one number saying the number of distinct substrings.
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.
Source
ByteCode '06[Submit] [Go Back] [Status]
#include#include #include #include using namespace std; const int maxn=10000; int sa[maxn],rank[maxn],rank2[maxn],h[maxn],c[maxn],*x,*y,ans[maxn],n; char str[maxn]; bool cmp(int* r,int a,int b,int l,int n) { if(r[a]==r[b]&&a+l =0;i--) sa[--c[x[y[i]]]]=y[i]; } void get_sa(char c[],int n,int sz=128) { x=rank,y=rank2; for(int i=0;i =len) y[yid++]=sa[i]-len; radix_sort(n,sz); swap(x,y); x[sa[0]]=yid=0; for(int i=1;i =n) break; } for(int i=0;i