POJ3690:Constellations(二维哈希)
Constellations
| Time Limit: 3000MS | Memory Limit: 65536K | |
| Total Submissions: 6822 | Accepted: 1382 |
题目链接:http://poj.org/problem?id=3690
Description:
The starry sky in the summer night is one of the most beautiful things on this planet. People imagine that some groups of stars in the sky form so-called constellations. Formally a constellation is a group of stars that are connected together to form a figure or picture. Some well-known constellations contain striking and familiar patterns of bright stars. Examples are Orion (containing a figure of a hunter), Leo (containing bright stars outlining the form of a lion), Scorpius (a scorpion), and Crux (a cross).
In this problem, you are to find occurrences of given constellations in a starry sky. For the sake of simplicity, the starry sky is given as a N × M matrix, each cell of which is a '*' or '0' indicating a star in the corresponding position or no star, respectively. Several constellations are given as a group of T P × Q matrices. You are to report how many constellations appear in the starry sky.
Note that a constellation appears in the sky if and only the corresponding P × Q matrix exactly matches some P × Q sub-matrix in the N × M matrix.
Input:
The input consists of multiple test cases. Each test case starts with a line containing five integers N, M, T, P and Q(1 ≤ N, M ≤ 1000, 1 ≤ T ≤ 100, 1 ≤ P, Q ≤ 50).
The following N lines describe the N × M matrix, each of which contains M characters '*' or '0'.
The last part of the test case describe T constellations, each of which takes P lines in the same format as the matrix describing the sky. There is a blank line preceding each constellation.
The last test case is followed by a line containing five zeros.
Output:
For each test case, print a line containing the test case number( beginning with 1) followed by the number of constellations appearing in the sky.
Sample Input:
3 3 2 2 2
*00
0**
*00 **
00 *0
**
3 3 2 2 2
*00
0**
*00 **
00 *0
0*
0 0 0 0 0
Sample Output:
Case 1: 1
Case 2: 2
题意:
给出一个n*m个矩阵,并且给出若干个p*q的小矩阵,然后回答有多少个小矩阵在大矩阵中出现了的。
题解:
数据范围不是很大,直接二维暴力hash就是了。
二维hash跟一维都差不多的吧,具体细节见代码吧。
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <set>
using namespace std;
typedef long long ll;
typedef unsigned int Ull ;
const int N = ;
int n, m, T, p, q;
Ull x1 = , x2 = ;
Ull px1[N], px2[N];
Ull Hash[N][N], Hash_Table[N * N], val[][];
char s[N][N], t[N][N]; int main() {
px1[] = px2[] = ;
for(int i = ; i <= ; i++) {
px1[i] = px1[i - ] * x1;
px2[i] = px2[i - ] * x2;
}
int cnt = ;
while(scanf("%d%d%d%d%d", &n, &m, &T, &p, &q) != EOF) {
if(n + m + T + p + q <= )
break ;
cnt++;
memset(Hash,,sizeof(Hash));
for(int i = ; i <= n; i++) {
scanf("%s", s[i] + );
for(int j = ; j <= m; j++) {
Hash[i][j] = Hash[i][j - ] * x1 + (Ull)s[i][j];
}
}
for(int j = ; j <= m; j++) {
for(int i = ; i <= n; i++) {
Hash[i][j] = Hash[i - ][j] * x2 + Hash[i][j];
}
}
int tt = T;
multiset<Ull> S;
while(tt--) {
memset(val,,sizeof(val));
for(int i = ; i <= p; i++) {
scanf("%s", t[i] + );
for(int j = ; j <= q; j++) {
val[i][j] = val[i][j - ] * x1 + (Ull)t[i][j];
}
}
for(int j = ; j <= q; j++) {
for(int i = ; i <= p; i++) {
val[i][j] = val[i - ][j] * x2 + val[i][j];
}
}
S.insert(val[p][q]);
}
for(int i = p; i <= n; i++) {
for(int j = q; j <= m; j++) {
Ull Val = Hash[i][j] + Hash[i - p][j - q] * px1[q] * px2[p] - Hash[i][j - q] * px1[q] - Hash[i - p][j] * px2[p];
S.erase(Val);
}
}
printf("Case %d: %d\n", cnt, T - (int)S.size());
}
return ;
}
POJ3690:Constellations(二维哈希)的更多相关文章
- 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 ...
- 【BZOJ 2462】矩阵模板 (二维哈希)
题目 给定一个M行N列的01矩阵,以及Q个A行B列的01矩阵,你需要求出这Q个矩阵哪些在 原矩阵中出现过. 所谓01矩阵,就是矩阵中所有元素不是0就是1. 输入 输入文件的第一行为M.N.A.B,参见 ...
- AcWing - 156 矩阵(二维哈希)
题目链接:矩阵 题意:给定一个$m$行$n$列的$01$矩阵$($只包含数字$0$或$1$的矩阵$)$,再执行$q$次询问,每次询问给出一个$a$行$b$列的$01$矩阵,求该矩阵是否在原矩阵中出现过 ...
- 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 哈 ...
- TTTTTTTTTTTTTTTTTTTTT POJ 3690 0与* 二维哈希 模板 +multiset
Constellations Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 5923 Accepted: 1164 De ...
- POJ3690 Constellations
嘟嘟嘟 哈希 刚开始我一直在想二维哈希,但发现如果还是按行列枚举的话会破坏子矩阵的性质.也就是说,这个哈希只能维护一维的子区间的哈希值. 所以我就开了个二维数组\(has_{i, j}\)表示原矩阵\ ...
- golang 多维哈希(map,hashmap)实践随笔
有些场景使用多维哈希来存储数据,时间复杂度恒定,简单粗暴好用.这里记录一下. 如下是三维哈希的简单示意图,建议层数不要太多,否则时间久了,自己写的代码都不认识. 下图是三维哈希在内存的存储形式,has ...
随机推荐
- PNG和PVR之间互相转换的脚本
项目经常会将png和pvr之间互相转换,这里mark一个脚本,会将当前目录下的文件全部批量转换 png转换成pvr @echo off path %path%;"C:\Program Fil ...
- git branch 分支与合并
在使用 git 进行分支开发与合并的时候需要用到这些命令.其他基本 git 命令参考 Git 简易食用指南 git branch 查看分支 git branch 查看当前分支情况 创建分支 git b ...
- 从零开始的Python学习Episode 5——字典
字典 字典是另一种可变容器模型,且可存储任意类型对象. 一.添加 (1)直接添加 dict={'name':'smilepup'} dict['age']=20 dict['name']='piggy ...
- 01背包问题:DP
题目描述: 有 N 件物品和一个容量是 V 的背包.每件物品只能使用一次. 第 i 件物品的体积是 vi,价值是 wi. 求解将哪些物品装入背包,可使这些物品的总体积不超过背包容量,且总价值最大.输出 ...
- 线性代数之——微分方程和 exp(At)
本节的核心是将常系数微分方程转化为线性代数问题. \[\frac{du}{dt}=\lambda u \quad 的解为 \quad u(t) = Ce^{\lambda t}\] 代入 \(t=0\ ...
- JQuery文本框验证
<" CODEPAGE="936"%><!--#include file="conncon.asp"--><!--#in ...
- VBA基础之Excel 工作薄(Book)的操作(三)
三. Excel 工作薄(Book)的操作1. Excel 创建工作薄(Book) Sub addWorkbook() Workbooks.Add End Sub 2. Excel 打开工作薄(Boo ...
- vue移动音乐app开发学习(三):轮播图组件的开发
本系列文章是为了记录学习中的知识点,便于后期自己观看.如果有需要的同学请登录慕课网,找到Vue 2.0 高级实战-开发移动端音乐WebApp进行观看,传送门. 完成后的页面状态以及项目结构如下: 一: ...
- Python基础1 Hello World!
从今天开始和大家分享一下python最基础的知识,以便帮助初学者快速入门. 最最基础的当然是hello world 了,无论哪门语言都会从它开始... 简单的‘Hello World!’ 1. 直接运 ...
- 读取游标 BEGIN END
USE db_2008 --引入数据库 DECLARE ReadCursor CURSOR --声明一个游标 FOR SELECT * FROM Student OPEN ReadCursor --打 ...