矩阵hash + KMP - UVA 12886 The Big Painting
The Big Painting
Problem's Link: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=88791
Mean:
给你两个由字符组成的矩阵,让你判断第一个矩阵在第二个矩阵中出现了多少次。
analyse:
典型的二维矩阵hash。
这题有两种做法:
第一种:横向hash,然后纵向KMP。时间复杂度是O(N*(N+M)).
第二种:二维hash。直接对两个矩阵做二维hash,然后判断大矩阵的子矩阵的hash值是否等于小矩阵的hash值,统计答案即可。
从实现上来说,二维hash更容易写,也更容易理解,但是我在比赛时忽略了一种情况:
xo
xoxo
oxox
正确答案是:3.
但如果我们在hash时,横向hash选取的种子值和纵向hash选取的种子值是相同的,就会出现错误,答案就变为了5。
这是因为本来答案只要统计横向的,但是由于横向和纵向的种子值相同,就会多统计进去两次纵向的。
如果题目说小矩形(或者大矩形)可以旋转匹配,那么种子值选取一样就对了。
Time complexity: 二维hash:O(N*M) hash+KMP:O(N*(N+M))
Source code:
二维hash:
* this code is made by crazyacking
* Verdict: Accepted
* Submission Date: 2015-08-10-08.37
* Time: 0MS
* Memory: 137KB
*/
#include <queue>
#include <cstdio>
#include <set>
#include <string>
#include <stack>
#include <cmath>
#include <climits>
#include <map>
#include <cstdlib>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#define LL long long
#define ULL unsigned long long
using namespace std;
const int MAXN=;
const ULL seed1 = ; // line h
const ULL seed2 = ;// row w
int hp,wp,hm,wm;
char G1[MAXN][MAXN],G2[MAXN][MAXN];
ULL Tab1[MAXN][MAXN],Tab2[MAXN][MAXN],goal;
void GetHash()
{
goal=;
for(int i=;i<hp;++i)
{
ULL a=;
for(int j=;j<wp;++j)
{
a=a*seed2+G1[i][j];
}
goal=goal*seed1+a;
}
}
int solve()
{
int ans=;
ULL base1=,base2=;
for(int i=;i<hp;++i) base1*=seed1;
for(int i=;i<wp;++i) base2*=seed2;
for(int i=;i<hm;++i) // line
{
ULL a=;
for(int j=;j<wp;++j) a=a*seed2+G2[i][j];
Tab1[i][wp-]=a;
for(int j=wp;j<wm;++j)
{
Tab1[i][j]=Tab1[i][j-]*seed2 + G2[i][j] -base2*G2[i][j-wp];
}
}
for(int i=wp-;i<wm;++i)
{
ULL a=;
for(int j=;j<hp;++j) a=a*seed1+Tab1[j][i];
Tab2[hp-][i]=a;
if(a==goal) ++ans;
for(int j=hp;j<hm;++j)
{
Tab2[j][i]=Tab2[j-][i]*seed1 +Tab1[j][i] -Tab1[j-hp][i]*base1;
if(Tab2[j][i]==goal) ++ans;
}
}
return ans;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie();
while(~scanf("%d %d %d %d",&hp,&wp,&hm,&wm))
{
getchar();
for(int i=;i<hp;++i) gets(G1[i]);
for(int i=;i<hm;++i) gets(G2[i]);
GetHash();
printf("%d\n",solve());
}
return ;
}
/*
*/
hash+KMP:
* this code is made by crazyacking
* Verdict: Accepted
* Submission Date: 2015-08-09-13.27
* Time: 0MS
* Memory: 137KB
*/
#include <queue>
#include <cstdio>
#include <set>
#include <string>
#include <stack>
#include <cmath>
#include <climits>
#include <map>
#include <cstdlib>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#define LL long long
#define ULL unsigned long long
using namespace std;
const int MAXN = ;
const ULL seed =;
ULL base[MAXN];
ULL hashval[MAXN][MAXN],Ti[MAXN][MAXN],hashTmp[MAXN],val[MAXN];
int Next[MAXN],h,w,n,m;
char s[MAXN][MAXN],t[MAXN][MAXN];
int KMP()
{
int ans=;
for(int i=,j=-;i<m;++i)
{
while(j!=- && val[i]!=hashTmp[j+]) j=Next[j];
if(val[i]==hashTmp[j+])
{
if(++j==w-)
{
j=Next[j];
ans++;
}
}
}
return ans;
}
void init(char s[MAXN][MAXN],ULL hashval[MAXN][MAXN],int h,int w)
{
for(int j=;j<w;++j)
{
hashval[h][j]=;
for(int i=h-;i>=;--i) hashval[i][j]=hashval[i+][j]*seed+s[i][j];
}
}
int solve()
{
init(s,hashval,h,w);
init(t,Ti,n,m);
for(int i=;i<w;++i)
{
hashTmp[i]=hashval[][i];
}
// KMP Matching
Next[]=-;
// puts("- - - - - - - - - - - Program Run Here ! - - - - - - - - - - - - ");
for(int i=,j=-;i<w;++i)
{
while(j!=- && hashTmp[i]!=hashTmp[j+]) j=Next[j];
Next[i]=hashTmp[i]==hashTmp[j+]? ++j:j;
}
// for(int i=1;i<w;++i)
// {
// printf("%d",Next[i]);
// }
int ans=;
for(int i=;i<n-h+;++i)
{
for(int j=;j<m;++j)
val[j]=Ti[i][j]-Ti[i+h][j]*base[h];
ans+=KMP();
}
return ans;
}
void _()
{
base[]=;
for(int i=;i<MAXN;++i) base[i]=base[i-]*seed;
}
int main()
{
_();
while(~scanf("%d %d %d %d",&h,&w,&n,&m))
{
getchar();
for(int i=;i<h;++i) gets(s[i]);
for(int i=;i<n;++i) gets(t[i]);
printf("%d\n",solve());
}
}
矩阵hash + KMP - UVA 12886 The Big Painting的更多相关文章
- LA 6893 The Big Painting(矩阵Hash)
https://vjudge.net/problem/UVALive-6893 题意: 给出一个小矩阵和大矩阵,在大矩阵中能找到相同的小矩阵. 思路: 矩阵Hash,先对小矩阵计算出它的Hash值,然 ...
- [poj] 3690 Constellations || 矩阵hash
原题 在大矩阵里找有几个小矩阵出现过,多组数据 将t个矩阵hash值放入multiset,再把大矩阵中每个hash值从multiset里扔出去,这样最后剩在multiset里的值就是没有找到的小矩阵, ...
- 7.26机房报零赛——无尽的矩阵【kmp+hash】
恩,其实大家都没有报零,反正我是蒟蒻 为了纪念我第一次打过哈希,特此写一篇题解 题目描述 从前有一个的小矩阵,矩阵的每个元素是一个字母(区分大小写),突然有一天它发生了 变异,覆盖了整个二维空间,即不 ...
- UVA - 11019 Matrix Matcher hash+KMP
题目链接:传送门 题解: 枚举每一行,每一行当中连续的y个我们hash 出来 那么一行就是 m - y + 1个hash值,形成的一个新 矩阵 大小是 n*(m - y + 1), 我们要找到x*y这 ...
- 牛客网训练1--------矩阵 (二份+二维矩阵hash)
不懂hash的话:https://www.cnblogs.com/ALINGMAOMAO/p/10345850.html 思路:对于一个大矩阵的每一个子矩阵都对应着一个hash值k, 当k出现2次以上 ...
- LA 6893 矩阵HASH (模板)
#include<stdio.h> #include<string.h> typedef unsigned long long ULL; ; ; int test,n,m,x, ...
- 【BZOJ】1009: [HNOI2008]GT考试(dp+矩阵乘法+kmp+神题)
http://www.lydsy.com/JudgeOnline/problem.php?id=1009 好神的题orzzzzzzzzzz 首先我是连递推方程都想不出的人...一直想用组合来搞..看来 ...
- BZOJ 1009 [HNOI2008]GT考试 ——矩阵乘法 KMP
先用KMP处理所有的转移,或者直接暴力也可以. 然后矩阵快速幂即可. #include <cstdio> #include <cstring> #include <ios ...
- D. Tavas and Malekas DFS模拟 + kmp + hash || kmp + hash
http://codeforces.com/contest/535/problem/D 如果真的要把m个串覆盖上一个串上面,是可以得,不会超时. 要注意到一点,全部覆盖后再判断时候合法,和边放边判断, ...
随机推荐
- (C++)函数参数传递中的一级指针和二级指针
主要内容: 1.一级指针和二级指针 2.函数指针传递的例子 3.什么时候需要传递二级指针? 4.二级指针在链表中的使用 1.一级指针和二级指针 一级指针:即我们一般说的指针,就是内存地址: 二级指针: ...
- 无法加载Dll”ArcGISVersion.dll”:0x8007007E
在Win7x64位环境下,无法加载Dll"ArcGISVersion.dll":找不到指定的模块 解决方案: 打开项目的属性-生成-常规-目标平台,选择X86. 参考:http:/ ...
- [转载]手工安全测试方法&修改建议
转载自: Web安全测试(一)-手工安全测试方法&修改建议 1.XSS(Cross-Site Script)跨站脚本攻击 XSS(Cross-Site Script):跨站脚本攻击. 它指的是 ...
- 【JavaScript】2013年人气最高的JavaScript框架排名
本文概述 本文介绍2013年人气急速上升,2014年必须知道的JavaScript框架排名.本文所介绍的排名为Google根据全世界2013年的搜索关键词所做出的统计结果. MVC框架 JavaScr ...
- android 使用AChartEngine 饼图的实现
1.AChartEngine 简介 AChartEngine是为Android应用而设计的绘图工具库.可用于绘制多种图表,我使用的是achartengine-1.1.0.jar.ChartEngine ...
- 编程算法 - 翻转单词顺序 代码(C)
翻转单词顺序 代码(C) 本文地址: http://blog.csdn.net/caroline_wendy 题目: 输入一个英文句子, 翻转句子中单词的顺序, 但单词内字符的顺序不变. 首先翻转(r ...
- oracle导出数据库dmp文件
导出数据库为dmp文件,按照当前导出时间设置文件名称 @ECHO OFF ECHO 备份 SCOTT 用户的数据…… SET DBUserName=scott SET DBPassword= SET ...
- 为什么我没有拔出钥匙 ——开锁引发的程序bug解决方案的思考
http://blog.csdn.net/wojiushiwo987/article/details/8851204为什么我没有拔出钥匙 ——开 ...
- ubuntu 命令行下查看网页 w3m
w3m localhost/index.php
- mysql的grant用法(转)
GRANT USAGE ON *.* TO 'discuz'@'localhost' IDENTIFIED BY PASSWORD '*C242DDD213BE9C6F8DA28D49245BF69F ...