CH1806 Matrix
题意
描述
给定一个M行N列的01矩阵(只包含数字0或1的矩阵),再执行Q次询问,每次询问给出一个A行B列的01矩阵,求该矩阵是否在原矩阵中出现过。
输入格式
第一行四个整数M,N,A,B。
接下来一个M行N列的01矩阵,数字之间没有空格。
接下来一个整数Q。
接下来Q个A行B列的01矩阵,数字之间没有空格。
输出格式
对于每个询问,输出1表示出现过,0表示没有。
样例输入
3 3 2 2
111
000
111
3
11
00
11
11
00
11
样例输出
1
0
1
数据范围与约定
对于40%的数据,A = 1。
对于80%的数据,A ≤ 10。
对于100%的数据,A ≤ 100,M, N ≤ 1000,Q ≤ 1000。
来源
CCF NOI2011 北京市选
分析
矩阵hash,原来hash可以像二维前缀和一样处理。对行,列分别看成进制数,运算与二维前缀和类似。
时间复杂度\(O(M N + Q)\)
代码
#include<bits/stdc++.h>
#define rg register
#define il inline
#define co const
template<class T>il T read(){
rg T data=0,w=1;
rg char ch=getchar();
while(!isdigit(ch)){
if(ch=='-') w=-1;
ch=getchar();
}
while(isdigit(ch))
data=data*10+ch-'0',ch=getchar();
return data*w;
}
template<class T>il T read(rg T&x){
return x=read<T>();
}
typedef unsigned long long ULL;
co int maxn=1002,pc=131,ppc=13331,mod=maxn*maxn;
ULL p1[maxn],p2[maxn],sum[maxn][maxn];
int fa,adj[mod];
char b[maxn][maxn];
struct my{
ULL zhi;
int next;
}bian[maxn*maxn];
void hash(ULL u){
int x=u%mod;
bian[++fa].zhi=u,bian[fa].next=adj[x],adj[x]=fa;
}
bool ask(ULL u){
int x=u%mod;
for(int i=adj[x];i;i=bian[i].next)
if(bian[i].zhi==u) return 1;
return 0;
}
int main(){
// freopen(".in","r",stdin);
// freopen(".out","w",stdout);
int m,n,r,c,q;
read(m),read(n),read(r),read(c);
for(int i=1;i<=m;++i)
scanf("%s",b[i]+1);
read(q);
for(int i=1;i<=m;++i)
for(int j=1;j<=n;++j)
sum[i][j]=b[i][j]-'0';
for(int i=1;i<=m;++i)
for(int j=1;j<=n;++j)
sum[i][j]+=sum[i-1][j]*pc;
for(int i=1;i<=m;++i)
for(int j=1;j<=n;++j)
sum[i][j]+=sum[i][j-1]*ppc;
p1[0]=p2[0]=1;
for(int i=1;i<=r;++i)
p1[i]=p1[i-1]*pc;
for(int j=1;j<=c;++j)
p2[j]=p2[j-1]*ppc;
for(int i=r;i<=m;++i)
for(int j=c;j<=n;++j)
hash(sum[i][j]-sum[i-r][j]*p1[r]-sum[i][j-c]*p2[c]+sum[i-r][j-c]*p1[r]*p2[c]);
while(q--){
for(int i=1;i<=r;++i)
scanf("%s",b[i]+1);
for(int i=1;i<=r;++i)
for(int j=1;j<=c;++j)
sum[i][j]=b[i][j]-'0';
for(int i=1;i<=r;++i)
for(int j=1;j<=c;++j) sum[i][j]+=sum[i-1][j]*pc;
for(int i=1;i<=r;++i)
for(int j=1;j<=c;++j) sum[i][j]+=sum[i][j-1]*ppc;
puts(ask(sum[r][c])?"1":"0");
}
return 0;
}
CH1806 Matrix的更多相关文章
- angular2系列教程(十一)路由嵌套、路由生命周期、matrix URL notation
今天我们要讲的是ng2的路由的第二部分,包括路由嵌套.路由生命周期等知识点. 例子 例子仍然是上节课的例子:
- Pramp mock interview (4th practice): Matrix Spiral Print
March 16, 2016 Problem statement:Given a 2D array (matrix) named M, print all items of M in a spiral ...
- Atitit Data Matrix dm码的原理与特点
Atitit Data Matrix dm码的原理与特点 Datamatrix原名Datacode,由美国国际资料公司(International Data Matrix, 简称ID Matrix)于 ...
- Android笔记——Matrix
转自:http://www.cnblogs.com/qiengo/archive/2012/06/30/2570874.html#translate Matrix的数学原理 在Android中,如果你 ...
- 通过Matrix进行二维图形仿射变换
Affine Transformation是一种二维坐标到二维坐标之间的线性变换,保持二维图形的"平直性"和"平行性".仿射变换可以通过一系列的原子变换的复合来 ...
- [LeetCode] Kth Smallest Element in a Sorted Matrix 有序矩阵中第K小的元素
Given a n x n matrix where each of the rows and columns are sorted in ascending order, find the kth ...
- [LeetCode] Longest Increasing Path in a Matrix 矩阵中的最长递增路径
Given an integer matrix, find the length of the longest increasing path. From each cell, you can eit ...
- [LeetCode] Search a 2D Matrix II 搜索一个二维矩阵之二
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...
- [LeetCode] Search a 2D Matrix 搜索一个二维矩阵
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...
随机推荐
- rails性能优化
1,使用Unicorn或者Thin服务器替代默认的webrick.2,静态资源压缩合并,放到云存储上.3,同时可以使用rails的Turbolinks,使用js替换title和body,但也带来了js ...
- FlexPaper_1.2.1.swc——Flex在线显示PDF文档(使用FlexPaper)感悟
http://www.cnblogs.com/wuhenke/archive/2010/03/16/1686885.html 想想自己先前搞PDF转SWF,然后在线浏览功能时,实在是费了不少精力.后来 ...
- Android Media (Audio) Framework 多媒体系统框架
http://blog.csdn.net/lskshz/article/details/17264113 原址:http://blog.csdn.net/myzhzygh/article/detail ...
- RPC和REST
什么是RPC? 是指远程过程调用,就是两个服务A.B,一个应用部署在A服务器上,想要调用B服务器上应用提供的函数/方法,由于不在一个内存空间,不能直接调用,需要通过网络来表达调用的语义和传达调用的数据 ...
- Git命令速查表【转】
本文转载自:http://www.cnblogs.com/kenshinobiy/p/4543976.html 一. Git 常用命令速查 git branch 查看本地所有分支git status ...
- UVA 11019 Matrix Matcher(二维hash + 尺取)题解
题意:在n*m方格中找有几个x*y矩阵. 思路:二维hash,总体思路和一维差不太多,先把每行hash,变成一维的数组,再对这个一维数组hash变成二维hash.之前还在想怎么快速把一个矩阵的hash ...
- 2017NOIP模拟赛-科普基地
今天回来打的第一场NOIP难度的试题,结果惨不忍睹.写一下每道题的做法,然后每道题犯的__弱智__错误 UPD:2018.9.15 突然这篇题解就变成很多大佬要看的了,因为之前是写给自己看的,所以写的 ...
- HDU 3315 My Brute(二分图最佳匹配+尽量保持原先匹配)
http://acm.hdu.edu.cn/showproblem.php?pid=3315 题意: 有S1到Sn这n个勇士要和X1到Xn这n个勇士决斗,初始时,Si的决斗对象是Xi. 如果Si赢了X ...
- spring boot: 热部署(一) run as – java application (spring-loader-1.2.4.RELEASE.jar)
spring boot: 热部署(一) run as – java application (spring-loader-1.2.4.RELEASE.jar) 如果使用的run as – java a ...
- Spring的JdbcTemplate实现分页
PageList.java实体类 /** * 封装分页对象 **/ public class PageList { private int page; //当前页 private int totalR ...