hash一下, 把原矩阵所有A*B的子矩阵的hash值存在set里面, 然后对于每个询问就求出hash值, 在set中查找.

------------------------------------------------------------------------

#include<cstdio>
#include<cstring>
#include<set>
#include<algorithm>
 
using namespace std;
 
typedef unsigned long long ull;
 
const int maxn = 1009;
const int p = 31, _p = 59;
 
char str[maxn];
int N, M, n, m;
ull h[maxn][maxn], Power, H[maxn];
set<int> S;
 
ull get(int x, int y) {
return h[x][y] - h[x][y + m] * Power;
}
 
void init() {
scanf("%d%d%d%d", &N, &M, &n, &m);
for(int i = 0; i < N; i++) {
scanf("%s", str);
h[i][M] = 0;
for(int j = M; j--; )
h[i][j] = h[i][j + 1] * p + str[j];
}
Power = 1;
for(int i = 1; i <= m; i++) Power = Power * p;
S.clear();
for(int i = N - n; i >= 0; i--)
for(int j = M - m; j >= 0; j--) {
ull t = 0;
for(int k = i; k < i + n; k++) t = t * _p + get(k, j);
S.insert(t);
}
}
 
int main() {
init();
int Q; scanf("%d", &Q);
while(Q--) {
ull res = 0;
for(int i = 0; i < n; i++) {
ull H = 0;
scanf("%s", str);
for(int j = m; j--; )
H = H * p + str[j];
res = res * _p + H;
}
printf("%d\n", S.find(res) != S.end());
}
return 0;
}

------------------------------------------------------------------------

2351: [BeiJing2011]Matrix

Time Limit: 20 Sec  Memory Limit: 128 MB
Submit: 705  Solved: 200
[Submit][Status][Discuss]

Description

给定一个M行N列的01矩阵,以及Q个A行B列的01矩阵,你需要求出这Q个矩阵哪些在原矩阵中出现过。
所谓01矩阵,就是矩阵中所有元素不是0就是1。

Input

输入文件的第一行为M、N、A、B,参见题目描述。
接下来M行,每行N个字符,非0即1,描述原矩阵。
接下来一行为你要处理的询问数Q。
接下来Q个矩阵,一共Q*A行,每行B个字符,描述Q个01矩阵。

Output

你需要输出Q行,每行为0或者1,表示这个矩阵是否出现过,0表示没有出现过,1表示出现过。

Sample Input

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

Sample Output

1
0
1

HINT

对于100%的实际测试数据,M、N ≤ 1000,Q = 1000

对于40%的数据,A = 1。

对于80%的数据,A ≤ 10。

对于100%的数据,A ≤ 100。

Source

2351: [BeiJing2011]Matrix( hash )的更多相关文章

  1. bzoj 2351 [BeiJing2011]Matrix——二维哈希

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2351 就是先把每行单独从左到右扫着乘一个 b1 哈希起来,然后再按列从上往下乘一个 b2 哈 ...

  2. BZOJ2351: [BeiJing2011]Matrix

    2351: [BeiJing2011]Matrix Time Limit: 20 Sec  Memory Limit: 128 MBSubmit: 589  Solved: 171[Submit][S ...

  3. 【BZOJ 2351】Matrix(Hash)

    题目链接 二维\(Hash\)类似二维前缀和,每一行看成一个\(h\)进制数,每一个以(1,1)为左上角的矩阵看成一个由每一行的\(Hash\)值组成的\(l\)进制数. 然后自己推推柿子就行. #i ...

  4. BZOJ2351[BeiJing2011]Matrix——二维hash

    题目描述 给定一个M行N列的01矩阵,以及Q个A行B列的01矩阵,你需要求出这Q个矩阵哪些在原矩阵中出现过.所谓01矩阵,就是矩阵中所有元素不是0就是1. 输入 输入文件的第一行为M.N.A.B,参见 ...

  5. 【bzoj2351】[BeiJing2011]Matrix 二维Hash

    题目描述 给定一个M行N列的01矩阵,以及Q个A行B列的01矩阵,你需要求出这Q个矩阵哪些在原矩阵中出现过.所谓01矩阵,就是矩阵中所有元素不是0就是1. 输入 输入文件的第一行为M.N.A.B,参见 ...

  6. 【BZOJ 2351】 Matrix

    [题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=2351 [算法] 哈希 [代码] #include<bits/stdc++.h& ...

  7. 【矩阵哈希】【哈希表】bzoj2351 [BeiJing2011]Matrix

    引用题解:http://blog.csdn.net/popoqqq/article/details/41084047 #include<cstdio> #include<cstrin ...

  8. bzoj AC倒序

    Search GO 说明:输入题号直接进入相应题目,如需搜索含数字的题目,请在关键词前加单引号 Problem ID Title Source AC Submit Y 1000 A+B Problem ...

  9. PHP-密码学算法及其应用-散列函数

    转自http://www.smatrix.org/bbs/simple/index.php?t5591.html /////////////////////////////////////////// ...

随机推荐

  1. Angularjs基础教程

    Angularjs-基础教程 一些angualr的基础概念,适合入门. 1.下载 推荐 bower 或 npm 安装. bower install angular bower install angu ...

  2. 半模对话框 QProgressDialog

    http://doc.qt.io/qt-4.8/qprogressdialog.html progressdialog 用到了qfuture http://blog.csdn.net/liang198 ...

  3. android HTTP发送及MD5加密收集

    发送部分: public void MyFunction{ HttpClient httpclient = new DefaultHttpClient(); //你的URL HttpPost http ...

  4. Codeforces 700A As Fast As Possible(二分答案)

    [题目链接] http://codeforces.com/problemset/problem/700/A [题目大意] 有一辆限载k人速度为v2的车,n个步行速度均为v1的人要通过一段长度为l的距离 ...

  5. 蓝桥杯之FBI树问题

    问题描述 我们可以把由"0"和"1"组成的字符串分为三类:全"0"串称为B串,全"1"串称为I串,既含"0&q ...

  6. To the Max(矩阵压缩)

    To the Max Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 20000/10000K (Java/Other) Total Su ...

  7. 编写可维护的JS 02

    2.注释 单行 //单行注释 多行 /* 多行注释 */ /** * 多行注释 * */ 使用注释 使用注释的原则是让代码更清晰 难于理解的代码 难于理解的代码都应添加注释 可能被误认为错误的代码 应 ...

  8. Linux Shell 学习笔记 一 目录结构

    以Red Hat Enterprise Linux 各版本为例,RHEL中目录具体作用如下, /bin       存放普通用户使用的命令 /sbin     存放管理员可以执行的命令 /home   ...

  9. 获取当前WEB应用全路径

    <%String path = request.getContextPath();String basePath =request.getScheme()+"://"+req ...

  10. 删除链表的中间节点和a/b处节点

    [说明]: 本文是左程云老师所著的<程序员面试代码指南>第二章中“删除链表的中间节点和a/b处节点”这一题目的C++复现. 本文只包含问题描述.C++代码的实现以及简单的思路,不包含解析说 ...