题解 CF1081H 【Palindromic Magic】

· · 题解

结论的证明请移步这里。

一点定义

我们称 t 为 s 的一个 border,当且仅当 s[1,t]=s[|s|-t+1,|s|]。

特别地,我们令 border(s) 为 s 最长的 border,如 border('aba')='a', border('ab')=''(空串)。

我们称 s 是一个循环串,当且仅当 s 存在一个不等于 |s| 的完全周期。

令 S=pq,p,q 是两个非空回文串,我们称 (p,q) 是一组 S 的回文拆分。

若 S=pq,p,q 是两个回文串且 q 非空,则我们称 (p,q) 是一组 S 的非严格回文拆分。

题解

考虑怎么统计题上要求的字符串。

不考虑去重,我们可以计数 A 中本质不同的回文串个数和 B 中本质不同的回文串个数,然后将它们两个乘起来。这显然会计重。

有一个重要的结论是,设一个串 S 的所有(可以非严格)回文拆分为 p_1q_1,p_2q_2,\cdots,p_tq_t,且 |p_1|<|p_2|<\cdots<|p_t|,则 p_i=border(p_{i+1}),q_{i+1}=border(q_i) 中至少有一条成立。

假设 S=xy,如果用 border(x) 代替 x 后能得到一种凑出 S 的方法,或者用 border(y) 代替 y 后能得出一种凑出 S 的方法,显然此时 S 就被重复统计了。

现在问题变成了:对于一个 A 中的回文串 x,我们想要知道有多少个 S 既能从 x 得到又能从 border(x) 得到,然后将答案减去那么多。找到 x 和 border(x) 分别是啥是简单的,只需要一棵回文树即可。

令 x=border(x)w,我们要数的就是在 B 中有多少个 T,使得 T=wS,且 S 和 T 都是 B 中的回文串。因为 |w| 是 x 最短的周期,因此 w 不是一个循环串。

若 |w|>|S|,则 w 一定可以写成 S+U,U 是某个回文串(因为要满足 wS 是回文串)。

因为 w 不是一个循环串,有结论是,w 只有至多一种非严格回文拆分,并且这个非严格回文拆分一定包含 w 的最长回文前缀/后缀,我们只需要验证这个即可,然后使用哈希验证 S 和 wS 是否在 B 中存在即可。

若 |w|\leq|S|,若 S 不是 T 的最长回文后缀,记最长回文后缀为 P,有 |T|-|P| 是一个 T 的周期,还有 |w| 也是一个 T 的周期,由另一个结论,有 (|T|-|P|)\mid |w|,得出 w 是循环串,矛盾。

因此,我们只需要对回文树上所有 S=border(T),且 (|T|-|S|)\times 2\leq |T| 的位置判断一遍即可,也可以使用哈希实现。

关于如何找 A 某个子串的最长回文前缀/后缀,你可以在回文树上倍增直到合法。

同理,我们还需要在 B 中数一遍 y。

注意到这样会额外减掉既能被 border(x) 统计到的,又能被 border(y) 统计到的,因此我们需要在最后额外加回来这种情况。

考虑若此时 w 既是左边那个大串的 border,又是右边那个串的 border,且红串属于 A,蓝串属于 B,这种情况就会被减去两次,于是只需要加上公共 w 对数即可。

代码:

#include <bits/stdc++.h>

using namespace std;

const int mod1 = 1e9 + 7,mod2 = 1e9 + 9;
const int base1 = 131,base2 = 1331;

int pw1[200050],pw2[200050];

struct Hash
{
    int h1,h2;
    long long val() const
    {
        return h1 * 1ll * mod2 + h2;
    }
    bool operator == (const Hash &a) const
    {
        return val() == a.val();
    }
    bool operator < (const Hash &a) const
    {
        return val() < a.val();
    }
    Hash operator + (const int &a) const
    {
        return (Hash){(h1 + a) % mod1,(h2 + a) % mod2};
    }
    Hash operator + (const Hash &a) const
    {
        return (Hash){(h1 + a.h1) % mod1,(h2 + a.h2) % mod2};
    }
    Hash operator - (const Hash &a) const
    {
        return (Hash){(h1 - a.h1 + mod1) % mod1,(h2 - a.h2 + mod2) % mod2};
    }
    Hash operator * (const int &h) const
    {
        return (Hash){h1 * 1ll * pw1[h] % mod1,h2 * 1ll * pw2[h] % mod2};
    }
    Hash operator * (const long long &h) const
    {
        return (Hash){h1 * h % mod1,h2 * h % mod2};
    }
}h1[200050],h2[200050];

map<Hash,int> mpa1,mpa2,mpb1,mpb2;

Hash HashA(int l,int r)
{
    return h1[r] - h1[l - 1] * (r - l + 1);
}

Hash HashB(int l,int r)
{
    return h2[r] - h2[l - 1] * (r - l + 1);
}

int N;

struct Palindromic_Tree
{
    int fail[200050],len[200050],son[200050][26];
    int fa[200050][19],pcnt,ln;
    int dy[200050];
    int ed[200050];
    int lst;
    bool rev_tag;
    char str[200050];
    void init()
    {
        pcnt = 1;
        fail[0] = 1; len[1] = -1;
    }
    bool chk(int nd,int r)
    {
        return str[r] == str[r - len[nd] - 1];
    }
    void append(char s)
    {
        str[++ ln] = s;
        int p = lst;
        while(!chk(p,ln))
            p = fail[p];
        if(!son[p][s - 'a'])
        {
            int k = fail[p];
            while(!chk(k,ln)) k = fail[k];
            pcnt ++; lst = pcnt; len[pcnt] = len[p] + 2; dy[pcnt] = ln;
            fail[pcnt] = fa[pcnt][0] = son[k][s - 'a']; son[p][s - 'a'] = pcnt;
            for(int i = 1;i <= 18; ++ i)
                fa[pcnt][i] = fa[fa[pcnt][i - 1]][i - 1];
        }
        else lst = son[p][s - 'a'];
        if(rev_tag) ed[N - ln + 1] = lst;
        else ed[ln] = lst;
    }
    int locate(int l,int r)
    {
        if(rev_tag)
        {
            int po = ed[l];
            for(int i = 18;i >= 0; -- i)
                if(len[fa[po][i]] >= r - l + 1)
                    po = fa[po][i];
            if(len[po] > r - l + 1) po = fail[po];
            return l + len[po] - 1;
        }
        else
        {
            int po = ed[r];
            for(int i = 18;i >= 0; -- i)
                if(len[fa[po][i]] >= r - l + 1)
                    po = fa[po][i];
            if(len[po] > r - l + 1) po = fail[po];
            return r - len[po] + 1;
        }
    }
    bool check(int l,int r)
    {
        if(rev_tag)
        {
            int po = ed[l];
            for(int i = 18;i >= 0; -- i)
                if(len[fa[po][i]] >= r - l + 1)
                    po = fa[po][i];
            return len[po] == r - l + 1;
        }
        else
        {
            int po = ed[r];
            for(int i = 18;i >= 0; -- i)
                if(len[fa[po][i]] >= r - l + 1)
                    po = fa[po][i];
            return len[po] == r - l + 1;
        }
    }
}A,RA,B,RB;

char S[200050],T[200050];
/*
aaabbaabbaabbbbaaaba
bbabababbaaaaabbbaba
*/

int main()
{
    A.init(); RA.init(); B.init(); RB.init();
    scanf("%s%s",S + 1,T + 1);
    int len1 = strlen(S + 1),len2 = strlen(T + 1);
    pw1[0] = pw2[0] = 1;
    for(int i = 1;i <= 200000; ++ i)
        pw1[i] = pw1[i - 1] * 1ll * base1 % mod1,
        pw2[i] = pw2[i - 1] * 1ll * base2 % mod2;
    N = len1;
    for(int i = 1;i <= len1; ++ i)
    {
        A.append(S[i]);
        h1[i].h1 = h1[i - 1].h1 * 1ll * base1 % mod1;
        h1[i].h2 = h1[i - 1].h2 * 1ll * base2 % mod2;
        h1[i] = h1[i] + S[i];
    }
    RA.rev_tag = 1; RB.rev_tag = 1;
    for(int i = len1;i >= 1; -- i)
        RA.append(S[i]);
    N = len2;
    for(int i = 1;i <= len2; ++ i)
    {
        B.append(T[i]);
        h2[i].h1 = h2[i - 1].h1 * 1ll * base1 % mod1;
        h2[i].h2 = h2[i - 1].h2 * 1ll * base2 % mod2;
        h2[i] = h2[i] + T[i];
    }
    for(int i = len2;i >= 1; -- i)
        RB.append(T[i]);
    long long as = (A.pcnt - 1) * 1ll * (B.pcnt - 1);
    cerr << as << endl;
    for(int i = 2;i <= A.pcnt; ++ i)
    {
        mpa1[HashA(A.dy[i] - A.len[i] + 1,A.dy[i])] ++;
        if(A.fail[i] < 2) continue;
        if((A.len[i] - A.len[A.fail[i]]) * 2 <= A.len[i])
            mpa2[HashA(A.dy[i] - (A.len[i] - A.len[A.fail[i]]) + 1,A.dy[i])] ++;
    }
    for(int i = 2;i <= B.pcnt; ++ i)
    {
        mpb1[HashB(B.dy[i] - B.len[i] + 1,B.dy[i])] ++;
        if(B.fail[i] < 2) continue;
        if((B.len[i] - B.len[B.fail[i]]) * 2 <= B.len[i])
            mpb2[HashB(B.dy[i] - B.len[i] + 1,B.dy[i] - B.len[B.fail[i]])] ++;
    }
    for(int i = 2;i <= A.pcnt; ++ i)
    {
        if(A.fail[i] < 2) continue;
        int l = A.dy[i] - (A.len[i] - A.len[A.fail[i]]) + 1,r = A.dy[i];
        Hash w = HashA(l,r);
        as -= mpb2[w];
        int rbd = A.locate(l,r);
        if(rbd == l) continue;
        // U 在后面
        if(A.check(l,rbd - 1))
        {
            w = HashA(l,r) * (rbd - l) + HashA(l,rbd - 1);
            if(mpb1.count(HashA(l,rbd - 1))) 
                as -= mpb1[w];
            continue;
        }
        int lbd = RA.locate(l,r);
        if(A.check(lbd + 1,r))
        {
            w = HashA(l,r) * (lbd - l + 1) + HashA(l,lbd);
            if(mpb1.count(HashA(l,lbd)))
                as -= mpb1[w];
        }
    }
    cerr << as << endl;
    for(int i = 2;i <= B.pcnt; ++ i)
    {
        if(B.fail[i] < 2) continue;
        int l = B.dy[i] - B.len[i] + 1,r = B.dy[i] - B.len[B.fail[i]];
        Hash w = HashB(l,r);
        as -= mpa2[w];
        int rbd = B.locate(l,r);
        if(rbd == l) continue;
        // U 在后面
        if(B.check(l,rbd - 1))
        {
            w = HashB(rbd,r) * (r - l + 1) + HashB(l,r);
            if(mpa1.count(HashB(rbd,r)))
                as -= mpa1[w];
            continue;
        }
        int lbd = RB.locate(l,r);
        if(B.check(lbd + 1,r))
        {
            w = HashB(lbd + 1,r) * (r - l + 1) + HashB(l,r);
            if(mpa1.count(HashB(lbd + 1,r)))
                as -= mpa1[w];
        }
    }
    cerr << as << endl;
    for(int i = 2;i <= A.pcnt; ++ i)
        if(A.fail[i] >= 2 && (A.len[i] - A.len[A.fail[i]]) * 2 > A.len[i])
            mpa2[HashA(A.dy[i] - (A.len[i] - A.len[A.fail[i]]) + 1,A.dy[i])] ++;
    for(int i = 2;i <= B.pcnt; ++ i)
        if(B.fail[i] >= 2 && (B.len[i] - B.len[B.fail[i]]) * 2 > B.len[i])
            mpb2[HashB(B.dy[i] - B.len[i] + 1,B.dy[i] - B.len[B.fail[i]])] ++;
    for(auto v : mpa2)
        as += v.second * 1ll * mpb2[v.first];
    printf("%lld\n",as);
}