跳到正文

题解

题解 CF1081H 【Palindromic Magic】

Wen_kr约 15 分钟
原文
文章信息与版本

文章信息

发布
2020年6月23日
收录
2026年3月23日
更新
2026年11月14日
篇幅
约 15 分钟
题目
CF1081H

版本

  1. v2当前
  2. v1

结论的证明请移步这里。

一点定义

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


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


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


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

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


题解

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

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

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

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

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

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

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

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

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

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

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

同理,我们还需要在 BB 中数一遍 yy。

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

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

代码:

C++
#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);
}
原文
存档评论3 条抓取于