zoukankan      html  css  js  c++  java
  • 2018.12.17-dtoj-1171-长度不小于k的公共子串的个数

    题目描述:

    给定两个字符串,求出长度不小于k的公共字串的个数。

    算法标签:后缀数组

    以下代码:

    #include<bits/stdc++.h>
    #define il inline
    #define LL long long
    #define _(d) while(d(isdigit(ch=getchar())))
    using namespace std;
    const int N=2e5+5;
    char s[N],b[N];int l1,l2,n,k,sa[N],rk[N],height[N],x[N],y[N],c[N],top,q[N][2];LL ans,tot;
    il int read(){int x,f=1;char ch;_(!)ch=='-'?f=-1:f;x=ch^48;_()x=(x<<1)+(x<<3)+(ch^48);return f*x;}
    il void getsa(){
        int m=130;
        for(int i=1;i<=m;i++)c[i]=0;
        for(int i=1;i<=n;i++)c[x[i]=s[i]]++;
        for(int i=2;i<=m;i++)c[i]+=c[i-1];
        for(int i=n;i;i--)sa[c[x[i]]--]=i;
        for(int g=1;g<=n;g<<=1){
            int num=0;
            for(int i=n-g+1;i<=n;i++)y[++num]=i;
            for(int i=1;i<=n;i++)if(sa[i]>g)y[++num]=sa[i]-g;
            for(int i=1;i<=m;i++)c[i]=0;
            for(int i=1;i<=n;i++)c[x[i]]++;
            for(int i=2;i<=m;i++)c[i]+=c[i-1];
            for(int i=n;i;i--)sa[c[x[y[i]]]--]=y[i],y[i]=0;
            swap(x,y);num=1;x[sa[1]]=1;
            for(int i=2;i<=n;i++)
                x[sa[i]]=(y[sa[i]]==y[sa[i-1]]&&y[sa[i]+g]==y[sa[i-1]+g])?num:++num;
            if(num==n)break;m=num;
        }
        for(int i=1;i<=n;i++)rk[sa[i]]=i;
    }
    il void geth(){
        int g=0;
        for(int i=1;i<=n;i++){
            if(rk[i]==1)continue;
            if(g)g--;int j=sa[rk[i]-1];
            while(i+g<=n&&j+g<=n&&s[i+g]==s[j+g])g++;
            height[rk[i]]=g;
        }
    }
    int main()
    {
        while(1){
            k=read();if(k==0)break;
            scanf(" %s",s+1);n=l1=strlen(s+1);scanf(" %s",b+1);l2=strlen(b+1);
            s[++n]='@';for(int i=1;i<=l2;i++)s[++n]=b[i];s[++n]='0';
            getsa();geth();
            top=tot=ans=0;
            for(int i=2;i<=n;i++){
                if(height[i]<k){tot=top=0;continue;}
                int cnt=0;
                if(sa[i-1]<=l1)cnt++,tot+=height[i]-k+1;
                while(top>0&&height[i]<=q[top][0]){
                    tot-=q[top][1]*(q[top][0]-height[i]);
                    cnt+=q[top--][1];
                }
                q[++top][0]=height[i];q[top][1]=cnt;
                if(sa[i]>l1+1)ans+=tot;
            }
            tot=top=0;
            for(int i=2;i<=n;i++){
                if(height[i]<k){tot=top=0;continue;}
                int cnt=0;
                if(sa[i-1]>l1+1)cnt++,tot+=height[i]-k+1;
                while(top>0&&height[i]<=q[top][0]){
                    tot-=q[top][1]*(q[top][0]-height[i]);
                    cnt+=q[top--][1];
                }
                q[++top][0]=height[i];q[top][1]=cnt;
                if(sa[i]<=l1)ans+=tot;
            }
            printf("%lld
    ",ans);
        }
        return 0;
    }
    View Code
  • 相关阅读:
    MVC Form
    The way to learn english
    Test FastThree
    C#中Trim()、TrimStart()、TrimEnd()的用法
    c# Dictionary 简介
    visual studio快捷键大全
    ASP.NET MVC 中 ActionResult
    MVC4中使用 Ninject
    MVC Chapter 12 Overview of MVC Projects
    ASP.NET Razor
  • 原文地址:https://www.cnblogs.com/Jessie-/p/10131503.html
Copyright © 2011-2022 走看看