矩阵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个串覆盖上一个串上面,是可以得,不会超时. 要注意到一点,全部覆盖后再判断时候合法,和边放边判断, ...
随机推荐
- 从头认识java-13.11 对照数组与泛型容器,观察类型擦除给泛型容器带来什么问题?
这一章节我们继续类型擦除的话题,我们将通过对照数组与泛型容器,观察类型擦除给泛型容器带来什么问题? 1.数组 package com.ray.ch13; public class Test { pub ...
- RaceWeb介绍(7):由500强公司数据高速生成百度地图——生成坐标字段及坐标数据
接上篇. 一.生成X坐标.Y坐标两个字段. 我们须要为每一个公司建立X坐标和Y坐标字段,用来保存XY坐标. 既然为了突出"快",这一步就有程序来完毕吧. 右键单击"世界5 ...
- 微信小程序 - 五星评分(含半分)
转载自:http://blog.csdn.net/column/details/13721.html 演示: 下载:小程序-星级评论.zip
- 格式化文本数据抽取工具awk
在管理和维护Linux系统过程中,有时可能需要从一个具有一定格式的文本(格式化文本)中抽取数据,这时可以使用awk编辑器来完成这项任务.发明这个工具的作者是Aho.Weinberg和Kernighan ...
- Patterns-Observer
http://book.javanb.com/java-design-patterns/index.html Java深入到一定程度,就不可避免的碰到设计模式(design pattern)这一概念, ...
- VS下控制台执行保持(不要一闪而过)
曾经上课的时候是用VC++6.0来学习编程的,编完打印出来的东西就直接显示在控制台上.而在Visual Studio下会出现控制台一闪而过的情况.这个问题事实上是非常好解决的.方法有多种.以下列举两种 ...
- WPF显示经常使用的几个显示文字控件TextBox, TextBlock, Lable
TextBox, TextBlock. Lable 当中TextBox 和Lable均继承了Control类 能够对其进行模板编辑. 而TextBlock没有继承Control所以不能对其进行模板编辑 ...
- javascript 原生实现 jquery live/delegate
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xht ...
- 用Drupal快速实现mobile平台服务端【转】
原文地址:http://www.terrysco.com/node/drupal-as-mobile-backend.html 用Drupal很容易实现一个API,让手机平台或者其他系统使用json的 ...
- 点滴的积累---初学Javascript
在学习知识的路上,我们须要的不断的去接触新的知识,同一时候我们也不要不停地对自己旧的知识进行总结.近期通过<牛腩Javascript>和姜昊的<Javascript专题视频>对 ...