矩阵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个串覆盖上一个串上面,是可以得,不会超时. 要注意到一点,全部覆盖后再判断时候合法,和边放边判断, ...
随机推荐
- 如何使用飞秋FeiQ实现两电脑通信(或传输文件)
如何使用飞秋FeiQ实现两电脑通信(或传输文件) 1. 在两天电脑上,分别按照飞秋FeiQ 我使用的绿色飞秋2013正式版 2. 使用一根网线,将两电脑的网口连接一起 3. 设置飞秋FeiQ的端口号不 ...
- Github.com sshkey 生成与添加
大致流程是先在你电脑上生成的一个ssh key 然后把key 加到github.com上你个人帐户的设置里面 cd ~/.ssh 如果你的电脑上还没有.ssh目录 ssh-keygen -t rsa ...
- ReadOnly field saved with NULL value
On CRM opportunity form view, i added readonly="1" for probability field. When i saved, wh ...
- LInux下inode空间报警-CROND出错导致/var/spool/postfix/maildrop/堆积
Linux下显示磁盘空间不足,,通过 df -ih 查询发现/dev/mapper/*****var 下的inode用满.inode介绍 通过 du -sh * 查询/目录下的问题,最终查到/var/ ...
- Android系统源代码——所需工具
一.概述 众所周知,Android移动操作系统是Google花费了很大的财力.物力及人力的前提下,推广到世界各个角落,以开放源代码的方式(当然也不是完全开放所有),使其在世界范围内迅速漫延开来,到目前 ...
- OFBiz:配置过程
OFBiz使用了大量的配置文件,整个过程有点复杂.这里将配置过程大略整理了一下,方便后面查阅. 第一层:org.ofbiz.base.start.Start启动类.该类载入org/ofbiz/base ...
- ubuntu PATH 出错修复
我的 ubuntu10.10设置交叉编译环境时,PATH 设置错误了,导致无法正常启动,错误情况如下: { PATH:找不到命令ubuntu2010@ubuntu:~$ ls命令 'ls' 可在 '/ ...
- easyui框架Date日期类型以json形式显示到前台datagrid时,显示为[object Object]
如下图,easyui当后台把时间数据返回转换成json然后加载在easyui的datagrid里面,显示为[object Object] 需要对时间格式添加格式的显示方法 /** * 时间格 ...
- js日期控件
My97日期控件 官方网站 My97 Datepicker Home http://www.my97.net/
- 视频播放器控制原理:ffmpeg之ffplay播放器源代码分析
版权声明:本文由张坤原创文章,转载请注明出处: 文章原文链接:https://www.qcloud.com/community/article/535574001486630869 来源:腾云阁 ht ...