题意

描述

给定一个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的更多相关文章

  1. angular2系列教程(十一)路由嵌套、路由生命周期、matrix URL notation

    今天我们要讲的是ng2的路由的第二部分,包括路由嵌套.路由生命周期等知识点. 例子 例子仍然是上节课的例子:

  2. 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 ...

  3. Atitit Data Matrix dm码的原理与特点

    Atitit Data Matrix dm码的原理与特点 Datamatrix原名Datacode,由美国国际资料公司(International Data Matrix, 简称ID Matrix)于 ...

  4. Android笔记——Matrix

    转自:http://www.cnblogs.com/qiengo/archive/2012/06/30/2570874.html#translate Matrix的数学原理 在Android中,如果你 ...

  5. 通过Matrix进行二维图形仿射变换

    Affine Transformation是一种二维坐标到二维坐标之间的线性变换,保持二维图形的"平直性"和"平行性".仿射变换可以通过一系列的原子变换的复合来 ...

  6. [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 ...

  7. [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 ...

  8. [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 ...

  9. [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 ...

随机推荐

  1. 2016 CCPC 长春 Solution

    A - Hanzo vs. Genji 留坑. B - Fraction 水. #include <bits/stdc++.h> using namespace std; inline i ...

  2. hdu5141 线段树

    这题说的是给了一串然后计算出这个串的最长递增子序列的长度,然后计算出有过少个子串[i,j] 他们的最长递增子序列和这整个子串的最长递增子序列相同,我们对于每个j最长递增子序列找出他在序列中的使成为最长 ...

  3. 883. Projection Area of 3D Shapes

    问题 NxN个格子中,用1x1x1的立方体堆叠,grid[i][j]表示坐标格上堆叠的立方体个数,求三视图面积. Input: [[1,2],[3,4]] Output: 17 Explanation ...

  4. Flask Web 开发 错误页面自定义

    如果要自定义错误画面,无法大多数情况是自定义404和500的情况 首先,要在路由中设置 通过app.error_handler这个装饰器来绑定响应函数 @main.errorhandler(404) ...

  5. linux内核分析第三周-跟踪分析Linux内核的启动过程

    一.实验流程 1.打开环境 执行命令:cd LinuxKernel/ 执行命令:qemu -kernel linux-3.18.6/arch/x86/boot/bzImage -initrd root ...

  6. Linux删除重复行 排序和不排序的做法--转载

    本文部分翻译自这里,来自 Jadu Saikia 的博客,这个博客上有很多非常有用的小技巧,有空可以多看看. 通常如果我们想获取一个文件里不重复的行的时候,我们可以直接通过 sort -u 命令,先把 ...

  7. /msgsrvmgr.cpp:4:26: fatal error: kdl/frames.hpp: No such file or directory #include <kdl/frames.hpp>

    /home/xxx/ros_workspace/src/bp_protocol_bridge/protospot/src/msgsrvmgr.cpp::: fatal error: kdl/frame ...

  8. CentOS6.4x86EngCustomize120g__20160307.rar

    安装的镜像包: CentOS-6.4-i386-bin-DVD1to2(CentOS-6.4-i386-bin-DVD1.iso / CentOS-6.4-i386-bin-DVD2.iso) 1. ...

  9. zookeeper和Eureka对CAP理论的支持

    著名的CAP理论指出,一个分布式系统不可能同时满足C(一致性).A(可用性)和P(分区容错性).由于分区容错性在是分布式系统中必须要保证的,因此我们只能在A和C之间进行权衡.在此Zookeeper保证 ...

  10. UVA-673 Parentheses Balance(栈)

    题目大意:给一个括号串,看是否匹配. 题目分析:一开始用区间DP写的,超时了... 注意:空串合法. 代码如下: # include<iostream> # include<cstd ...