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. Oracle-数据库增删改查基本操作

    一.创建数据表 1).创建不存在的新表: create table tname(  Data_Name Date_Type [default][默认值]  );2).创建已存在表的副本 create ...

  2. OSS文件上传及OSS与ODPS之间数据连通

    场景描述        有这样一种场景,用户在自建服务器上存有一定数量级的CSV格式业务数据,某一天用户了解到阿里云的OSS服务存储性价比高(嘿嘿,颜值高),于是想将CSV数据迁移到云上OSS中,并且 ...

  3. Java动态代码模式

    java动态代理(JDK和cglib) JAVA的动态代理 代理模式 代理模式是常用的java设计模式,他的特征是代理类与委托类有同样的接口,代理类主要负责为委托类预处理消息.过滤消息.把消息转发给委 ...

  4. c++ 反射类型

    来自: 实现代码=== // // Created by lizhen on 2017/9/29. // #ifndef BOOST_ALL_CALLBACKFUNCTION_H #define BO ...

  5. python学习笔记08:安装django

    linux环境安装django: sudo pip install django windows环境安装django: pip install django 验证django是否安装: python ...

  6. (十一)instanceof 和 getclass 的区别

    判断两个对象是否为同一类型,时常用到getclass 和 instanceof ,而这两个函数又是时常让人混淆.下面从一个例子说明两者的区别: public class Test_drive { pu ...

  7. iOS-创建UIScrollerView(封装UIScrollerView)

    创建继承于UIView的类WJImageScrollView,代码实现如下: WJImageScrollView.h #import <UIKit/UIKit.h> /**点击图片bloc ...

  8. SQL 单表分页存储过程和单表多字段排序和任意字段分页存储过程

      第一种:单表多字段排序分页存储过程       --支持单表多字段查询,多字段排序 create PROCEDURE [dbo].[UP_GetByPageFiledOrder] ( ), --表 ...

  9. c#控件的name和text属性有什么不同?

    text 是显示出来,供用户和自己编辑方便使用的,name 属性是编辑代码用的. 比如要读取一个text栏的内容 取name='txtName' text='姓名'代码段需要写的是, txtName. ...

  10. Python自定义包在linux服务器导入错误的解决办法

    在本地机器上跑python代码,自己定义的文件进行导包运行是没有问题,但是放到linux服务器上的时候就会提示 ImportError:No module named xxxx(要导入的文件包名) 在 ...