SPOJ SUBST1 - New Distinct Substrings(后缀数组[不相同的子串的个数])

来源:互联网 发布:拼接屏矩阵 编辑:程序博客网 时间:2024/05/18 20:12

此文章可以使用目录功能哟↑(点击上方[+])

 SPOJ SUBST1 - New Distinct Substrings

Accept: 0    Submit: 0
Time Limit: 280 MS    Memory Limit : 1536 MB

 Problem 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 <= 50000

 Output

For each test case output one number saying the number of distinct substrings.

 Sample Input

2
CCCCC
ABABA

 Sample Output

5
9

 Hint

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.

 Problem Idea

解题思路:

【题意】

给你一个字符串,我们需要找出该字符串中不相同子串的数目

【类型】
后缀数组[不相同的子串的个数]
【分析】

本题是一道裸的后缀数组题

"不相同的子串的个数"解法(摘自罗穗骞的国家集训队论文):

每个子串一定是某个后缀的前缀, 那么原问题等价于求所有后缀之间的不相同的前缀的个数。如果所有的后缀按照 suffix(sa[1]), suffix(sa[2]),
suffix(sa[3]), …… ,suffix(sa[n])的顺序计算, 不难发现, 对于每一次新加进来的后缀 suffix(sa[k]),它将产生 n-sa[k]+1 个新的前缀。但是其中有
height[k]个是和前面的字符串的前缀是相同的。所以suffix(sa[k])将“ 贡献”出n-sa[k]+1- height[k]个不同的子串。累加后便是原问题的答案。这个做法的时间复杂度为O(n)。

ps:字符串是从下标0开始存储的,故每一次新加进来的后缀 suffix(sa[k]),它将产生 n-sa[k]-height[k] 个新的前缀

另外,此题有个坑点,题目并没有说字符串仅包含大写字母,所以,在求解的过程中,我们可以当做ASCII码表中的所有字符都会出现,所以在求后缀数组sa[]时,最大值设置为128即可

【时间复杂度&&优化】
O(nlogn)

题目链接→SPOJ SUBST1 - New Distinct Substrings

 Source Code

/*Sherlock and Watson and Adler*/#pragma comment(linker, "/STACK:1024000000,1024000000")#include<stdio.h>#include<string.h>#include<stdlib.h>#include<queue>#include<stack>#include<math.h>#include<vector>#include<map>#include<set>#include<bitset>#include<cmath>#include<complex>#include<string>#include<algorithm>#include<iostream>#define eps 1e-9#define LL long long#define PI acos(-1.0)#define bitnum(a) __builtin_popcount(a)using namespace std;const int N = 10;const int M = 100005;const int inf = 1000000007;const int mod = 1000000007;const int MAXN = 100010;//rnk从0开始//sa从1开始,因为最后一个字符(最小的)排在第0位//height从1开始,因为表示的是sa[i - 1]和sa[i]//倍增算法 O(nlogn)int wa[MAXN], wb[MAXN], wv[MAXN], ws_[MAXN];//Suffix函数的参数m代表字符串中字符的取值范围,是基数排序的一个参数,如果原序列都是字母可以直接取128,如果原序列本身都是整数的话,则m可以取比最大的整数大1的值//待排序的字符串放在r数组中,从r[0]到r[n-1],长度为n//为了方便比较大小,可以在字符串后面添加一个字符,这个字符没有在前面的字符中出现过,而且比前面的字符都要小//同上,为了函数操作的方便,约定除r[n-1]外所有的r[i]都大于0,r[n-1]=0//函数结束后,结果放在sa数组中,从sa[0]到sa[n-1]void Suffix(int *r, int *sa, int n, int m){    int i, j, k, *x = wa, *y = wb, *t;    //对长度为1的字符串排序    //一般来说,在字符串的题目中,r的最大值不会很大,所以这里使用了基数排序    //如果r的最大值很大,那么把这段代码改成快速排序    for(i = 0; i < m; ++i) ws_[i] = 0;    for(i = 0; i < n; ++i) ws_[x[i] = r[i]]++;//统计字符的个数    for(i = 1; i < m; ++i) ws_[i] += ws_[i - 1];//统计不大于字符i的字符个数    for(i = n - 1; i >= 0; --i) sa[--ws_[x[i]]] = i;//计算字符排名    //基数排序    //x数组保存的值相当于是rank值    for(j = 1, k = 1; k < n; j *= 2, m = k)    {        //j是当前字符串的长度,数组y保存的是对第二关键字排序的结果        //第二关键字排序        for(k = 0, i = n - j; i < n; ++i) y[k++] = i;//第二关键字为0的排在前面        for(i = 0; i < n; ++i) if(sa[i] >= j) y[k++] = sa[i] - j;//长度为j的子串sa[i]应该是长度为2 * j的子串sa[i] - j的后缀(第二关键字),对所有的长度为2 * j的子串根据第二关键字来排序        for(i = 0; i < n; ++i) wv[i] = x[y[i]];//提取第一关键字        //按第一关键字排序 (原理同对长度为1的字符串排序)        for(i = 0; i < m; ++i) ws_[i] = 0;        for(i = 0; i < n; ++i) ws_[wv[i]]++;        for(i = 1; i < m; ++i) ws_[i] += ws_[i - 1];        for(i = n - 1; i >= 0; --i) sa[--ws_[wv[i]]] = y[i];//按第一关键字,计算出了长度为2 * j的子串排名情况        //此时数组x是长度为j的子串的排名情况,数组y仍是根据第二关键字排序后的结果        //计算长度为2 * j的子串的排名情况,保存到数组x        t = x;        x = y;        y = t;        for(x[sa[0]] = 0, i = k = 1; i < n; ++i)            x[sa[i]] = (y[sa[i - 1]] == y[sa[i]] && y[sa[i - 1] + j] == y[sa[i] + j]) ? k - 1 : k++;        //若长度为2 * j的子串sa[i]与sa[i - 1]完全相同,则他们有相同的排名    }}int Rank[MAXN], height[MAXN], sa[MAXN], r[MAXN];void calheight(int *r,int *sa,int n){    int i,j,k=0;    for(i=1; i<=n; i++)Rank[sa[i]]=i;    for(i=0; i<n; height[Rank[i++]]=k)        for(k?k--:0,j=sa[Rank[i]-1]; r[i+k]==r[j+k]; k++);}char s[MAXN];int main(){    int t,n,i;    LL ans;    scanf("%d",&t);    while(t--)    {        ans=0;        scanf("%s",s);        n=strlen(s);        for(i=0;i<n;i++)            r[i]=s[i];        r[i]=0;        Suffix(r,sa,n+1,128);        calheight(r,sa,n);        for(i=1;i<=n;i++)            ans+=n-sa[i]-height[i];        printf("%lld\n",ans);    }    return 0;}
菜鸟成长记
1 0
原创粉丝点击