跳到正文

题解

P14363 谁没判呢?

SJZ2010约 6 分钟
原文

这是 2025年11月2日 存档的第 1 版,不是当前版本。查看当前版本

文章信息与版本

文章信息

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

版本

  1. v2当前
  2. v1

没保证 ∣t1∣=∣t2∣|t_1| = |t_2|,谁没判呢?/ll


纯 trie,可以线性的做法,但是考场上我写的是 O(n+q+Llog⁡∣Σ∣)O(n + q + L \log |\Sigma|) 的,因为有一棵 trie 要用 map 存儿子。


首先观察到,每个 (s1,s2)(s_1, s_2) 只有一次机会。因为 (t1,t2)(t_1, t_2) 肯定是前边后边一样,中间一段不一样的。(s1,s2)(s_1, s_2) 必须长得和这个 (t1,t2)(t_1, t_2) 差不多。可以见下图:

所以,我们把 s,ts, t 都拆成 LCP 和剩下那段,称为 xx 和 yy。然后,你要做的就是,对于每个询问,求出 xx 和 yy 都是 tt 的 xx 和 yy 的前缀的 ss 的数量。

当然,可能有 ss 的 yy 匹配 tt 的 yy 到一半,没把中间部分匹配完的情况,这个情况可以记录中间那段的长度解决。

考虑这件事情怎么做。前缀这回事可以用 trie。所以我们先把 yy 塞到 trie 里。此时查询的形式是,对于 trie 上的到根链,求 xx 被 tt 的 xx 当前缀一类。此时是最精彩的一部分,我们离线,把 tt 的 x,yx, y 预处理出来,yy 也插到 trie 里面,对 trie 进行 dfs,第一次访问时插入 xx 到另一棵 trie 里并处理询问,回溯时把 xx 删掉。就可以避免重复查询,优化复杂度。

各种做法中这个代码应该算是比较好写的一种,2k 多一点的样子。

C++
#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using vi = vector < int >;
using pii = pair < int, int >;
#define pb push_back
#define sz(x) ((int)(x).size())
#define all(a) (a).begin(), (a).end()
#define qio() cin.tie(0) -> sync_with_stdio(0)
#define L(i, j, k) for (int i = j; i <= int(k); i++)
#define R(i, j, k) for (int i = j; i >= int(k); i--)

const int N = 2e5 + 5, L = 5e6 + 6, M = L << 1, S = 26;

struct node {
	string pre, sufs, suft;
	int len;
};
struct trie {
	int tot, ch[M][S], cnt[M];
	void modify(string s, int dt, int u = 0) {
		for (auto w : s) {
			int c = w - 'a';
			if (!ch[u][c]) ch[u][c] = ++tot;
			u = ch[u][c];
		}
		cnt[u] += dt;
	}
	int query(string s, int u = 0, int res = 0) {
		for (auto w : s) {
			int c = w - 'a';
			res += cnt[u];
			u = ch[u][c];
			if (!u) return res;
		}
		res += cnt[u];
		return res;
	}
} tr;

int get(char x, char y) {
	return (x - 'a') * 26 + y - 'a';
}

int n, q, tot, ans[N], rt[L];
map < int, int > ch[M];
vi Q[M];
string prep[N], preq[N];

node solve(string s, string t) {
	int P = 0, S = sz(s) - 1;
	string pre = "", sufs = "", suft = "";
	for (; s[P] == t[P]; pre += s[P++]);
	reverse(all(pre));
	for (; s[S] == t[S]; S--);
	L(i, P, sz(s) - 1) sufs += s[i], suft += t[i];
	return {pre, sufs, suft, S - P + 1};
}

inline void insert(int& u, int pos, string& s, string& t, int id) {
	if (!u) u = ++tot;
	if (pos == sz(s)) {
		Q[u].pb(id);
		return;
	}
	insert(ch[u][get(s[pos], t[pos])], pos + 1, s, t, id);
}
inline void dfs(int u) {
	if (!u) return;
	for (auto id : Q[u]) {
		if (id <= n) tr.modify(prep[id], 1);
		else ans[id - n] = tr.query(preq[id - n]);
	}
	for (auto [key, v] : ch[u]) dfs(v);
	for (auto id : Q[u]) {
		if (id <= n) tr.modify(prep[id], -1);
	}
}

int main() {
	qio(), cin >> n >> q;
	L(i, 1, n) {
		string s, t;
		cin >> s >> t;
		if (s == t) continue;
		auto [pre, sufs, suft, len] = solve(s, t);
		prep[i] = pre;
		insert(rt[len], 0, sufs, suft, i);
	}
	L(i, 1, q) {
		string s, t;
		cin >> s >> t;
		if (sz(s) != sz(t)) continue; // ... 
		auto [pre, sufs, suft, len] = solve(s, t);
		preq[i] = pre;
		insert(rt[len], 0, sufs, suft, i + n);
	}
	L(i, 1, L - 1) dfs(rt[i]);
	L(i, 1, q) cout << ans[i] << '\n';
	return 0;
}