标题
增加 1 行删除 1 行
1题解1「LKJOI R1 B」近似匹配 题解
正文
增加 1 行删除 70 行
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970# 题目传送门[题目传送门]()# 题解## 5分直接暴力匹配。不给出代码。## 100分(AC)可以发现,我们改一位的总共数量对于每个字符串来说长度是有限的,所以,将每个字符串可以与其近似匹配的字符串都哈希后记录下来然后在每个子串哈希后匹配即可。代码见下(100分)(AC):```cpp#include <bits/stdc++.h>#include <ext/pb_ds/assoc_container.hpp>using namespace std;using namespace __gnu_pbds;using u64 = unsigned long long;const u64 N = 5e5 + 5, B = 11;u64 n, m, k, hsh[N], pw[N], ans;string s, t;gp_hash_table<u64, null_type> mp;u64 gethsh(u64 l, u64 r) {return hsh[r] - hsh[l - 1] * pw[r - l + 1];}int main() {ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);cin >> n >> m >> k >> s, s = ' ' + s;pw[0] = 1;for (u64 i = 1; i <= n; ++i) {hsh[i] = (1ull * hsh[i - 1] * B + (s[i] - '0' + 1));}for (u64 i = 1; i <= max(n, k); ++i) {pw[i] = 1ull * pw[i - 1] * B;}while (m--) {cin >> t, t = ' ' + t;u64 sum = 0;for (u64 i = 1; i <= k; ++i) {sum = (1ull * sum * B + (t[i] - '0' + 1));}mp.insert(sum);for (u64 i = 1; i <= k; ++i) {u64 now = sum - 1ull * pw[k - i] * (t[i] - '0' + 1);for (u64 j = 0; j < 10; ++j) {if (j == t[i] - '0') continue;u64 cur = now + 1ull * pw[k - i] * (j + 1);mp.insert(cur);}}}for (u64 i = 1; i <= n - k + 1; ++i)if (mp.find(gethsh(i, i + k - 1)) != mp.end()) {++ans;}cout << ans;return 0;}```1X