跳到正文

题解

关于谐音替换(replace)的主席树做法

qkhm约 6 分钟
原文
文章信息与版本

文章信息

发布
2025年11月8日
收录
2025年12月12日
更新
2026年11月14日
篇幅
约 6 分钟
题目
P14363

版本

  1. v1当前

首先考虑暴力,对 s1s_1 建出 ACAM ,对于 t1t_1 进行匹配。

设当前匹配 t1t_1 结尾位置为 pp ,fail 树上为 uu 。

那么首先要保证 pp 之后的部分 t1,t2t_1, t_2 完全相同。 然后就可以暴力跳 fail 树上 uu 祖先的结尾节点,然后通过 hash 值的变化量判断是否合法。 (注意到替换对于 Hash 值的影响不过是减去一部分,又加上一部分,而后续长度是固定的,所以 Δ\Delta 只和 s1,s2s_1,s_2 的 ΔHash\Delta Hash 有关。)

这样我们就得到了一个 O(匹配次数)\mathcal O(\text{匹配次数}) 的做法。

然后考虑优化跳祖先这个过程,发现合法的 ΔHash\Delta Hash 值是固定的,在 fail 树上维护到根链的值域(这里使用主席树)即可做到 O(∣L∣log⁡mod)\mathcal O(\left|L\right|\log mod) 。

考场做法,赛后发现这样并不怕 s1=s2s_1=s_2 和 ∣t1∣≠∣t2∣\left|t_1\right|\ne \left|t_2\right| 的情况。(真是一对苦命鸳鸯)

码量很少,细节基本没有, 2020 min 就能写完。

C++
#include <bits/stdc++.h>
#define lep(i, a, b) for (int i = (a), ed##i = (b); i <= ed##i; ++i)
#define rep(i, a, b) for (int i = (a), ed##i = (b); i >= ed##i; --i)
#define il inline
#define pb push_back
#define fi first
#define se second
#define arr(ty, tn) std::array<ty, tn>
#define gmx(a, b) a = std::max(a, b)
#define gmn(a, b) a = std::min(a, b)

const int LN = 2e5 + 7;
const int LS = 5e6 + 7;
const int mod = ;
const int bs = ;
const int dl = ;
typedef long long ll;
typedef std::pair<int, int> PII;

bool FIRPOS;

int n, qy; std::string s1, s2, t1, t2;
int ch[LS][26], fl[LS], tot;
ll pw[LS], hs1[LS], hs2[LS];
std::vector <int> o[LS], e[LS];
int rt[LS], ls[LS << 2], rs[LS << 2], cnt[LS << 2], tct;

bool ENDPOS;

#define md ((s + t) >> 1)
void mdy(int v, int&u, int d, int s = 0, int t = mod) {
	u = ++tct; ls[u] = ls[v], rs[u] = rs[v], cnt[u] = cnt[v] + 1;
	if (s == t) return; d <= md ? mdy(ls[v], ls[u], d, s, md) : mdy(rs[v], rs[u], d, md + 1, t);
}
int qry(int u, int d, int s = 0, int t = mod) {
	if (!u) return 0; if (s == t) return cnt[u];
	return d <= md ? qry(ls[u], d, s, md) : qry(rs[u], d, md + 1, t);
}
#undef md
void ins() {
	int nw = 0, len = s1.size(), k; ll hs = 0;
	lep(i, 0, len - 1) {
		k = s1[i] - 'a', hs = (hs * bs % mod + (s2[i] - s1[i]) * dl % mod) % mod;
		if (!ch[nw][k]) ch[nw][k] = ++tot;
		nw = ch[nw][k];
	} hs = (hs + mod) % mod;
	o[nw].pb(hs);
}
void dfs(int u) {
	for (int t : o[u]) mdy(rt[u], rt[u], t);
	for (int v : e[u]) rt[v] = rt[u], dfs(v);
}
void bd() {
	std::queue <int> q;
	lep(k, 0, 25) if (ch[0][k]) q.push(ch[0][k]);
	while (!q.empty()) {
		int u = q.front(); q.pop(); e[fl[u]].pb(u);
		lep(k, 0, 25) {
			if (ch[u][k]) fl[ch[u][k]] = ch[fl[u]][k], q.push(ch[u][k]);
			else ch[u][k] = ch[fl[u]][k];
		}
	}
	dfs(0);
}
ll qry() {
	int nw = 0, len = t1.size(); ll ans = 0;
	if (t1.size() != t2.size()) return 0;
	t1 = " " + t1, t2 = " " + t2;
	lep(i, 1, len) hs1[i] = (hs1[i - 1] * bs % mod + t1[i] * dl % mod) % mod,
		hs2[i] = (hs2[i - 1] * bs % mod + t2[i] * dl % mod) % mod;
	lep(i, 1, len) {
		nw = ch[nw][t1[i] - 'a'];
		if ((hs1[len] - hs1[i] * pw[len - i] % mod + mod) % mod == (hs2[len] - hs2[i] * pw[len - i] % mod + mod) % mod)
			ans += qry(rt[nw], (hs2[i] - hs1[i] + mod) % mod);
	}
	return ans;
}

int main() {
	std::ios::sync_with_stdio(false),
	std::cin.tie(nullptr), std::cout.tie(nullptr);
	int c1 = clock();

	pw[0] = 1; lep(i, 1, LS - 1) pw[i] = pw[i - 1] * bs % mod;
	std::cin >> n >> qy;
	lep(i, 1, n) std::cin >> s1 >> s2, ins();
	bd();

	while (qy--) std::cin >> t1 >> t2, std::cout << qry() << '\n';

	std::cerr << clock() - c1 << " ms " << std::fabs(&ENDPOS - &FIRPOS) / 1024 / 1024 << " MB\n";
	return 0;
}
原文
存档评论1 条抓取于