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

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. mongodb客户端操作常用命令(续)

    之前有写过一篇mongodb客户端的操作常用命令 ,今天接着来记录分享一些关于mongodb账户权限设置的命令操作 上期mongodb客户端的操作常用命令地址:https://www.cnblogs. ...

  2. 自定义checkbox,radio样式

    input[type=radio] { margin-right: 5px; cursor: pointer; font-size: 14px; width: 15px; height: 15px; ...

  3. Angular.js分页代码

                $scope.reloadList=function(){                             $scope.findPage( $scope.pagina ...

  4. 8、iota枚举

    1.iota常量自动生成器,每一行,自动累加1 2.iota给常量赋值使用 3.如果iota遇到const,就会重置为0 4.可以可以只写一个iota 5.如果是同一行,值是一样的 // 09_iot ...

  5. 状态压缩dp增量统计贡献——cf1238E(好题)

    这题的状态设计非常巧妙,因为dp[S]表示的并非当前正确的值,而是维护一个中间量,这个中间量在到达末状态时才正确 当然官方的题解其实更加直观,只不过理解起来其实有点困难 /* 给定一个串s,字符集为2 ...

  6. SVG和canvas

    1.SVG实现的圆环旋转效果 参考:http://www.softwhy.com/article-6472-1.html 2.SVG中的图形可以通过  transform="matrix(0 ...

  7. XMPP的总体架构和Jabber ID

    通常XMPP的架构用C/S来实现,但是也并不是强制的,Client和Server,Server和Server之间通过TCP连接来通信. 架构的简单示意图如下: C1-----S1----S2----C ...

  8. myeclipse 启动卡住的解决办法

    myeclipse 启动卡住的解决办法 今天启动myeclipse突然卡住,CPU一直占用,启动任务管理器强制关闭.重启myeclipse,重启电脑都不能够解决. 上网查找,在工程路径(工作空间的路径 ...

  9. VMware Workstation 无法打开内核设备:\\Global\\vmx86

    解决方法:win10系统,打开“服务”后右击选择使用管理员打开.然后在一大串服务中找到vm开头的服务项,全部都启动.重新启动vm就ok了(vm需要以管理员身份打开).不用复杂的代码!!

  10. jeecg随笔-3.X的生成后配置

    生成后按以上步骤进行配置即可.