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的更多相关文章

  1. UVALive - 6893 The Big Painting 字符串哈希

    题目链接: http://acm.hust.edu.cn/vjudge/problem/129730 The Big Painting Time Limit: 5000MS 题意 给你一个模板串和待匹 ...

  2. LA 6893 The Big Painting(矩阵Hash)

    https://vjudge.net/problem/UVALive-6893 题意: 给出一个小矩阵和大矩阵,在大矩阵中能找到相同的小矩阵. 思路: 矩阵Hash,先对小矩阵计算出它的Hash值,然 ...

  3. UVALive - 4513 Stammering Aliens ——(hash+二分 || 后缀数组加二分)

    题意:找一个出现了m次的最长子串,以及这时的最右的位置. hash的话代码还是比较好写的,,但是时间比SA多很多.. #include <stdio.h> #include <alg ...

  4. 矩阵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 ...

  5. UVALive 7274 Canvas Painting (优先队列)

    Canvas Painting 题目链接: http://acm.hust.edu.cn/vjudge/contest/127406#problem/C Description http://7xjo ...

  6. LA 6893 矩阵HASH (模板)

    #include<stdio.h> #include<string.h> typedef unsigned long long ULL; ; ; int test,n,m,x, ...

  7. 一些简单二分题,简单的hash,H(i),字符串题

    说在前面: 题是乱七八糟的. 几个二分的题. (但是我的做法不一定是二分,有些裸暴力. 1. Equations HDU - 1496 输入a,b,c,d问你这个方程有多少解.a*x1^2+b*x2^ ...

  8. HASH算法小结

    一.简述 HASH算法的本质是特征提取——将某种不太好表示的特征,通过某种压缩的方式映射成一个值.这样,就可以优雅解决一部分难以解决的特征统计问题. 同时考虑到hash算法的本质是个概率算法,因此并不 ...

  9. UVALive 6507 Passwords

    Passwords Time Limit: 3000ms Memory Limit: 131072KB This problem will be judged on UVALive. Original ...

随机推荐

  1. 浅谈 js 下 with 对性能的影响

    这几天多次看到有博主们在写 with 的文章,这货确实非常方便,但是却是个性能杀手,所以一直都是上不得台面的.那么他究竟会让效率低下到什么程度呢?先来看下 with 是如何的便捷吧.. // 正常调用 ...

  2. centos安装lrzsz

    yum -y install lrzsz 使用rz打开上传框

  3. oracle用户密码过期!the password has expired

    Oracle提示错误消息ORA-28001: the password has expired,是由于Oracle11G的新特性所致, Oracle11G创建用户时缺省密码过期限制是180天(即6个月 ...

  4. 第10月第4天 Mac g++ sfml opendir

    1. g++ OpenGL.cpp -I/Users/temp/Downloads/SFML-2.4.2-osx-clang/include -L/usr/local/lib -framework O ...

  5. Eric6启动时“无法定位序数4540于动态链接库LIBEAY32.dll”的错误

    参考自:https://blog.csdn.net/HongAndYi/article/details/80721478 在安装PyQt5的编程环境时,安装Eric6-17.12后运行eric6,却出 ...

  6. 【逆向工具】IDA Python安装与使用

    1.IDA Pyhon介绍 IDA Python是IDA6.8后自带插件,可以使用Python做很多的辅助操作,非常方便的感觉. 2.IDA Python安装 从github上IDAPython项目获 ...

  7. 如何对xilinx FPGA进行bit文件加密

    记录背景:最近在用Vivado评估国外一个公司所提供的ISE所建的工程时,由于我并没有安装ISE工程,因此将其提供的所有v文件导入到Vivado中,对其进行编译.添加完之后成功建立顶层文件,但奇怪的是 ...

  8. C# 百度搜索结果xpath分析

    using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net ...

  9. java垃圾回收的回收器

    回收器的种类: --串行(–XX:+UseSerialGC ) Out ofBox算法,年轻代串行复制,年老代串行标记整理,主要用于桌面应用 --并行(–XX:+UseParallelGC ) 年轻代 ...

  10. poj2709

    模拟题,在合成灰色的时候,每次取当前剩余最多的三种颜色,各取1mL合成.然后重新看剩余最多的是哪三个. #include <cstdio> #include <cstdlib> ...