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 ...
随机推荐
- [Clr via C#读书笔记]Cp13接口
Cp13接口 类和接口继承 接口只提供签名,不提供实现:等效于契约:凡事能使用具名接口的地方都能够使用实现了的接口. 定义接口 定义很简单,FCL也提供了大量的现成接口供使用: 继承接口 类不能多继承 ...
- java学习笔记-9.违例差错控制
1.违例规范是告诉程序员这个方法可能抛出哪些类型的异常.他的格式在方法声明中,位于自变量(参数)列表的后面,如void f() throws tooBig, tooSmall, divZero { ...
- django 连接mysql报错
原因: 问题1. 即从mysql5.7版本之后,默认采用了caching_sha2_password验证方式. 问题2. 然后在执行 python manage.py makemigrations依 ...
- 【QT】宏
宏 Q_CORE_EXPORT _CORE_EXPORT 其实是一个宏,用来说明这是一个动态库导出类.QT是个跨平台的库,而不同的操作系统,不同的编译器,对动态库的导出说明是不一样的,比如,在wind ...
- 1.EOS源码编译运行
目前网络上都是针对老版EOS2.0源码编译的文章,我在mac上参考这些文章编译,最后发现根本就不对,最新版本只需一条命令(./eosio_build.sh,依赖库会自动安装的)即可.我根据这些文章手动 ...
- 2.hbase原理(未完待续)
hbase简介相关概念hmsterhregionserver表regionhstorememstorestorefilehfileblockcacheWALminorcompactmajorcompa ...
- redis集群sentinel哨兵模式的搭建与实际应用
参考资料:https://blog.csdn.net/men_wen/article/details/72724406 之前环境使用的keepalived+redis vip集群模式,现在我们服务切换 ...
- iOS开发解决页面滑动返回跟scrollView左右划冲突
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithG ...
- Token安全
token相对安全加密算法 http://blog.csdn.net/q8649912/article/details/52370565 关于文章的理解 1 sessionid 这个名词应该理解为:一 ...
- adb shell input keyevent值所对应的字符
转自:http://blog.csdn.net/chen825919148/article/details/18732041 0 --> "KEYCODE_UNKNOWN" ...