forked from ShahjalalShohag/code-library
-
Notifications
You must be signed in to change notification settings - Fork 0
/
String Hashing 2D.cpp
74 lines (72 loc) · 2.11 KB
/
String Hashing 2D.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include<bits/stdc++.h>
using namespace std;
const int N = 3e5 + 9;
struct Hashing {
vector<vector<int>> hs;
vector<int> PWX, PWY;
int n, m;
static const int PX = 3731, PY = 2999, mod = 998244353;
Hashing() {}
Hashing(vector<string>& s) {
n = (int)s.size(), m = (int)s[0].size();
hs.assign(n + 1, vector<int>(m + 1, 0));
PWX.assign(n + 1, 1);
PWY.assign(m + 1, 1);
for (int i = 0; i < n; i++) PWX[i + 1] = 1LL * PWX[i] * PX % mod;
for (int i = 0; i < m; i++) PWY[i + 1] = 1LL * PWY[i] * PY % mod;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
hs[i + 1][j + 1] = s[i][j] - 'a' + 1;
}
}
for (int i = 0; i <= n; i++) {
for (int j = 0; j < m; j++) {
hs[i][j + 1] = (hs[i][j + 1] + 1LL * hs[i][j] * PY % mod) % mod;
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j <= m; j++) {
hs[i + 1][j] = (hs[i + 1][j] + 1LL * hs[i][j] * PX % mod) % mod;
}
}
}
int get_hash(int x1, int y1, int x2, int y2) { // 1-indexed
assert(1 <= x1 && x1 <= x2 && x2 <= n);
assert(1 <= y1 && y1 <= y2 && y2 <= m);
x1--;
y1--;
int dx = x2 - x1, dy = y2 - y1;
return (1LL * (hs[x2][y2] - 1LL * hs[x2][y1] * PWY[dy] % mod + mod) % mod -
1LL * (hs[x1][y2] - 1LL * hs[x1][y1] * PWY[dy] % mod + mod) % mod * PWX[dx] % mod + mod) % mod;
}
int get_hash() {
return get_hash(1, 1, n, m);
}
};
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int t;
cin >> t;
while (t--) {
int n, m;
cin >> n >> m;
vector<string> a(n);
for (int i = 0; i < n; i++) cin >> a[i];
Hashing H(a);
int x, y;
cin >> x >> y;
vector<string> b(x);
for (int i = 0; i < x; i++) cin >> b[i];
auto z = Hashing(b).get_hash();
int ans = 0;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if (i + x - 1 <= n && j + y - 1 <= m && H.get_hash(i, j, i + x - 1, j + y - 1) == z) ans++;
}
}
cout << ans << '\n';
}
return 0;
}
// https://vjudge.net/problem/UVA-11019