【poj3690】Constellations 哈希
题目分析
考虑将大矩阵的每个1*q矩阵哈希值求出,然后让小矩阵的第一行在大矩阵中找,如果找到,并且能匹配所有行则出现过。否则没出现过。
在初始化1*q矩阵时可以进行优化:假设该行为123456,要求1*5的矩阵哈希值,可以先暴力求出1~5,为 $1 * H^4 + 2 * H^3 + 3 * H^2 + 4 * H + 5$,现在要删除1添加6:变为$2 * H^4 + 3 * H^3 + 4 * H^2 + 5 * H + 6$,也就是先减去1的哈希值乘以$H^{len - 1}$,然后乘以H,加上6的哈希值。代码如下:
for(int i = ; i <= n; i++){
for(int k = ; k <= q; k++)
fixedHash[i][] = fixedHash[i][] * H + getVal(matrix[i][k]);
for(int j = ; j <= m - q + ; j++)
fixedHash[i][j] = (fixedHash[i][j - ] - getVal(matrix[i][j - ]) * poww[q - ]) * H + getVal(matrix[i][j + q - ]);
}
code
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<algorithm>
using namespace std; const int N = ;
typedef unsigned long long ull;
const ull H = ;
ull fixedHash[N][N], hash[], poww[];
int n, m, T, p, q, k;
char matrix[N][N], small[][]; inline int getVal(char x){
return x == '*' ? : ;
} inline bool check(int x, int y){
for(int k = x + ; k <= x + p - ; k++)
if(fixedHash[k][y] != hash[k - x + ])
return false;
return true;
} int main(){
freopen("h.in", "r", stdin);
poww[] = ;
for(int i = ; i <= ; i++) poww[i] = poww[i - ] * H;
while(scanf("%d%d%d%d%d", &n, &m, &T, &p, &q), n + m + T + p + q){
memset(matrix, , sizeof matrix);
for(int i = ; i <= n; i++) scanf("%s", matrix[i] + );
memset(fixedHash, , sizeof fixedHash);
for(int i = ; i <= n; i++){
for(int k = ; k <= q; k++)
fixedHash[i][] = fixedHash[i][] * H + getVal(matrix[i][k]);
for(int j = ; j <= m - q + ; j++)
fixedHash[i][j] = (fixedHash[i][j - ] - getVal(matrix[i][j - ]) * poww[q - ]) * H + getVal(matrix[i][j + q - ]);
}
int cnt = ;
for(int i = ; i <= T; i++){
memset(small, , sizeof small);
for(int j = ; j <= p; j++)
scanf("%s", small[j] + );
memset(hash, , sizeof hash);
for(int j = ; j <= p; j++)
for(int k = ; k <= q; k++)
hash[j] = hash[j] * H + getVal(small[j][k]);
bool flag = true;
for(int j = ; j <= n - p + ; j++){
for(int k = ; k <= m - q + ; k++){
if(fixedHash[j][k] == hash[])
if(check(j, k)){
cnt++;
flag = false;
break;
}
}
if(!flag) break;
}
}
printf("Case %d: %d\n", ++k, cnt);
}
}
【poj3690】Constellations 哈希的更多相关文章
- POJ3690:Constellations(二维哈希)
Constellations Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 6822 Accepted: 1382 题目 ...
- POJ3690 Constellations 【KMP】
Constellations Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 5044 Accepted: 983 Des ...
- POJ3690 Constellations
嘟嘟嘟 哈希 刚开始我一直在想二维哈希,但发现如果还是按行列枚举的话会破坏子矩阵的性质.也就是说,这个哈希只能维护一维的子区间的哈希值. 所以我就开了个二维数组\(has_{i, j}\)表示原矩阵\ ...
- POJ 3690 Constellations (哈希)
题意:给定上一n*m的矩阵,然后的t个p*q的小矩阵,问你匹配不上的有多少个. 析:可以直接用哈希,也可以用AC自动机解决. 代码如下: #pragma comment(linker, "/ ...
- POJ3690:Constellations——题解
http://poj.org/problem?id=3690 题目大意:给一个图和几个子图,判断有多少种子图在原图出现过. —————————————————————— 二维哈希即可,操作看代码,我觉 ...
- [PHP内核探索]PHP中的哈希表
在PHP内核中,其中一个很重要的数据结构就是HashTable.我们常用的数组,在内核中就是用HashTable来实现.那么,PHP的HashTable是怎么实现的呢?最近在看HashTable的数据 ...
- java单向加密算法小结(2)--MD5哈希算法
上一篇文章整理了Base64算法的相关知识,严格来说,Base64只能算是一种编码方式而非加密算法,这一篇要说的MD5,其实也不算是加密算法,而是一种哈希算法,即将目标文本转化为固定长度,不可逆的字符 ...
- Java 哈希表运用-LeetCode 1 Two Sum
Given an array of integers, find two numbers such that they add up to a specific target number. The ...
- 网络安全——Base64编码、MD5、SHA1-SHA512、HMAC(SHA1-SHA512)哈希
据说今天520是个好日子,为什么我想起的是502.500.404这些?还好服务器没事! 一.Base64编码 Base64编码要求把3个8位字节(3*8=24)转化为4个6位的字节(4*6=24),之 ...
随机推荐
- 10559 - Blocks(方块消除|DP)
该题乍一看和矩阵链乘非常类似,但是有一个不同之处就是该题能够拼接 . 为了达到这个目的.我们不得不拓展维度d[i][j][k].用一个k表示最右边拼接了k个和a[j]同样颜色的方块. 问题的关键在 ...
- liunx基本操作常用命令
liunx通常用作服务器,运行服务器软件,服务器要等待,类似超市学关键命令操作 内核,外壳 shell命令跟内核打交道用的是发行版本,不是内核,Radhat公司的CentOS,阿里巴巴也用这个 liu ...
- [Angular] New async 'as' syntax and ngIf.. else
From Anuglar v4 above, we are able to using 'as' with async pipe. This allow as using 'new variable' ...
- android 5.x system.img 大于2G导致编译otapackage时报错怎样处理
当system分区预制过多apk时假设img size超过2G 在make otapackage时会报例如以下错误 zipfile.LargeZipFile: Zipfile size would ...
- php课程 8-30 实现验证码验证的难点是什么
php课程 8-30 实现验证码验证的难点是什么 一.总结 一句话总结:session技术实现验证码传递. 1.生成验证码的那个网页(php文件)中的验证码怎么搁到别的网页中去? 直接在img的src ...
- Java学习很好的笔记
http://www.cnblogs.com/vamei/archive/2013/03/31/2991531.html
- NIO 入门(转)
NIO 入门 Greg Travis2003 年 11 月 17 日发布 分享此页面 WeiboGoogle+用电子邮件发送本页面 20 在开始之前 关于本教程 新的输入/输出 (NIO) 库是在 J ...
- 使用DNSCrypt解决Dropbox污染问题
作者:半点闲 时间:2014-6-27 18:27 博客:blog.csdn.net/cg_i 邮箱:b_dx@sohu.com 背景知识:防火长城(GFW) keyword:DNSCrypt ...
- Cocos2d-x使用Javascript开发js绑定C++<代码演示样例>
class IOSiAPDelegate{ public: virtual ~IOSiAPDelegate() {} }; class IOSAlipay{ public: IOSAlipay(); ...
- nslookup详解(name server lookup)( 域名查询)
nslookup详解(name server lookup)( 域名查询) 一.总结 1.爬虫倒是很方便拿到页面数据:a.网页的页面源码我们可以轻松获得 b.比如cnsd博客,文章的正文内容全部放在 ...