Spoj-disubstr Distinct substrings (number of different substrings)

Source: Internet
Author: User

Distinct substrings

Time Limit: 159MS Memory Limit: 1572864KB 64bit IO Format: %lld &%llu

Description

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.

/** Spoj-disubstr Distinct substrings * To find the number of different substrings * * The string with the length n is * * (n+1)/2 substrings, minus the same substring. * For substrings, it is definitely a suffix prefix if height[i ]==k, the suffix i-1 and suffix I have the same k substring, * so minus it, minus the height array after n-1*/#include<bits/stdc++.h>using namespacestd;Const intMAXN = ++ -;intSA[MAXN];intT1[MAXN],T2[MAXN],C[MAXN];intRANK[MAXN],HEIGHT[MAXN];voidBuild_sa (intS[],intNintm) {    inti,j,p,*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(j=1; j<=n;j<<=1) {p=0;  for(i=n-j;i<n;i++) y[p++]=i;  for(i=0; i<n;i++)if(SA[I]&GT;=J) y[p++]=sa[i]-J;  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); P=1; x[sa[0]]=0;  for(i=1; i<n;i++) X[sa[i]]=y[sa[i-1]]==y[sa[i]] && y[sa[i-1]+j]==y[sa[i]+j]?p-1:p + +; if(p>=n) Break; M=p; }}voidGetHeight (intS[],intN) {    inti,j,k=0;  for(i=0; i<=n;i++) rank[sa[i]]=i;  for(i=0; i<n;i++)    {        if(k) k--; J=sa[rank[i]-1];  while(S[i+k]==s[j+k]) k++; Height[rank[i]]=K; }}CharSTR[MAXN];intSS[MAXN];intMain () {intT; scanf ("%d",&T);  while(t--) {scanf ("%s", str); intlen=strlen (str);  for(intI=0; i<len;i++) ss[i]=Str[i]; Ss[len]=0; Build_sa (Ss,len+1, -);        GetHeight (Ss,len); intans=len* (len+1)/2;  for(intI=2; i<=len;i++) ans-=Height[i]; printf ("%d\n", ans); }    return 0;}

Spoj-disubstr Distinct substrings (number of different substrings)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.