(有任何问题欢迎留言或私聊 && 欢迎交流讨论哦

Catalog

Problem:传送门

Portal

 原题目描述在最下面。

 给你两个二维矩阵,问第一个矩阵在第二个矩阵中的出现次数。

Solution:

二维hash:

 直接二维矩阵hash,枚举求值即可。注意横纵base值不要取相同。枚举的时候注意一些小细节。

hash+Kmp:

 一维hash,把一个矩阵hash成一维序列。然后另一位维Kmp判断出现次数。

AC_Code:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long LL;
typedef unsigned long long uLL; const uLL base1 = 1572872831;
const uLL base2 = 1971536491;
const int MXN = 2005;
int n1, n2, m1, m2;
char ar[MXN][MXN], br[MXN][MXN];
uLL cr[MXN][MXN];
int solve(int n1,int m1,int n2,int m2) {
int cnt = 0;
uLL ans1 = 0, tmp, pw1 = 1, pw2 = 1;
for(int i = 1; i <= m1; ++i) pw1 = pw1 * base1;
for(int i = 1; i <= n1; ++i) {
tmp = 0;
pw2 = pw2 * base2;
for(int j = 1; j <= m1; ++j) {
tmp = tmp * base1 + ar[i][j];
}
ans1 = ans1 * base2 + tmp;
}//ans1是第一个矩阵的hash值
for(int i = 1; i <= n2; ++i) {
for(int j = 1; j <= m1; ++j) {
cr[i][j] = cr[i][j-1] * base1 + br[i][j];
}
for(int j = m1+1; j <= m2; ++j) {//预处理第i行第j个字母前m1的字母的一维hash值
cr[i][j] = cr[i][j-1] * base1 + br[i][j] - br[i][j-m1]*pw1;
}
}
for(int j = m1; j <= m2; ++j) {//枚举列
tmp = 0;
for(int i = 1; i <= n1; ++i) tmp = tmp * base2 + cr[i][j];
if(tmp == ans1) cnt++;
for(int i = n1 + 1; i <= n2; ++i) {//维持长度为n1
tmp = tmp * base2 + cr[i][j] - cr[i-n1][j]*pw2;
if(tmp == ans1) cnt++;
}
}
return cnt;
}
int main(){
while(~scanf("%d%d%d%d", &n1, &m1, &n2, &m2)){
for(int i = 1; i <= n1; ++i) scanf("%s", ar[i]+1);
for(int i = 1; i <= n2; ++i) scanf("%s", br[i]+1);
printf("%d\n", solve(n1,m1,n2,m2));
}
return 0;
}
/*
4 4 10 10
oxxo
xoox
xoox
oxxo
xxxxxxoxxo
oxxoooxoox
xooxxxxoox
xooxxxoxxo
oxxoxxxxxx
ooooxxxxxx
xxxoxxoxxo
oooxooxoox
oooxooxoox
xxxoxxoxxo
*/

Problem 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

For each test case, output a single integer representing the number of possible locations where his

painting might be, on a line by itself.

Sample Output Explanation

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

UvaLive6893_The_Big_Painting的更多相关文章

随机推荐

  1. 重视项目排期,对dateline 有所敬畏

    项目排期 = 个人任务完成 + 风险预估 + 意外情况 + 项目上下游依赖 个人任务完成 个人任务具体话 不要考虑私人情感,专注工作 风险预估 对可能出现的情况进行考虑 意外情况 对出现的意外情况提前 ...

  2. C# 调用c++数据类型对应

    C#调用 非托管C++ dll 传入Stringbuilder.ref string . ref char 等都报错,如mscorlib.dll 异常.其他信息: 尝试读取或写入受保护的内存.这通常指 ...

  3. Robot Framework:日志输出中文Unicode编码

    robotframework 输出日志时,中文显示为Unicode编码 . 修改方法: 在Python27\Lib\site-packages\robotframework-3.0.4-py2.7.e ...

  4. Android中如何搭建一个WebServer

    今天终于把老大交代的任务搞完了,感觉收获挺多的,所以就写一篇来记录一下吧,首先还是来看一下,老大们的需求 需求: 希望移动端的用户标识(IMEI)和HTML页面的用户标识(Cookie)连接起来,其中 ...

  5. 区间dp+预处理——cf1278F(难题)

    感觉很难的区间dp,主要是状态难想 /* 对于一个区间[i,j],设其最小的颜色编号是c=Min[i,j],那么该区间显然有一大段是以c为底的 设这个颜色在该区间出现位置的两端是L[c],R[c],那 ...

  6. VC内联汇编,引用程序中的变量

    int a=5; //变量a _asm { mov eax,a;       //将变量a的值放入寄存器eax add eax,eax;   //相当于a=a+a mov a,eax;      // ...

  7. netif_rx解析

    netif_rx函数是在网上收到数据包后,通过中断机制通知CPU而间接调用的中断处理例程. 首先,会将Packet传给netpoll框架,该框架用于在网络协议栈不可用的情况下,也能够提供给内核一个收发 ...

  8. Comet OJ - Contest #4 B题 奇偶性

    题目链接:https://www.cometoj.com/contest/39/problem/B?problem_id=1577 题意:给你一个数列,求L 到 R 区间内 所有数列 (ƒn mod ...

  9. Django框架(三)—— orm增删改查、Django生命周期

    目录 orm增删改查.Django生命周期 一.orm介绍 二.orm增删改字段 三.Django生命周期 orm增删改查.Django生命周期 一.orm介绍 1.什么是orm ORM即Object ...

  10. 20140725 快速排序时间复杂度 sTL入门

    1.快速排序的时间复杂度(平均时间复杂度为) 数组本身就有序时,效果很差为O(n^2) 2.STl入门 (1) C++内联函数(inline)和C中宏(#define)区别 内联函数有类型检查,宏定义 ...