版本比较
CSP-S2 2025游记 & 题解
正文
增加 107 行删除 1 行
360 行未改动全部展开36136236336436536536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447588 行未改动360 行未改动全部展开 work(); return 0;}```关于此题 AC 自动机做法:把一组谐音串:`abcijklxyz, abcqwerxyz` 压成 `abc#ijklqwer#xyz`。询问串同理。然后就是 ACAM + BIT?好像是 阿狸的打字机 那道题。改天做做。关于此题 AC 自动机做法:把一组谐音串:`abcijklxyz, abcqwerxyz` 压成 `abc#ijklqwer#xyz`。询问串同理。然后就是 ACAM 板子。只需要查询询问串跳到的点到根的路径上有多少文本串就行了。(可以暴力调,复杂度是 $O(\sqrt {|L|})$ 的;直接预处理就是 $O(1)$ 查)复杂度为 $O(26 \sum |L|)$。宇宙无敌好写,但是跑的超级慢()```#include <iostream>#include <cstring>#include <cstdio>#include <vector>#include <algorithm>#include <queue>using namespace std;const int LL = 1.1e7 + 10;inline int cg(char ch){ if (ch == '#') return 26; return ch - 'a';}struct Trie{ int g[30][LL], fail[LL], sum[LL], tot; inline void ins(string s) { int u = 0; for (char ch : s) { if (!g[cg(ch)][u]) g[cg(ch)][u] = ++tot; u = g[cg(ch)][u]; } sum[u]++; } inline int calc(string s) { int ret = 0, u = 0; for (char ch : s) u = g[cg(ch)][u], ret += sum[u]; return ret; } inline void bfs() { queue<int> q; for (int i = 0; i <= 26; i++) if (g[i][0]) q.push(g[i][0]); while (!q.empty()) { int u = q.front(); q.pop(); for (int i = 0; i <= 26; i++) if (g[i][u]) fail[g[i][u]] = g[i][fail[u]], sum[g[i][u]] += sum[fail[g[i][u]]], q.push(g[i][u]); else g[i][u] = g[i][fail[u]]; } }}trie;int main(){ ios::sync_with_stdio(0); cin.tie(0); int n, q; cin >> n >> q; for (int i = 1; i <= n; i++) { string s1, s2; cin >> s1 >> s2; int len = s1.size(); s1 = " " + s1, s2 = " " + s2; int bg = len + 1, ed = len; for (int j = 1; j <= len; j++) if (s1[j] != s2[j]) { if (bg == len + 1) bg = j; ed = j; } string ans = ""; for (int j = 1; j < bg; j++) ans += s1[j]; ans += "#"; for (int j = bg; j <= ed; j++) ans += s1[j], ans += s2[j]; ans += "#"; for (int j = ed + 1; j <= len; j++) ans += s1[j]; trie.ins(ans); } trie.bfs(); while (q--) { string s1, s2; cin >> s1 >> s2; if (s1.size() != s2.size()) { cout << "0\n"; continue; } int len = s1.size(); s1 = " " + s1, s2 = " " + s2; int bg = len + 1, ed = len; for (int j = 1; j <= len; j++) if (s1[j] != s2[j]) { if (bg == len + 1) bg = j; ed = j; } string ans = ""; for (int j = 1; j < bg; j++) ans += s1[j]; ans += "#"; for (int j = bg; j <= ed; j++) ans += s1[j], ans += s2[j]; ans += "#"; for (int j = ed + 1; j <= len; j++) ans += s1[j]; cout << trie.calc(ans) << "\n"; } return 0;}```## T4好困难()场上觉得 A 性质不是很可做就没思考太久,现在发现是明智的。
考虑状态 $f_{i, j, k}$ 代表考虑到第 $i$ 个位置,$j$ 个人已经被淘汰了;选了的 $c \le j$ 的人有 $k$ 个且**分配好这些人出现的位置但没给 $c > j$ 的人分配具体值**的方案数。88 行未改动