• SPOJ 705 New Distinct Substrings


    后缀数组。按照排序完的后缀一个一个统计。每一个后缀对答案做出的贡献为:n-SA[i]-height[i]。

    #pragma comment(linker, "/STACK:1024000000,1024000000")
    #include<cstdio>
    #include<cstring>
    #include<cmath>
    #include<algorithm>
    #include<vector>
    #include<map>
    #include<set>
    #include<queue>
    #include<stack>
    #include<iostream>
    using namespace std;
    typedef long long LL;
    const double pi=acos(-1.0),eps=1e-8;
    void File()
    {
        freopen("D:\in.txt","r",stdin);
        freopen("D:\out.txt","w",stdout);
    }
    inline int read()
    {
        char c = getchar();  while(!isdigit(c)) c = getchar();
        int x = 0;
        while(isdigit(c)) { x = x * 10 + c - '0'; c = getchar(); }
        return x;
    }
    
    const int maxn=50000+10;
    
    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 *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++);
        return;
    }
    
    int T,n,a[maxn],SA[maxn];
    char str[maxn];
    
    int main()
    {
        scanf("%d",&T);
        while(T--)
        {
            scanf("%s",str); n=strlen(str);
            for(int i=0;i<n;i++) a[i]=(int)str[i];
            a[n]=0; da(a,SA,n+1,300); calheight(a,SA,n);
            LL ans=0;
            for(int i=0;i<=n;i++) ans=ans+n-SA[i]-height[i];
            printf("%lld
    ",ans);
        }
        return 0;
    }
  • 相关阅读:
    【Codeforces Round #645 (Div. 2) A】 Park Lighting
    【Codeforces Round #636 (Div. 3) D】Constant Palindrome Sum
    【Codeforces Round #628 (Div. 2) D】Ehab the Xorcist
    【Codeforces Round #642 (Div. 3)】Constructing the Array
    【LeetCode 85】最大矩形(第二遍)
    scrum例会报告+燃尽图02
    scrum例会报告+燃尽图01
    版本控制报告
    Scrum立会报告+燃尽图 07
    Scrum立会报告+燃尽图 05
  • 原文地址:https://www.cnblogs.com/zufezzt/p/5720817.html
Copyright © 2020-2023  润新知