题意

描述

给定一个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. Educational Codeforces Round 59 Solution

    A. Digits Sequence Dividing 签. #include <bits/stdc++.h> using namespace std; #define N 1010 ch ...

  2. Java遍历包中所有类方法注解

    import java.io.File; import java.io.FileFilter; import java.io.IOException; import java.lang.annotat ...

  3. css 文档流中块级非替换元素水平区域的计算规则(1)

    最近在读<Basic Visual Formatting in CSS>,结合以前看的<css权威指南>和css标准.今天就做个笔记. 以前在遇到一些宽度不明确指明的一些布局的 ...

  4. Fms3和Flex打造在线视频录制和回放

    本博推荐文章快速导航: Sql Server2005 Transact-SQL 新兵器学习MCAD学习 代码阅读总结 ASP.NET状态管理 DB(数据库)WAPWinFormFlex,Fms aie ...

  5. python使用zipfile解压中文乱码问题

    在zipfile.ZipFile中获得的filename有中日文则很大可能是乱码,这是因为 在zip标准中,对文件名的 encoding 用的不是 unicode,而可能是各种软件根据系统的默认字符集 ...

  6. table--边框样式设置

    Table的一些设置(自适应以及溢出)   table的两个属性 单行溢出点点显示 表格的宽度设置 双栏自适应连续连续英文符换行 1.table重置的两个属性: ①border-collapse: c ...

  7. 初识PHP(四)PDO对象配置于使用

    一.PDO的概念 PDO其实就是一个数据库的抽象层,使用PDO编程可以方便的在之后的实际运营中随时更改数据库而不用变更源代码.PDO的位置如下图所示: 二.PDO的开启 PDO需要使用php 5.1 ...

  8. 设置VS快捷代码片段

    一.自定义sinppets方式 1.在VS安装路径[D:\vs2013\VC\Snippets\2052\Visual C++]下新建一个snippt文件 2.添加代码 <?xml versio ...

  9. git-format-patch如何指定补丁生成的Subject格式

    答:使用-N来指定,如: git format-patch -N <commit-id> 生成的补丁中Subject将以[PATCH]的格式呈现,例如:Subject: [PATCH] a ...

  10. 记第一场atcoder和codeforces 2018-2019 ICPC, NEERC, Northern Eurasia Finals Online Mirror

    下午连着两场比赛,爽. 首先是codeforses,我和一位dalao一起打的,结果考炸了,幸亏不计rating.. A Alice the Fan 这个就是记忆化搜索一下预处理,然后直接回答询问好了 ...