https://www.acwing.com/problem/content/158/
#include <iostream> #include <algorithm> #include <unordered_set> using namespace std; typedef unsigned long long ULL; const int N = 1010, M = N * N, P = 131; int n, m, a, b; ULL h[N][N], power[M]; char str[N]; unordered_set<ULL> S; void init() { power[0] = 1; for (int i = 1; i <= m * n; i ++ ) power[i] = power[i - 1] * P; for (int i = 1; i <= m; i ++ ) { scanf("%s", str + 1); for (int j = 1; j <= n; j ++ ) h[i][j] = h[i][j - 1] * P + str[j] - '0'; } } ULL calc(ULL f[], int l, int r) { return f[r] - f[l - 1] * power[r - l + 1]; } void populateSet() { for (int r = b; r <= n; r ++ ) { int l = r - b + 1; ULL s = 0; for (int j = 1; j <= m; j ++ ) // bottom { s = s * power[b] + calc(h[j], l, r); if (j - a > 0) s -= calc(h[j - a], l, r) * power[a * b]; if (j >= a) S.insert(s); } } } int main() { scanf("%d%d%d%d", &m, &n, &a, &b); init(); populateSet(); int Q; scanf("%d", &Q); while (Q -- ) { ULL s = 0; for (int i = 0; i < a; i ++ ) { scanf("%s", str); for (int j = 0; j < b; j ++ ) s = s * P + str[j] - '0'; } if (S.count(s)) puts("1"); else puts("0"); } return 0; }