题意

描述

给定一个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. A题:Common Substrings(KMP应用)

    原题链接 注意:2号和3号get_next()函数中next[i]赋值时的区别,一个是0,一个是1,且不能互换 #include<cstdio> #include<cstring&g ...

  2. 牛客国庆集训派对Day5 Solution

    A    璀璨光滑 留坑. B    电音之王 蒙特马利大数乘模运算 #include <bits/stdc++.h> using namespace std; typedef long ...

  3. Java中使用OpenSSL生成的RSA公私钥进行数据加解密

    当前使用的是Linux系统,已经按装使用OpenSSL软件包, 一.使用OpenSSL来生成私钥和公钥 1.执行命令openssl version -a 验证机器上已经安装openssl 1 open ...

  4. springcloud15---zuul-fallback

    package com.itmuch.cloud; import org.springframework.boot.SpringApplication; import org.springframew ...

  5. 如何使用STM32F4的BootLoader和APP程序

    源:如何使用STM32F4的BootLoader和APP程序 STM32 BootLoader升级固件

  6. 20145322何志威《网络对抗》逆向及Bof基础

    20145322何志威<网络对抗>逆向及Bof基础 实践目标 本次实践的对象是一个名为pwn1的linux可执行文件. 该程序正常执行流程是:main调用foo函数,foo函数会简单回显任 ...

  7. sqlmap简单使用方法

    sqlmap使用 注入点   http://1xx.xxx.xxx.xxxx/dvwa/vulnerabilities/sqli/index.php?id=1&Submit=Submit# 通 ...

  8. Linux清除Windows密码

    下载安装ntfs-3g 下载驱动让linux挂载windows磁盘 https://tuxera.com/opensource/ntfs-3g_ntfsprogs-2017.3.23.tgz 安装 t ...

  9. jquery-ui autocomplete在模态框(model)中,出不来

    知识点:在使用模态框中使用 jquery-ui autocomplete,无法显示下拉框的数据 参考博客:https://www.jianshu.com/p/3944693773ed 解决办法:在au ...

  10. IDEA使用Git管理项目

    今天将项目使用Git管理了,IDEA. 第一步: 第二步: