Matrix
Matrix
https://ac.nowcoder.com/acm/problem/51003
很显然的一个二维矩阵哈希,就是有点卡内存。
离线处理即可。
#include <bits/stdc++.h> #include <unordered_map> using namespace std; typedef long long ll; typedef unsigned long long ull; #ifdef LOCAL #define debug(x) cout << "[" __FUNCTION__ ": " #x " = " << (x) << "]\n" #define TIME cout << "RuningTime: " << clock() << "ms\n", 0 #else #define TIME 0 #endif #define hash_ 1000000009 #define hash_1 998244353 #define gc p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1000000, stdin), p1 == p2) ? EOF : *p1++; inline int read() { static char buf[1000000], *p1 = buf, *p2 = buf; int x = false; char ch = gc; bool sgn = false; while (ch != '-' && (ch < '0' || ch > '9')) ch = gc; if (ch == '-') sgn = true, ch = gc; while (ch >= '0'&& ch <= '9') x = (x << 1) + (x << 3) + (ch ^ 48), ch = gc; return sgn ? -x : x; } template<class T> T gcd(T a, T b) { return b == 0 ? a : gcd(b, a % b); }; ll fpow(ll a, ll b, int mod) { ll res = 1; for (; b > 0; b >>= 1) { if (b & 1) res = res * a % mod; a = a * a % mod; } return res; } const int mod = 1e9 + 7; const int INF = 0x3f3f3f3f; const ll LINF = 0x3f3f3f3f3f3f3f3f; const int N = 1e3 + 10; char s[N][N]; ull b[N]; int a[N][N]; int base[N], base1[N]; int ask[N]; int main() { #ifdef LOCAL freopen("E:/input.txt", "r", stdin); #endif int n, m, u, v; scanf("%d%d%d%d", &n, &m, &u, &v); base[0] = base1[0] = 1; for (int i = 1; i <= max(n, m); i++) base[i] = 1LL * base[i - 1] * hash_ % mod, base1[i] = 1LL * base1[i - 1] * hash_1 % mod; for (int i = 1; i <= n; i++) scanf("%s", s[i] + 1); for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) b[j] = (1LL * b[j - 1] * hash_ % mod + s[i][j]) % mod; for (int j = v; j <= m; j++) a[i][j] = (1LL * a[i - 1][j] * hash_1 % mod + 1LL * b[j] - 1LL * b[j - v] * base[v] % mod + mod) % mod; } unordered_map<int, int>mp1, mp2; int q; cin >> q; for (int i = 1; i <= q; i++) { ll now = 0; for (int i = 1; i <= u; i++) { scanf("%s", s[i] + 1); ll tmp = 0; for (int j = 1; j <= v; j++) tmp = (tmp * hash_ % mod + s[i][j]) % mod; now = (now * hash_1 % mod + tmp) % mod; } ask[i] = now; mp1[now] = 1; } for (int i = u; i <= n; i++) for (int j = v; j <= m; j++) { ll res = (1LL * a[i][j] - 1LL * a[i - u][j] * base1[u] % mod + mod) % mod; if (mp1.find(res) != mp1.end()) mp2[res] = 1; } for (int i = 1; i <= q; i++) cout << (mp2.find(ask[i]) != mp2.end()) << endl; return TIME; }