POJ - 3415 Common Substrings 求2个串大于K的公共子串个数

本文介绍使用Suffix Array Machine (SAM)算法解决两个字符串中长度大于特定阈值K的公共子串数量问题。通过构建SAM并利用拓扑序确定每个节点在字符串中的出现次数,再用第二个字符串遍历SAM来统计匹配情况。

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

题目:求2个串大于K的公共子串个数

思路:对第一个串建立SAM,通过拓扑序,算出每个节点在串中的出现次数,然后用第二个串去跑SAM,match[i]表示i节点的经过次数

代码:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<algorithm>
#include<ctime>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<list>
#include<numeric>
using namespace std;
#define LL long long
#define ULL unsigned long long
#define INF 0x3f3f3f3f
#define mm(a,b) memset(a,b,sizeof(a))
#define PP puts("*********************");
template<class T> T f_abs(T a){ return a > 0 ? a : -a; }
template<class T> T gcd(T a, T b){ return b ? gcd(b, a%b) : a; }
template<class T> T lcm(T a,T b){return a/gcd(a,b)*b;}
// 0x3f3f3f3f3f3f3f3f
//0x3f3f3f3f

const int MAXN = 200050, SIZE = 52;
int getid(char ch){
    if(ch>='A'&&ch<='Z') return ch-'A'+26;
    else return ch-'a';
}
struct SAM {
    int len[MAXN], link[MAXN], next[MAXN][SIZE];
    int total, last;
    inline int newNode(int L) {
        len[++total] = L; link[total] = 0;
        for(int i = 0; i < SIZE; ++i) next[total][i] = 0;
        return total;
    }
    inline void Add(int c) {
        int i, p = last, cur = newNode(len[last] + 1);
        for(; p && !next[p][c]; p = link[p]) next[p][c] = cur;
        if(!p) link[cur] = 1;//令其指向初始状态
        else {
            int q = next[p][c];
            if(len[q] == len[p] + 1) link[cur] = q;
            else {//>
                int clone = newNode(len[p] + 1);
                for(i = 0; i < SIZE; ++i) next[clone][i] = next[q][i];
                link[clone] = link[q];
                link[q] = link[cur] = clone;
				for(; p && next[p][c] == q; p = link[p]) next[p][c] = clone;
            }
        }
        last = cur;
    }
    void Init () {//根节点是1
        total = 0;
        last = newNode(0);
    }
}sam;
char str[MAXN];
int num[MAXN],idx[MAXN],tim[MAXN],match[MAXN];
int main(){

    int K;
    while(~scanf("%d",&K)){
        if(K==0)
            break;
        scanf("%s",str);
        sam.Init();
        for(int i=0;str[i]!='\0';i++)
            sam.Add(getid(str[i]));
        for(int i=1;i<=sam.total;i++)
            num[i]=tim[i]=match[i]=0;
        for(int i=1;i<=sam.total;i++) num[sam.len[i]]++;
        for(int i=1;i<=sam.total;i++) num[i]+=num[i-1];
        for(int i=1;i<=sam.total;i++) idx[num[sam.len[i]]--]=i;
        int now=1,len=0;
        for(int i=0;str[i]!='\0';i++){
            int c=getid(str[i]);
            now=sam.next[now][c];
            tim[now]++;
        }
        for(int i=sam.total;i>=1;i--){
            int p=idx[i],np=sam.link[p];
            tim[np]+=tim[p];
        }
        scanf("%s",str);
        now=1,len=0;
        LL sum=0;
        for(int i=0;str[i]!='\0';i++){
            int c=getid(str[i]);
            if(sam.next[now][c]){
                len++;
                now=sam.next[now][c];
            }
            else{
                while(sam.next[now][c]==0&&now!=0)
                    now=sam.link[now];
                if(now==0){
                    now=1;
                    len=0;
                }
                else{
                    len=sam.len[now]+1;
                    now=sam.next[now][c];
                }
            }
            if(len>=K){
                int L=sam.len[sam.link[now]];
                sum+=(LL)tim[now]*(len-max(L,K-1));
                if(sam.len[sam.link[now]]>=K)
                    match[sam.link[now]]++;
            }
        }
        for(int i=sam.total;i>=1;i--){
            int p=idx[i];
            int np=sam.link[p];
            int L=sam.len[np];
            int x=max(0,sam.len[p]-max(L,K-1));
            sum+=(LL)x*match[p]*tim[p];
            if(sam.len[np]>=K)
                match[np]+=match[p];
        }
        printf("%lld\n",sum);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值