加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

spoj694后缀树组

发布时间:2020-12-16 00:36:25 所属栏目:大数据 来源:网络整理
导读:? 题目来源:http://www.spoj.pl/problems/DISUBSTR/ 题目分类:后缀树组 此题心得:学会用后缀数组解决问题 作者:王耀宣 时间:2011-7-21 SPOJ Problem Set (classical) 694. Distinct Substrings Problem code: DISUBSTR Given a string,we need to find
?

题目来源:http://www.spoj.pl/problems/DISUBSTR/

题目分类:后缀树组

此题心得:学会用后缀数组解决问题

作者:王耀宣

时间:2011-7-21

SPOJ Problem Set (classical)

694. Distinct Substrings

Problem code: DISUBSTR

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.

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.


Added by:

Prasanna

Date:

2006-01-13

Time limit:

1s

Source limit:

50000B

Languages:

All except: PERL 6

Resource:

ByteCode '06

?

?

?

?

?


hide comments

?

题意与分析

题意:题意很简单,给你一个串,求这个串的互不相同的字串的个数。

分析:用后缀数组求解height数组,可以理解,对排序的后缀串,第一个子串有几个字母就有几个不同的串,然后第二个和第一个之间的差别在于与前一个不相同的部分,所以就加上每个子串的长度len-i+1减去height[i]。这个比较好理解,其实稍微想想就更简单了。1---n都加了一遍,height[1—n]都被减了,所以不用一步步算,直接求就可以了(但小心n*n会超int)。。。题目很变态的就是说了只有大写字母,实际上所有ascii字符都有,re了好久都不知道错哪里。

源代码

?

#include<stdio.h>

#include<string.h>

 

#define maxn 1000001

int wa[maxn],wb[maxn],wv[maxn],ws[maxn];

int cmp(int *r,int a,int b,int l)

{

    return r[a]==r[b]&&r[a+l]==r[b+l];

}

void da(int *r,int *sa,int n,int m)

{

     int i,j,p,*x=wa,*y=wb,*t;

     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];

     for(i=n-1;i>=0;i--) sa[--ws[x[i]]]=i;

     for(j=1,p=1;p<n;j*=2,m=p)

     {

       for(p=0,i=n-j;i<n;i++) y[p++]=i;

       for(i=0;i<n;i++) if(sa[i]>=j) y[p++]=sa[i]-j;

       for(i=0;i<n;i++) wv[i]=x[y[i]];

       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];

       for(t=x,x=y,y=t,p=1,x[sa[0]]=0,i=1;i<n;i++)

       x[sa[i]]=cmp(y,sa[i-1],sa[i],j)?p-1:p++;

     }

     return;

}

int rank[maxn],height[maxn];

void calheight(int *r,int n)

{

     int i,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++);

     return;

}

int RMQ[maxn];

int mm[maxn];

int best[20][maxn];

void initRMQ(int n)

{

     int i,a,b;

     for(mm[0]=-1,i=1;i<=n;i++)

     mm[i]=((i&(i-1))==0)?mm[i-1]+1:mm[i-1];

     for(i=1;i<=n;i++) best[0][i]=i;

     for(i=1;i<=mm[n];i++)

     for(j=1;j<=n+1-(1<<i);j++)

     {

       a=best[i-1][j];

       b=best[i-1][j+(1<<(i-1))];

       if(RMQ[a]<RMQ[b]) best[i][j]=a;

       else best[i][j]=b;

     }

     return;

}

int askRMQ(int a,int b)

{

    int t;

    t=mm[b-a+1];b-=(1<<t)-1;

    a=best[t][a];b=best[t][b];

    return RMQ[a]<RMQ[b]?a:b;

}

int lcp(int a,int b) //最长公共前缀

{

    int t;

    a=rank[a];b=rank[b];

    if(a>b) {t=a;a=b;b=t;}

    return(height[askRMQ(a+1,b)]);

}

 

//求一个串任意两后缀最长公共前缀示例。。。RMQ实现。。

int len1,len2,n,k,mx;

char s[maxn];

int sa[maxn],a[maxn];

int q[maxn];

int main()

{

    int i,j;

    int head,tail,ans,cas;

 

    scanf("%d",&cas);

    while(cas--)

    {

       scanf("%s",s);

       n = strlen(s);

       for(i=0; i<n; i++)

       {

           //scanf("%d",&a[i]);

           a[i] = s[i]+1;

       }

       a[n] = 0;

       da(a,sa,n+1,250);

       calheight(a,n);

       ans = n*(1+n)/2;

       for(i=1; i<=n; i++)

           ans -= height[i];

       printf("%dn",ans);

    }

    return 0;

}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读