# [BZOJ2462][BeiJing2011]矩阵模板

## Description

所谓01矩阵，就是矩阵中所有元素不是0就是1。

3 3 2 2
111
000
111
3
11
00
11
11
00
11

## Sample Output

1
0
1
A,B<=1

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
using namespace std;
#define reg register
inline int read() {
int res = 0;char ch=getchar();bool fu=0;
while(!isdigit(ch))fu|=(ch=='-'), ch=getchar();
while(isdigit(ch))res=(res<<3)+(res<<1)+(ch^48), ch=getchar();
return fu?-res:res;
}
#define N 100005
#define mod 1000006
int n, m, A, B;
unsigned long long hsh[1005][1005], kp1[1005], kp2[1005];
int a[1005][1005];
char str[1005];
bool mp[1000006];

int main()
{
n = read(), m = read(), A = read(), B = read();
for (reg int i = 1 ; i <= n ; i ++) {
scanf("%s", str + 1);
for (reg int j = 1 ; j <= m ; j ++)
a[i][j] = str[j] - '0';
}

kp1[0] = kp2[0] = 1;
for (reg int i = 1 ; i <= 100 ; i ++) kp1[i] = kp1[i - 1] * 233, kp2[i] = kp2[i - 1] * 313;
for (reg int i = 1 ; i <= n ; i ++)
for (reg int j = 1 ; j <= m ; j ++)
hsh[i][j] = hsh[i - 1][j] * 233 + a[i][j];
for (reg int i = 1 ; i <= n ; i ++)
for (reg int j = 1 ; j <= m ; j ++)
hsh[i][j] += hsh[i][j - 1] * 313;
for (reg int i = A ; i <= n ; i ++)
for (reg int j = B ; j <= m ; j ++) {
unsigned long long tmp = 0;
tmp = hsh[i][j] - hsh[i - A][j] * kp1[A] - hsh[i][j - B] * kp2[B] + hsh[i - A][j - B] * kp1[A] * kp2[B];
mp[tmp%mod] = 1;
}
int q = read();
while(q--)
{
for (reg int i = 1 ; i <= A ; i ++) {
scanf("%s", str + 1);
for (reg int j = 1 ; j <= B ; j ++)
a[i][j] = str[j] - '0';
}
memset(hsh, 0, sizeof hsh);
for (reg int i = 1 ; i <= A ; i ++)
for (reg int j = 1 ; j <= B ; j ++)
hsh[i][j] = hsh[i - 1][j] * 233 + a[i][j];
for (reg int i = 1 ; i <= A ; i ++)
for (reg int j = 1 ; j <= B ; j ++)
hsh[i][j] += hsh[i][j - 1] * 313;
puts(mp[hsh[A][B] % mod] ? "1" : "0");
}
return 0;
}

00

posted @ 2018-10-18 15:42  zZhBr  阅读(...)  评论(...编辑  收藏