题解 CF1081H 【Palindromic Magic】
结论的证明请移步这里。
一点定义
我们称
特别地,我们令
我们称
令
若
题解
考虑怎么统计题上要求的字符串。
不考虑去重,我们可以计数
有一个重要的结论是,设一个串
假设
现在问题变成了:对于一个
令
若
因为
若
因此,我们只需要对回文树上所有
关于如何找
同理,我们还需要在
注意到这样会额外减掉既能被
考虑若此时
代码:
#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);
}