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. Red Pen - 快速高效的获取设计项目的反馈

    Red Pen 让设计师能够快速,高效的从你的同事和客户获取反馈.只需要简单的拖放图像到 Red Pen 主页,然后把生成的链接分享给你的同事或者客户.他们打开链接就能看到设计稿,并给予实时的反馈,所 ...

  2. innodb和myisam数据库文件存储详解以及mysql表空间

    数据库常用的两种引擎有Innodb和Myisam,关于二者的区别参考:https://www.cnblogs.com/qlqwjy/p/7965460.html 1.关于数据库的存储在两种引擎的存储是 ...

  3. nodejs 配置服务自启动

    1安装包 输入以下命令,安装需要的包 npm install node-windows -g 2编写自启动js 在目标server.js目录下新建auto_start_nodejs.js文件,将以下j ...

  4. C语言内存分布

    C语言内存分布 典型的C语言程序内存表示分区共有5个部分: 正文段 Text segment 已初始化数据段(数据段)Initialized data segment 未初始化数据段(bss)Unin ...

  5. APP的CPU,内存,耗电,流量测试工具

    APP的CPU,内存,耗电,流量测试工具下载地址,后续文章会介绍如何使用Emmagee.itest.gt APP应用的CPU,内存,耗电,流量调查 可和同类产品比较,使用GT等工具:CPU靠syste ...

  6. 001_chrome工具详解

    一.chrome https://segmentfault.com/a/1190000000683599

  7. Java 语言多态性

    https://www.ibm.com/developerworks/cn/java/java-language-polymorphism/index.html 定义多态性 多态性是面向对象编程中的一 ...

  8. opencv 彩色图像分割(inrange)

    灰度图像大多通过算子寻找边缘和区域生长融合来分割图像. 彩色图像增加了色彩信息,可以通过不同的色彩值来分割图像,常用彩色空间HSV/HSI, RGB, LAB等都可以用于分割! 笔者主要介绍inran ...

  9. 浅谈HIbernate

    Hiberbate是面向对象,需要把对象和数据库进行映射.与数据库无关,操作的是对象,会根据数据源和数据库的方言生成对应的sql语句进行查询,是一个优秀的java持久层解决方案,是当今主流的对象-关系 ...

  10. springMVC:将controller中数据传递到jsp页面

    1> 将方法的返回值该为ModelAndView在返回时,将数据存储在ModelAndView对象中如: newModelAndView("/WEBINF/jsp/showData.j ...