Spoj 694 Distinct Substrings

Given a string, we need to find the total number of its distinct substrings.

To give you a character, statistics on the number of different substrings
the Input
T-Test Number of Cases T <= 20 is;.
Each Test Case One Consists of String, IS Whose length <= 1000
the Output
the For each Test Case Number One saying Output Number of substrings DISTINCT The.
the Sample the Input
2
CCCCC
ABABA
the Sample the Output
. 5
. 9
// The TestCase with Explanation for String ABABA:
len =. 1: A, B
len = 2: AB, BA
len =. 3: the ABA, BAB-
len =. 4: ABAB, BABA
len =. 5: ABABA
THUS, Total Number of IS DISTINCT substrings. 9.

 

Sol:

A prefix substring must be a suffix, then the original problem is equivalent to finding all suffixes no phase shift between the
number of the same prefix. If all Suffixes suffix (SA [. 1]), suffix (SA [2]),
suffix (SA [. 3]), ......, sequence suffix (sa [n]) is calculated, not difficult to find, for each added
incoming suffix suffix (sa [k]), it generates n-sa [k] +1 new prefix. But there are
height [k] is a character string and the prefix in front of the same. Therefore suffix (sa [k]) the "contribution" to
the n-sa [k] + [ k] different substrings 1- height. Is the answer to the original question of post-accumulation.

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#define maxn 2005
using namespace std;
 
char st[1001];
ca int n, years, everything tmp [maxn] rankk [maxn] sa [maxn] sum [maxn] hei [maxn];
 
int main () {
      scanf("%d",&ca);
      while (ca--){
                scanf("%s",st+1);
                n=strlen(st+1);
                memset(sum,0,sizeof(sum));
                for (int i=1;i<=n;i++) sum[tmp[i]=st[i]]++;
                for (int i=1;i<=256;i++) sum[i]+=sum[i-1];
                for (int i=n;i>=1;i--) sa[sum[tmp[i]]--]=i;
                tot = 0;
                rankk [to [1]] ++ = all;
                for (int i=2;i<=n;i++){
                       if (tmp [the [i]] = tmp [the [i-1]]) tot ++;
                       rankk [a [i]] = tot;
                }
                for (int len=1;len<=n;len<<=1){
                           memset(sum,0,sizeof(sum));
                           for (int i=1;i<=n;i++) sum[rankk[i+len]]++;
                           for (int i=1;i<=n;i++) sum[i]+=sum[i-1];
                           for (int i=n;i>=1;i--) tmp[sum[rankk[i+len]]--]=i;
                           memset(sum,0,sizeof(sum));
                           for (int i=1;i<=n;i++) sum[rankk[i]]++;
                           for (int i=1;i<=n;i++) sum[i]+=sum[i-1];
                           for (int i=n;i>=1;i--) sa[sum[rankk[tmp[i]]]--]=tmp[i];
                           tot = 0;
                           tmp [sa [1]] ++ = all;
                           for (int i=2;i<=n;i++){
                                   if (Rankk [the [i]]! = Rankk [the [i-1]] || Rankk [the [i] + only]! = Rankk [the [i-1] + only]) tot ++;
                                   tmp [sa [i]] = all;
                           }
                           for (int i=1;i<=n;i++) rankk[i]=tmp[i];
                }
                a [1] = 0;
                for (int i=1,j=0;i<=n;i++){
                           if (rankk[i]==1) continue;
                           while (st[i+j]==st[sa[rankk[i]-1]+j]) j++;
                           hi [rankk [i]] = j;
                           if (j) j--;
                }
                years = n;
                for (int i=2;i<=n;i++){
                       years + = (n-i + 1);
                       ANS = hi [i];
                }
                printf("%d\n",ans);
      }
      return 0;
}

  

Guess you like

Origin www.cnblogs.com/cutemush/p/12336866.html