SPOJ 694 Distinct Substrings

后缀数组求不同的子串数。。

每一个子串都是某个后缀的前缀,每加入一个后缀都会增加 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.

Input

T- number of test cases. T<=20;
Each test case consists of one string, whose length is <= 1000

Output

For each 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.

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




你可能感兴趣的:(字符串)