Distinct Substrings SPOJ - 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 <= 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.

後綴數組的簡單運用,統計字符串中的所有不同子字符串個數。對於一個後綴sa[i],從他的第一個字符開始,後面有多長就有多少個子字符串,所有後綴的開頭都不一樣,所以所有後綴的子字符串個數之和就是總數。在height數組中,當前後綴和上一個後綴的共同前綴有多長,就有幾個子字符串是重複的,所以公式就是len-sa[i]-height[i](sa從0開始)

#include <iostream>
#include <stdio.h>
#include <map>
#include <set>
#include <queue>
#include <algorithm>
#include <math.h>
#include <string.h>
using namespace std;
int mo[4][2]={0,1,1,0,0,-1,-1,0};
const int MAXN=0x3f3f3f3f;
const int sz=100005;

int n;
int t1[sz],t2[sz],c[sz],sa[sz],tsa[sz],rk[sz],height[sz],r[sz],RMQ[sz],mm[sz];
int best[20][sz];
char str[sz];

bool cmp(int *r,int a,int b,int l){
    return r[a]==r[b]&&r[a+l]==r[b+l];
    //hash,判斷兩個關鍵字是否完全一樣
}

void da(int str[],int sa[],int rk[],int height[],int n,int m){
    n++;
    int i,j,p,*x=t1,*y=t2;
    for(i=0;i<m;i++) c[i]=0;
    for(i=0;i<n;i++) c[x[i]=str[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){//排序的子字符串長度以2^n速度增長,所以只要排序logn次
        p=0;
        for(i=n-j;i<n;i++) y[p++]=i;
        //後面j個數沒有第二關鍵字,可以理解爲第二關鍵字爲0,排名放在最前
        for(i=0;i<n;i++) if(sa[i]>=j) y[p++]=sa[i]-j;
        //sa[i]-j的第二關鍵字是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]]=cmp(y,sa[i-1],sa[i],j)?p-1:p++;
            //這裏的y是之前的x,記錄數組編號對應的具體字符
            //統計數字種類,縮小範圍
        if(p>=n) break;
        //每個位置都單獨對應了一個排名,可以不用排序了
        m=p;
    }
    int k=0;
    n--;
    for(i=0;i<=n;i++) rk[sa[i]]=i;
    for(i=0;i<n;i++){
        if(k)k--;
        j=sa[rk[i]-1];
        while(str[i+k]==str[j+k])k++;
        height[rk[i]]=k;
    }
}

void initRMQ(int n){
    mm[0]=-1;
    int i,j;
    for(i=1;i<=n;i++){
        mm[i]=((i&(i-1))==0)?mm[i-1]+1:mm[i-1];
        //當i是2^n時i&(i-1)爲0,則此時查詢的長度上限要增加一倍
    }
    for(i=1;i<=n;i++) best[0][i]=i;
    for(i=1;i<=mm[n];i++){
        for(j=1;j+(1<<i)-1<=n;j++){//j是起點,i是長度
            int a=best[i-1][j];
            int b=best[i-1][j+(1<<(i-1))];
            if(RMQ[a]<RMQ[b]) best[i][j]=a;
            else best[i][j]=b;
        }
    }
}

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){
    a=rk[a];b=rk[b];
    if(a>b) swap(a,b);
    return height[askRMQ(a+1,b)];
    //a爲什麼+1?因爲hight記錄的是i和i-1之間的前綴長度,求a到b就要從a+1開始
}

int main()
{
    int i,T;
    //freopen("in.txt","r",stdin);
    scanf("%d",&T);
    while(T--){
        scanf("%s",str);
        int len=strlen(str);
        n=len;
        for(i=0;i<len;i++) r[i]=str[i];
        r[len]=1;
        r[n]=0;
        da(r,sa,rk,height,n,128);
        /*for(i=1;i<=n;i++) RMQ[i]=height[i];
        initRMQ(n);*/
        int ans=0;
        /*for(i=1;i<=len;i++){
            cout<<sa[i]<<' ';
        }
        cout<<endl;
        for(i=1;i<=len;i++){
            cout<<height[i]<<' ';
        }
        cout<<endl;
        sa從0開始
        rk從1開始
        */
        for(i=1;i<=len;i++){
            //printf("i:%d sa:%d h:%d\n",i,sa[i],height[i]);
            ans+=len-sa[i]-height[i];
        }
        cout<<ans<<endl;
    }
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章