AcWing - 156 矩阵(二维哈希)
题目链接:矩阵
题意:给定一个$m$行$n$列的$01$矩阵$($只包含数字$0$或$1$的矩阵$)$,再执行$q$次询问,每次询问给出一个$a$行$b$列的$01$矩阵,求该矩阵是否在原矩阵中出现过
思路:二维哈希,从矩阵的右下角为低位到矩阵的左上角为高位,先求出每一行的一维哈希值$h[i][j]$,在$a$行$b$列的$01$矩阵向下移动的过程中,先向下扩展成$a+1$行$b$列的$01$矩阵,再将最上面的一行减去,这样矩阵就会向下移动一格,设原来$a$行$b$列矩阵的哈希值为$t$,所以新矩阵的哈希值
$$res=t*p[b]+(h[i+1][j]-h[i+1][j-b]*p[b])-((h[i-a][j]-h[i-a][j-b]*p[b])*p[a*b])$$
用$set$存储每个子矩阵的哈希值,求出询问矩阵的哈希值,判断是否出现过
其他二维哈希的题目,令矩阵的右下角为低位、矩阵的左上角为高位,然后推一下公式即可。
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <set> using namespace std; typedef unsigned long long ull; const int N = ;
const int M = N * N;
const ull P = ; int n, m, a, b, k;
ull h[N][N], p[M]; ull Hash(ull f[], int l, int r)
{
return f[r] - f[l - ] * p[r - l + ];
} int main()
{
scanf("%d%d%d%d", &n, &m, &a, &b);
p[] = ;
for (int i = ; i <= n * m; i++)
p[i] = p[i - ] * P;
for (int i = ; i <= n; i++) {
char s[N];
scanf("%s", s + );
for (int j = ; j <= m; j++)
h[i][j] = h[i][j - ] * P + s[j] - '';
}
set<ull> st;
for (int i = b; i <= m; i++) {
ull t = ;
int l = i - b + , r = i;
for (int j = ; j <= n; j++) {
t = t * p[b] + Hash(h[j], l, r);
if (j > a) t -= Hash(h[j - a], l, r) * p[a * b];
if (j >= a) st.insert(t);
}
}
scanf("%d", &k);
while (k--) {
ull t = ;
char s[N];
for (int i = ; i <= a; i++) {
scanf("%s", s + );
for (int j = ; j <= b; j++)
t = t * P + s[j] - '';
}
if (st.count(t)) printf("1\n");
else printf("0\n");
}
return ;
}
AcWing - 156 矩阵(二维哈希)的更多相关文章
- 【BZOJ 2462】矩阵模板 (二维哈希)
题目 给定一个M行N列的01矩阵,以及Q个A行B列的01矩阵,你需要求出这Q个矩阵哪些在 原矩阵中出现过. 所谓01矩阵,就是矩阵中所有元素不是0就是1. 输入 输入文件的第一行为M.N.A.B,参见 ...
- URAL - 1486 Equal Squares 二维哈希+二分
During a discussion of problems at the Petrozavodsk Training Camp, Vova and Sasha argued about who o ...
- 【URAL 1486】Equal Squares(二维哈希+二分)
Description During a discussion of problems at the Petrozavodsk Training Camp, Vova and Sasha argued ...
- AcWing 156. 矩阵 (哈希二维转一维查询)打卡
给定一个M行N列的01矩阵(只包含数字0或1的矩阵),再执行Q次询问,每次询问给出一个A行B列的01矩阵,求该矩阵是否在原矩阵中出现过. 输入格式 第一行四个整数M,N,A,B. 接下来一个M行N列的 ...
- POJ3690:Constellations(二维哈希)
Constellations Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 6822 Accepted: 1382 题目 ...
- BZOJ 1567: [JSOI2008]Blue Mary的战役地图 矩阵二维hash
1567: [JSOI2008]Blue Mary的战役地图 Description Blue Mary最近迷上了玩Starcraft(星际争霸) 的RPG游戏.她正在设法寻找更多的战役地图以进一步提 ...
- 用Python实现根据角4点进行矩阵二维插值并画出伪彩色图
哈哈,题目取得这么绕,其实就是自己写了一个很渣的类似图像放大的算法.已知矩阵四周的4点,扩展成更大的矩阵,中间的元素值均匀插入,例如: 矩阵: 1 2 3 4 扩展成3x3的: 1 1.5 2 ...
- UVA-11019 二维哈希算法
UVA-11019 题意: 就是给你AB两个字符矩阵,问你B矩阵在A矩阵中的出现次数. 题解: 参考链接:https://blog.csdn.net/qq_38891827/java/article ...
- bzoj 2351 [BeiJing2011]Matrix——二维哈希
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2351 就是先把每行单独从左到右扫着乘一个 b1 哈希起来,然后再按列从上往下乘一个 b2 哈 ...
随机推荐
- wamp 增加mongodb拓展 安装
安装环境: windows 7 64bit php 5.5.12 确认环境参数: 1.在phpinfo() 中查看compiler 2.在phpinfo() 中查看thread safety,线程是否 ...
- oracle dataguard配置
1.archivelog设置:(存档模式) 2.standy controlfile 设置: alter database create standby controlfile as '/data/o ...
- python3练习100题--024
因为过生日,好几天没做题了,有点松懈. 我要更加加油啦-为了打败现在每天都厌恶的生活! 原题链接:http://www.runoob.com/python/python-exercise-exampl ...
- python 自动化实现定时发送html报告到邮箱
# coding =utf-8 import os import unittest import time import datetime import smtplib from email.mime ...
- RemoteViews 整理
前言 RemoteViews表面意思就是远程的view,这个就很难理解了,远程的view.但是英语是抽象,remote本身就是偏僻的,偏远的意思. 所以remoteViews 就是地方view,天高皇 ...
- AcWing 893. 集合-Nim游戏
//只能拿某些特定个数的石子 #include <cstring> #include <iostream> #include <algorithm> #includ ...
- 怎么把项目发布到github上
方法一:在github上新建一个项目,然后在本地任意个文件夹(最好新建)右键 git bash here ,再之后 git clone https://github.com/CKTim/BlueT ...
- 【转】Git常用命令指南
1.git init 初始化一个Git仓库,git init –bare example.git创建一个裸仓,即没有工作区的git仓库.2.添加文件到Git仓库,分两步:git add <fil ...
- ffmpeg 音频常用命令
ffmpeg的使用方式: ffmpeg [options] [[infile options] -i infile]... {[outfile options] outfile}... Stream ...
- 一文明白所谓的CS与BS设计模式
CS设计模式 概念:CS设计模式,C代表的是Client,S代表的是Server.正如图中的所示,是客户机与服务器之间的交互.这种交互在早期的软件系统中,大多数都是采用这种模式,通过将任务合理分配到C ...