矩阵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个串覆盖上一个串上面,是可以得,不会超时. 要注意到一点,全部覆盖后再判断时候合法,和边放边判断, ...
随机推荐
- T-SQL 之 触发器
触发器可以做很多事情,但也会带来很多问题.正确的使用在于在适当的时候使用,而不要在不适当的时候使用它们. 触发器的一些常见用途如下: [1] 弹性参照完整性:实现很多DRI不能实现的操作(例如,跨数据 ...
- Project has no project.properties file! Edit the project properties to set one.
解决办法: 右击项目,选择android tools-->fix project properties.然后重启eclipse即可.
- nginx-rtmp流媒体服务器搭建【转】
nginx-rtmp https://github.com/pengqiuyuan/nginx-rtmp nginx-rtmp 流媒体服务器的搭建(采集桌面,手机直播) 在线Demo,直播自己的pc机 ...
- OSI模型图文说明
网关工作在第四层传输层及其以上 网络层:路由器 数据链路层:网桥,交换机 物理层:网卡,网线,集线器,中继器,调制解调器 OSI共7层:应用层,表示层,会话层,传输层,数据链路层,物理层. [7]:应 ...
- HQL的select new map ···语法
通常hibernate查询出的结果集是类似于 List<T> 或 List<Object[]> 的类型 类似于下面这个方法 public List<SfJmsfT> ...
- PHP权限控制(转)
PHP: 我这里说到的权限管理办法是一个普遍采用的方法,主要是使用到"位运行符"操作,& 位与运算符.| 位或运行符.参与运算的如果是10进制数,则会被转换至2进制数参与运 ...
- python如何获取公众号下面粉丝的openid
如何获取公众号下面粉丝的openid呢,首先要获取一个access_token,这个token可不是令牌(Token),如何获取这个access_token呢?有两种方法,方法如下: # -*- co ...
- linq to sql 去重复
ydc.GameScore.OrderByDescending(o => o.Score).GroupBy(ic => ic.UserPhone).Select(g => g.Fir ...
- Redis(十四):主从复制
当数据量变得庞大的时候,读写分离还是很有必要的.同时避免一个redis服务宕机,导致应用宕机的情况,我们启用sentinel(哨兵)服务,实现主从切换的功能. 主从复制 Redis 支持简单且易用的主 ...
- 自己定义TextView 调用ttf格式字体
方法一:自己定义TextView 调用ttf格式字体 <strong>将ttf格式文件存放在assets/fonts/下</strong> 注:PC系统字体存放在C:\Wind ...