牛客15957 Similarity

链接

点击跳转

题解

广义后缀自动机自动机做法:

两个串建广义后缀自动机,然后 d p dp dp统计 e n d p o s endpos endpos个数,然后乘起来

后缀自动机做法:

加一个分隔符,塞进 s a m sam sam,然后还是 d p dp dp,和上面说的那个流程一样

后缀数组做法(口胡,没写):

单调栈

代码(广义SAM)

#include <bits/stdc++.h>
#define iinf 0x3f3f3f3f
#define linf (1ll<<60)
#define eps 1e-8
#define maxn 1000010
#define cl(x) memset(x,0,sizeof(x))
#define rep(_,__) for(_=1;_<=(__);_++)
#define em(x) emplace(x)
#define emb(x) emplace_back(x)
#define emf(x) emplace_front(x)
#define fi first
#define se second
#define de(x) cerr<<#x<<" = "<<x<<endl
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
ll read(ll x=0)
{
    ll c, f(1);
    for(c=getchar();!isdigit(c);c=getchar())if(c=='-')f=-f;
    for(;isdigit(c);c=getchar())x=x*10+c-0x30;
    return f*x;
}
struct EXSAM
{
    int tot, ch[maxn<<1][26], fa[maxn<<1], len[maxn<<1], pref[maxn<<1];
    int* operator[](int u){return ch[u];}
    void init()
    {
        int i;
        rep(i,tot)cl(ch[i]),fa[i]=len[i]=pref[i]=0;
        tot=1;
    }
    int append(int las, int c, int tag)
    {
        int p(las);
        len[las=++tot]=len[p]+1;
        pref[las]=tag;
        for(;p and !ch[p][c];p=fa[p])ch[p][c]=las;
        if(!p)fa[las]=1;
        else
        {
            int q=ch[p][c];
            if(len[q]==len[p]+1)fa[las]=q;
            else
            {
                int qq=++tot;
                memcpy(ch[qq],ch[q],sizeof(ch[q]));
                fa[qq]=fa[q];
                len[qq]=len[p]+1;
                fa[q]=fa[las]=qq;
                for(;ch[p][c]==q;p=fa[p])ch[p][c]=qq;
            }
        }
        return las;
    }
}exsam;
ll dp1[maxn<<1], dp2[maxn<<1], deg[maxn<<1];
char s[maxn], t[maxn];
void dp()
{
    queue<int> q;
    int i;
    rep(i,exsam.tot)if(deg[i]==0)q.em(i);
    while(!q.empty())
    {
        auto u=q.front(); q.pop();
        if(exsam.pref[u]&1)dp1[u]++;
        if(exsam.pref[u]&2)dp2[u]++;
        auto f=exsam.fa[u];
        dp1[f]+=dp1[u];
        dp2[f]+=dp2[u];
        if(--deg[f]==0)q.em(f);
    }
}
int main()
{
    ll i, n, m, lcp=0;
    scanf("%s%s",s+1,t+1);
    n=strlen(s+1), m=strlen(t+1);
    rep(i,n)if(s[i]==t[i])lcp++;else break;
    exsam.init();
    ll p=1, p1, p2;
    rep(i,lcp)p=exsam.append(p,s[i]-'a',3);
    p1=p2=p;
    for(i=lcp+1;i<=n;i++)p1=exsam.append(p1,s[i]-'a',1);
    for(i=lcp+1;i<=m;i++)p2=exsam.append(p2,t[i]-'a',2);
    rep(i,exsam.tot)deg[exsam.fa[i]]++;
    ll ans=0;
    dp();
    rep(i,exsam.tot)ans+=dp1[i]*dp2[i]*(exsam.len[i]-exsam.len[ exsam.fa[i] ]);
    printf("%lld",ans);
    return 0;
}

代码(SAM)

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define iinf 0x3f3f3f3f
#define linf (1ll<<60)
#define eps 1e-8
#define maxn 1000010
#define maxe 1000010
#define cl(x) memset(x,0,sizeof(x))
#define rep(_,__) for(_=1;_<=(__);_++)
#define em(x) emplace(x)
#define emb(x) emplace_back(x)
#define emf(x) emplace_front(x)
#define fi first
#define se second
#define de(x) cerr<<#x<<" = "<<x<<endl
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
ll read(ll x=0)
{
    ll c, f(1);
    for(c=getchar();!isdigit(c);c=getchar())if(c=='-')f=-f;
    for(;isdigit(c);c=getchar())x=x*10+c-0x30;
    return f*x;
}
struct SAM
{
    int tot, las, ch[maxn<<1][27], fa[maxn<<1], len[maxn<<1], pref[maxn<<1];
    int* operator[](int u){return ch[u];}
    void init()
    {
        int i;
        rep(i,tot)cl(ch[i]),fa[i]=len[i]=pref[i]=0;
        tot=las=1;
    }
    void append(int c, int tag)
    {
        int p(las);
        len[las=++tot]=len[p]+1;
        pref[las]=tag;
        for(;p and !ch[p][c];p=fa[p])ch[p][c]=las;
        if(!p)fa[las]=1;
        else
        {
            int q=ch[p][c];
            if(len[q]==len[p]+1)fa[las]=q;
            else
            {
                int qq=++tot;
                memcpy(ch[qq],ch[q],sizeof(ch[q]));
                fa[qq]=fa[q];
                len[qq]=len[p]+1;
                fa[q]=fa[las]=qq;
                for(;ch[p][c]==q;p=fa[p])ch[p][c]=qq;
            }
        }
    }
    int mov(int p, int c){return max(1,ch[p][c]);}
}sam;
ll dp1[maxn<<1], dp2[maxn<<1], deg[maxn<<1];
char s[maxn], t[maxn];
void dp()
{
    queue<int> q;
    int i;
    rep(i,sam.tot)if(deg[i]==0)q.em(i);
    while(!q.empty())
    {
        auto u=q.front(); q.pop();
        if(sam.pref[u]==1)dp1[u]++;
        if(sam.pref[u]==2)dp2[u]++;
        auto f=sam.fa[u];
        dp1[f]+=dp1[u];
        dp2[f]+=dp2[u];
        if(--deg[f]==0)q.em(f);
    }
}
int main()
{
    ll i, n, m;
    scanf("%s%s",s+1,t+1);
    n=strlen(s+1), m=strlen(t+1);
    sam.init();
    rep(i,n)sam.append(s[i]-'a',1);
    sam.append(26,0);
    rep(i,m)sam.append(t[i]-'a',2);
    rep(i,sam.tot)deg[sam.fa[i]]++;
    ll ans=0;
    dp();
    rep(i,sam.tot)ans+=dp1[i]*dp2[i]*(sam.len[i]-sam.len[ sam.fa[i] ]);
    printf("%lld",ans);
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值