• 694. Distinct Substrings (后缀数组)


    694. Distinct Substrings

    Problem code: DISUBSTR

    http://www.spoj.com/problems/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.

     这个超时,有待检查:

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    
    using namespace std;
    
    const int maxn=1010;
    
    char str[maxn];
    int wa[maxn],wb[maxn],wv[maxn],wn[maxn],a[maxn],sa[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++)  wn[i]=0;
        for(i=0; i<n; i++)  wn[x[i]=r[i]]++;
        for(i=1; i<m; i++)  wn[i]+=wn[i-1];
        for(i=n-1; i>=0; i--)   sa[--wn[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++)  wn[wv[i]]++;
            for(i=1; i<m; i++)  wn[i]+=wn[i-1];
            for(i=n-1; i>=0; i--)   sa[--wn[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++;
        }
    }
    
    int rank[maxn],height[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++);
    }
    
    int main(){
    
        freopen("input.txt","r",stdin);
    
        int t;
        scanf("%d",&t);
        while(t--){
            scanf("%s",str);
            int n=strlen(str);
            for(int i=0;i<n;i++)
                a[i]=(int)str[i];
            a[n]=0;
            da(a,sa,n+1,260);
            calheight(a,sa,n);
            int sum=0;
            for(int i=1;i<=n;i++)
                sum+=n-sa[i]-height[i];
            printf("%d\n",sum);
        }
        return 0;
    }

    AC:http://www.cnblogs.com/kuangbin/archive/2013/04/24/3039634.html

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    
    using namespace std;
    
    const int maxn=1010;
    
    char str[maxn];
    int wa[maxn],wb[maxn],wv[maxn],wn[maxn],a[maxn],sa[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++)  wn[i]=0;
        for(i=0; i<n; i++)  wn[x[i]=r[i]]++;
        for(i=1; i<m; i++)  wn[i]+=wn[i-1];
        for(i=n-1; i>=0; i--)   sa[--wn[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++)  wn[wv[i]]++;
            for(i=1; i<m; i++)  wn[i]+=wn[i-1];
            for(i=n-1; i>=0; i--)   sa[--wn[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++;
        }
    }
    
    int rank[maxn],height[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++);
    }
    
    int main(){
    
        freopen("input.txt","r",stdin);
    
        int t;
        scanf("%d",&t);
        while(t--){
            scanf("%s",str);
            int n=strlen(str);
            for(int i=0;i<n;i++)
                a[i]=(int)str[i];
            a[n]=0;
            da(a,sa,n+1,260);
            calheight(a,sa,n);
            int sum=0;
            for(int i=1;i<=n;i++)
                sum+=n-sa[i]-height[i];
            printf("%d\n",sum);
        }
        return 0;
    }
  • 相关阅读:
    Android中Activity之间通信
    vs2017 2019 下载更新慢的解决方法
    c# 判断某个类是否实现某个接口
    mvc api 关于 post 跟get 请求的一些想法[FromUri] 跟[FromBody] 同一个控制器如何实现共存
    vs2017 mvc 自定义路由规则 出现 404.0 错误代码 0x80070002
    C# winform 发布的时候没有app.config去哪儿了?
    安装c#服务
    Type.GetType反射的对象创建Activator.CreateInstance
    c# 谷歌动态口令对接
    asp.net mvc 异步控制器
  • 原文地址:https://www.cnblogs.com/jackge/p/3092080.html
Copyright © 2020-2023  润新知