POJ3415 Common Substrings(后缀数组,单调栈)

本文介绍了一种高效计算两个字符串间所有长度大于等于K的公共子串数量的方法。通过后缀数组和单调栈实现,文章详细展示了算法的实现过程及核心代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Common Substrings
Time Limit: 5000MS Memory Limit: 65536K
Total Submissions: 11195 Accepted: 3701

Description

A substring of a string T is defined as:

T(ik)=TiTi+1...Ti+k-1, 1≤ii+k-1≤|T|.

Given two strings AB and one integer K, we define S, a set of triples (ijk):

S = {(ijk) | kKA(ik)=B(jk)}.

You are to give the value of |S| for specific AB and K.

Input

The input file contains several blocks of data. For each block, the first line contains one integer K, followed by two lines containing strings A and B, respectively. The input file is ended by K=0.

1 ≤ |A|, |B| ≤ 105
1 ≤ K ≤ min{|A|, |B|}
Characters of A and B are all Latin letters.

Output

For each case, output an integer |S|.

Sample Input

2
aababaa
abaabaa
1
xx
xx
0

Sample Output

22
5

题意:给出两个字符串,求出公共子串的个数。

思路:每个子串可以看作是后缀的公共前缀,首先考虑针对每个a串的后缀,b串每个后缀的贡献,用一个单调栈维护高度数组。两个后缀之间的最长公共前缀是后缀数组中连续高度数组的最小值,因此高度数组的较大值可以被较小值覆盖。


#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<vector>
#include<algorithm>
#define F(x) ((x)/3+((x)%3==1?0:tb))
#define G(x) ((x)<tb?(x)*3+1:((x)-tb)*3+2)
using namespace std;
const int MAXN = 4e6+10;
const int N = MAXN;
const int INF=1e9+7;
int wa[MAXN],wb[MAXN],wv[MAXN],ws[MAXN];
char s[MAXN],t[MAXN];
int a[MAXN];
int sa[MAXN],height[MAXN],RANK[MAXN];
int len[MAXN];
int c0(int *r,int a,int b)
{
    return r[a]==r[b]&&r[a+1]==r[b+1]&&r[a+2]==r[b+2];
}
int c12(int k,int *r,int a,int b)
{
    if(k==2) return r[a]<r[b]||r[a]==r[b]&&c12(1,r,a+1,b+1);
    else return r[a]<r[b]||r[a]==r[b]&&wv[a+1]<wv[b+1];
}
void sort(int *r,int *a,int *b,int n,int m)
{
    int i;
    for(i=0;i<n;i++) wv[i]=r[a[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--) b[--ws[wv[i]]]=a[i];
    return;
}
void dc3(int *r,int *sa,int n,int m) //涵义与DA 相同
{
    int i,j,*san=sa+n,ta=0,tb=(n+1)/3,tbc=0,p;
    int *rn=r+n;
    r[n]=r[n+1]=0;
    for(i=0;i<n;i++) if(i%3!=0) wa[tbc++]=i;
    sort(r+2,wa,wb,tbc,m);
    sort(r+1,wb,wa,tbc,m);
    sort(r,wa,wb,tbc,m);
    for(p=1,rn[F(wb[0])]=0,i=1;i<tbc;i++)
        rn[F(wb[i])]=c0(r,wb[i-1],wb[i])?p-1:p++;
    if(p<tbc) dc3(rn,san,tbc,p);
    else for(i=0;i<tbc;i++) san[rn[i]]=i;
    for(i=0;i<tbc;i++) if(san[i]<tb) wb[ta++]=san[i]*3;
    if(n%3==1) wb[ta++]=n-1;
    sort(r,wb,wa,ta,m);
    for(i=0;i<tbc;i++) wv[wb[i]=G(san[i])]=i;
    for(i=0,j=0,p=0;i<ta && j<tbc;p++)
        sa[p]=c12(wb[j]%3,r,wa[i],wb[j])?wa[i++]:wb[j++];
    for(;i<ta;p++) sa[p]=wa[i++];
    for(;j<tbc;p++) sa[p]=wb[j++];
    return;
}
void calheight(int *r,int *sa,int n)
{   // 此处N为实际长度
    int i,j,k=0;
    // height[]的合法范围为 1-N, 其中0是结尾加入的字符
    for(i=1;i<=n;i++)
        RANK[sa[i]]=i;
    // 根据SA求RANK
    for(i=0;i<n; height[RANK[i++]] = k )
        // 定义:h[i] = height[ RANK[i] ]
        for(k?k--:0,j=sa[RANK[i]-1];
            r[i+k]==r[j+k]; k++);
    //根据 h[i] >= h[i-1]-1 来优化计算height过程
}


int k;
int n,l;
//q[0][i]代表当前的高度数组的值
//q[1][i]代表被当前的值覆盖了几个数
int q[2][MAXN];
void solve(){
    long long res=0;
    long long tot=0;
    int top=0;
    for(int i=1;i<=n;i++){
        if(height[i]<k){
            tot=0;
            top=0;
            continue;
        }
        long long cnt=0;
        if(sa[i-1]<l){
            cnt++;
            tot+=1LL*height[i]-k+1;
        }
        while(top&&q[0][top]>height[i]){
            tot-=1LL*q[1][top]*(1LL*q[0][top]-height[i]);
            cnt+=q[1][top];
            top--;
        }
        q[0][++top]=height[i];
        q[1][top]=(int)cnt;
        if(sa[i]>l)
            res+=tot;
    }
    tot=0,top=0;
    for(int i=1;i<=n;i++){
        if(height[i]<k){
            tot=0;
            top=0;
            continue;
        }
        long long cnt=0;
        if(sa[i-1]>l){
            cnt++;
            tot+=1LL*height[i]-k+1;
        }
        while(top&&q[0][top]>height[i]){
            tot-=1LL*q[1][top]*(1LL*q[0][top]-height[i]);
            cnt+=q[1][top];
            top--;
        }
        q[0][++top]=height[i];
        q[1][top]=(int)cnt;
        if(sa[i]<l)
            res+=tot;
    }
    printf("%lld\n",res);
}
int main(){
    while(scanf("%d",&k)&&k){
        scanf("%s%s",s,t);
        n=(int)strlen(s);
        for(int i=0;i<n;i++){
            a[i]=s[i];
        }
        a[n++]=1;
        l=n-1;
        int m=(int)strlen(t);
        for(int i=0;i<m;i++){
            a[n+i]=t[i];
        }
        n=n+m;
        a[n]=0;
        dc3(a,sa,n+1,200);
        calheight(a, sa, n);
        solve();
    }
}






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值