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(二维哈希)的更多相关文章

  1. URAL - 1486 Equal Squares 二维哈希+二分

    During a discussion of problems at the Petrozavodsk Training Camp, Vova and Sasha argued about who o ...

  2. 【URAL 1486】Equal Squares(二维哈希+二分)

    Description During a discussion of problems at the Petrozavodsk Training Camp, Vova and Sasha argued ...

  3. 【BZOJ 2462】矩阵模板 (二维哈希)

    题目 给定一个M行N列的01矩阵,以及Q个A行B列的01矩阵,你需要求出这Q个矩阵哪些在 原矩阵中出现过. 所谓01矩阵,就是矩阵中所有元素不是0就是1. 输入 输入文件的第一行为M.N.A.B,参见 ...

  4. AcWing - 156 矩阵(二维哈希)

    题目链接:矩阵 题意:给定一个$m$行$n$列的$01$矩阵$($只包含数字$0$或$1$的矩阵$)$,再执行$q$次询问,每次询问给出一个$a$行$b$列的$01$矩阵,求该矩阵是否在原矩阵中出现过 ...

  5. UVA-11019 二维哈希算法

    UVA-11019 题意: 就是给你AB两个字符矩阵,问你B矩阵在A矩阵中的出现次数. 题解:  参考链接:https://blog.csdn.net/qq_38891827/java/article ...

  6. bzoj 2351 [BeiJing2011]Matrix——二维哈希

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2351 就是先把每行单独从左到右扫着乘一个 b1 哈希起来,然后再按列从上往下乘一个 b2 哈 ...

  7. TTTTTTTTTTTTTTTTTTTTT POJ 3690 0与* 二维哈希 模板 +multiset

    Constellations Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 5923   Accepted: 1164 De ...

  8. POJ3690 Constellations

    嘟嘟嘟 哈希 刚开始我一直在想二维哈希,但发现如果还是按行列枚举的话会破坏子矩阵的性质.也就是说,这个哈希只能维护一维的子区间的哈希值. 所以我就开了个二维数组\(has_{i, j}\)表示原矩阵\ ...

  9. golang 多维哈希(map,hashmap)实践随笔

    有些场景使用多维哈希来存储数据,时间复杂度恒定,简单粗暴好用.这里记录一下. 如下是三维哈希的简单示意图,建议层数不要太多,否则时间久了,自己写的代码都不认识. 下图是三维哈希在内存的存储形式,has ...

随机推荐

  1. python3 SQLAlchemy模块使用

    更详细的操作介绍:https://www.imooc.com/article/22343 定义: SQLAlchemy是Python编程语言下的一款ORM框架,该框架建立在数据库API之上,使用关系对 ...

  2. [leetcode-748-Largest Number At Least Twice of Others]

    In a given integer array nums, there is always exactly one largest element. Find whether the largest ...

  3. 水仙花数---基于python

    # coding:utf-8"""水仙花数是指一个 n 位数(n≥3 ),它的每个位上的数字的 n 次幂之和等于它本身(例如:1^3 + 5^3+ 3^3 = 153) ...

  4. 路由器如何设置上网(TP-LINK)

    最近宿舍公用的网络一直不太稳定,正赶上毕业季,本来就打算自己买一台自用的路由器,于是我从一个毕业的师姐手里15RMB收了一台路由器,师姐还给了我一根5m的网线和两根全新15m的,感觉光网线就赚翻了. ...

  5. Python练习—文件

    1.随机生成20个两位正整数,将其升序排序后再写入文本文件data_asc.txt中! import random alist = [random.randint(10,100) for i in r ...

  6. c#程序的config文件问题

    1.vshost.exe.config和app.config两个文件可不要,但exe.config文件不可少. 2.但是app.config最好也要修改了,每次重新生成程序的时候.exe.cmonfi ...

  7. 分布式系统理论-terms

    Distributed programming is the art of solving the same problem that you can solve on a single comput ...

  8. 结对作业二——WordCount进阶版

    软工作业三 要求地址 作业要求地址 结对码云项目地址 结对伙伴:秦玉 博客地址 PSP表格 PSP2.1 个人开发流程 预估耗费时间(分钟) 实际耗费时间(分钟) Planning 计划 10 7 · ...

  9. wpf 验证方法

    效果图,当放鼠标到文本框上会显示出错的提示.

  10. js 事件阻止传播方法,准确定位事件源

    1事件冒泡 在目标元素获得机会处理事件后,事件模型检查目标元素的父元素,看是否为同类型事件建立了处理程序.如果是,则也调用父元素的处理程序.在这之后,再检查其父元素,然后父元素,然后父元素...持续不 ...