跳到正文

个人记录

题解

bz02约 3 分钟
原文

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

文章信息与版本

文章信息

收录
2026年2月22日
更新
2026年3月3日
篇幅
约 3 分钟

版本

  1. v2当前
  2. v1

题目传送门

题目传送门

题解

5分

直接暴力匹配。

不给出代码。

100分(AC)

可以发现,我们改一位的总共数量对于每个字符串来说长度是有限的,所以,将每个字符串可以与其近似匹配的字符串都哈希后记录下来然后在每个子串哈希后匹配即可。

代码见下(100分)(AC):

C++
#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;
}