BZOJ2462: [BeiJing2011]矩阵模板(二维hash)
题意
Sol
二维矩阵hash,就是对行和列分配一个不同的base,然后分别做一遍hash,这样会减少冲突的概率。
预处理出所有大小为\(A \times B\)的矩阵的hash值,判断一下即可
mdzz居然卡常数
#include<bits/stdc++.h>
#define ull unsigned int
using namespace std;
const int MAXN = 1010, mod = 100000007;
const ull base1 = 19260817, base2 = 998244353;
inline int read() {
int x = 0, f = 1; char c = getchar();
while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
return x * f;
}
int N, M, A, B;
ull po1[MAXN], po2[MAXN], m[MAXN][MAXN], a[MAXN][MAXN];
bool ha[mod + 1];
int readch() {
char c = '.';
while(c != '0' && c != '1') c = getchar();
return c;
}
main() {
//freopen("1.in", "r", stdin);
N = read(); M = read(); A = read(); B = read();
po1[0] = 1; for(int i = 1; i <= N; i++) po1[i] = po1[i - 1] * base1;
po2[0] = 1; for(int i = 1; i <= M; i++) po2[i] = po2[i - 1] * base2;
for(int i = 1; i <= N; i++) for(int j = 1; j <= M; j++) m[i][j] = readch();
for(int i = 1; i <= N; i++)
for(int j = 1; j <= M; j++)
m[i][j] += m[i - 1][j] * base1;
for(int i = 1; i <= N; i++)
for(int j = 1; j <= M; j++)
m[i][j] += m[i][j - 1] * base2;
for(int i = A; i <= N; i++) {
for(int j = B; j <= M; j++) {
ull tmp = m[i][j] - m[i - A][j] * po1[A] - m[i][j - B] * po2[B] + m[i - A][j - B] * po1[A] * po2[B];
ha[tmp % mod] = 1;
}
}
int Q = read();
while(Q--) {
for(int i = 1; i <= A; i++) for(int j = 1; j <= B; j++) a[i][j] = readch();
for(int i = 1; i <= A; i++)
for(int j = 1; j <= B; j++)
a[i][j] += a[i - 1][j] * base1;
for(int i = 1; i <= A; i++)
for(int j = 1; j <= B; j++)
a[i][j] += a[i][j - 1] * base2;
putchar(ha[a[A][B] % mod] ? '1' : '0'); putchar('\n');
}
return 0;
}
作者:自为风月马前卒
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。