版本比较
「LKJOI R1 B」近似匹配 题解
标题
增加 1 行删除 1 行
11题解「LKJOI R1 B」近似匹配 题解正文
增加 1 行删除 70 行
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869701# 题目传送门
[题目传送门]()
# 题解
## 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;}```X