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

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. C# 三层架构的一个小练习(Winfrom与SQLite数据库组合)

    本文文字方面引用 微冷的風丶(博客地址:https://www.cnblogs.com/smbk/) 代码部分是本人亲自写的一个sqlite数据库的最简单登录界面练手(当时写的太烂了,现在回顾重构一下 ...

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

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

  3. 【LeetCode 24】两两交换链表中的节点

    题目链接 [题解] 简单的链表操作 [代码] /** * Definition for singly-linked list. * struct ListNode { * int val; * Lis ...

  4. (转)4年python,总结一些改善Python程序的建议

    自己写Python也有四五年了,一直是用自己的"强迫症"在维持自己代码的质量.都有去看Google的Python代码规范,对这几年的工作经验,做个简单的笔记,如果你也在学pythp ...

  5. (2)centos7 图形界面

    1.登录 2.先安装MATE可视化桌面 yum groups install "MATE Desktop" 选择y 3.安装X Window System:图形接口 yum gro ...

  6. eclipse spring boot 项目出现java.lang.ClassCastException 解决方法

    问题 eclipse spring boot 项目出现java.lang.ClassCastException 解决方法: 重新生成项目

  7. PAT_A1024#Palindromic Number

    Source: PAT A1024 Palindromic Number (25 分) Description: A number that will be the same when it is w ...

  8. java中trim()方法是用来干什么的?

    trim()的作用是去掉字符串两端的多余的空格,注意,是两端的空格,且无论两端的空格有多少个都会去掉,当然 中间的那些空格不会被去掉,如: String s = "  a s f g     ...

  9. Django框架(八)—— 模板层:模板导入、模板继承、静态文件

    目录 模板导入.继承.静态文件 一.模板导入 1.语法 2.使用 二.模板的继承 1.使用模板的继承 2.强调(注意点) 三.静态文件 1.在配置文件settings中配置过静态文件 2.导入使用静态 ...

  10. java.lang.Double.byteValue() 方法

    java.lang.Double.byteValue() 方法(通过转换成一个字节)返回此Double为一个字节的值. 声明 以下是java.lang.Double.byteValue()方法的声明 ...