UVALive 6893 The Big Painting hash
The Big Painting
题目连接:
http://acm.hust.edu.cn/vjudge/contest/view.action?cid=122283#problem/J
Description
Samuel W. E. R. Craft is an artist with a growing reputation.
Unfortunately, the paintings he sells do not provide
him enough money for his daily expenses plus the new supplies
he needs. He had a brilliant idea yesterday when he
ran out of blank canvas: ”Why don’t I create a gigantic
new painting, made of all the unsellable paintings I have,
stitched together?”. After a full day of work, his masterpiece
was complete.
That’s when he received an unexpected phone call: a
client saw a photograph of one of his paintings and is willing
to buy it now! He had forgotten to tell the art gallery to
remove his old works from the catalog! He would usually
welcome a call like this, but how is he going to find his old
work in the huge figure in front of him?
Given a black-and-white representation of his original
painting and a black-and-white representation of his masterpiece, can you help S.W.E.R.C. identify in
how many locations his painting might be?
Input
The input file contains several test cases, each of them as described below.
The first line consists of 4 space-separated integers: hp wp hm wm, the height and width of the
painting he needs to find, and the height and width of his masterpiece, respectively.
The next hp lines have wp lower-case characters representing his painting. After that, the next hm
lines have wm lower-case characters representing his masterpiece. Each character will be either ‘x’ or
‘o’.
Constraints:
1 ≤ hp, wp ≤ 2 000
1 ≤ hm, wm ≤ 2 000
hp ≤ hm
wp ≤ wm
Output
The painting could be in four locations as shown in the following picture. Two of the locations overlap
Sample Input
4 4 10 10
oxxo
xoox
xoox
oxxo
xxxxxxoxxo
oxxoooxoox
xooxxxxoox
xooxxxoxxo
oxxoxxxxxx
ooooxxxxxx
xxxoxxoxxo
oooxooxoox
oooxooxoox
xxxoxxoxxo
Sample Output
4
Hint
题意
给你两个只含有ox的矩阵,问你第二个矩阵内有多少个第一个矩阵
题解:
标准的二维矩阵匹配,但是这道题数据范围太大了,ac自动机T成傻逼,不要问我为什么
所以就写hash吧。。。。
代码
#include <bits/stdc++.h>
using namespace std;
const long long pr=1e9+7;
const int N=2005;
const long long p=233LL;
char s[N];
long long hl[N*N],h[N][N],xh[N][N],a[N][N],b[N][N],tt;
int main()
{
int hp,wp,hm,wm;
while(scanf("%d%d%d%d",&hp,&wp,&hm,&wm)!=EOF)
{
memset(h,0,sizeof(h));
memset(xh,0,sizeof(xh));
memset(a,0,sizeof(a));
memset(b,0,sizeof(b));
hl[0]=1LL;
for(int i=1;i<=wp*hp;i++)
{
hl[i]=(hl[i-1]*p)%pr;
}
tt=0;
for(int i=1;i<=hp;i++)
{
scanf("%s",s+1);
for(int j=1;j<=wp;j++)
{
if(s[j]=='o') a[i][j]=0;else a[i][j]=1;
tt=(tt*p+a[i][j])%pr;
}
}
for(int i=1;i<=hm;i++)
{
scanf("%s",s+1);
xh[i][0]=0;
for(int j=1;j<=wm;j++)
{
if(s[j]=='o') b[i][j]=0;else b[i][j]=1;
xh[i][j]=(xh[i][j-1]*p+b[i][j])%pr;
}
}
int cnt=0;
for(int j=1;j+wp-1<=wm;j++)
{
h[1][j]=0;
for(int k=1;k<=hp;k++)
h[1][j]=(h[1][j]*hl[wp]+(xh[k][j+wp-1]-xh[k][j-1]*hl[wp]%pr)+pr)%pr;
if(h[1][j]==tt) cnt++;
for(int i=2;i+hp-1<=hm;i++)
{
h[i][j]=((h[i-1][j]-(xh[i-1][j+wp-1]-xh[i-1][j-1]*hl[wp]%pr)*hl[wp*hp-wp]%pr)*hl[wp]%pr+
(xh[i+hp-1][j+wp-1]-xh[i+hp-1][j-1]*hl[wp]%pr)+pr+pr)%pr;
if(h[i][j]==tt) cnt++;
}
}
cout<<cnt<<endl;
}
return 0;
}
UVALive 6893 The Big Painting hash的更多相关文章
- UVALive - 6893 The Big Painting 字符串哈希
题目链接: http://acm.hust.edu.cn/vjudge/problem/129730 The Big Painting Time Limit: 5000MS 题意 给你一个模板串和待匹 ...
- LA 6893 The Big Painting(矩阵Hash)
https://vjudge.net/problem/UVALive-6893 题意: 给出一个小矩阵和大矩阵,在大矩阵中能找到相同的小矩阵. 思路: 矩阵Hash,先对小矩阵计算出它的Hash值,然 ...
- UVALive - 4513 Stammering Aliens ——(hash+二分 || 后缀数组加二分)
题意:找一个出现了m次的最长子串,以及这时的最右的位置. hash的话代码还是比较好写的,,但是时间比SA多很多.. #include <stdio.h> #include <alg ...
- 矩阵hash + KMP - UVA 12886 The Big Painting
The Big Painting Problem's Link: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=88791 M ...
- UVALive 7274 Canvas Painting (优先队列)
Canvas Painting 题目链接: http://acm.hust.edu.cn/vjudge/contest/127406#problem/C Description http://7xjo ...
- LA 6893 矩阵HASH (模板)
#include<stdio.h> #include<string.h> typedef unsigned long long ULL; ; ; int test,n,m,x, ...
- 一些简单二分题,简单的hash,H(i),字符串题
说在前面: 题是乱七八糟的. 几个二分的题. (但是我的做法不一定是二分,有些裸暴力. 1. Equations HDU - 1496 输入a,b,c,d问你这个方程有多少解.a*x1^2+b*x2^ ...
- HASH算法小结
一.简述 HASH算法的本质是特征提取——将某种不太好表示的特征,通过某种压缩的方式映射成一个值.这样,就可以优雅解决一部分难以解决的特征统计问题. 同时考虑到hash算法的本质是个概率算法,因此并不 ...
- UVALive 6507 Passwords
Passwords Time Limit: 3000ms Memory Limit: 131072KB This problem will be judged on UVALive. Original ...
随机推荐
- js 奇葩技巧之隐藏代码
昨天在群看到有人发了个文章叫<“短”化你的代码>,思路非常不错,采用unicode的零宽字符来实现字符隐藏,虽然有字符,可是你却看不见它.这篇文章详细的介绍了这种方法的实现原理,最后还给出 ...
- 20155211 2016-2017-2 《Java程序设计》第六周学习总结
20155211 2016-2017-2 <Java程序设计>第六周学习总结 教材学习内容总结 第十章 输入/输出 一.InputStream与OutputStream (一)串流设计的概 ...
- 第6月第6天 opengles 三角形
1. http://blog.csdn.net/u010963658/article/details/52691578 2.多张图 https://www.oschina.net/question/2 ...
- 理解 Memory barrier(内存屏障)【转】
转自:http://name5566.com/4535.html 参考文献列表:http://en.wikipedia.org/wiki/Memory_barrierhttp://en.wikiped ...
- Docker手动搭建sentry错误日志系统
Sentry介绍 在开发过程中,我们通过debug来排查bug,并且使用logging来记录系统的错误.但是logging有很多不足: 必须登陆到服务器查看日志文件 需要主动去查询 输出日志方式无法把 ...
- centos6.5环境DNS-本地DNS主从服务器bind的搭建
centos6.5环境DNS-本地DNS主从服务器bind的搭建 在上一篇博客中我已经搭建好了一个本地DNS服务器,能够实现正向反向解析,那么我们只需要加入一台从DNS服务器即可完成,我们来开始配置主 ...
- java虚拟机规范(se8)——java虚拟机结构(二)
2.5 运行时数据区域 java虚拟机定义了多个用于程序执行期间的运行时数据区域.这些数据区域中一些随着java虚拟机的启动而创建,随着虚拟机的退出而销毁.其他的数据区域时和线程相关的.线程相关数据区 ...
- cube-ui
cube-ui 新官网:https://didi.github.io/cube-ui/#/zh-CN
- Android 插件化 开发
插件化知识详细分解及原理 之Binder机制https://blog.csdn.net/yulong0809/article/details/56841993 插件化知识详细分解及原理 之代理,hoo ...
- norbert-构建服务器集群感知的 Java 应用程序
http://www.ibm.com/developerworks/cn/java/j-zookeeper/index.html