UvaLive6893_The_Big_Painting
(有任何问题欢迎留言或私聊 && 欢迎交流讨论哦
Catalog
Problem:传送门
原题目描述在最下面。
给你两个二维矩阵,问第一个矩阵在第二个矩阵中的出现次数。
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的更多相关文章
随机推荐
- WPF ItemControl的源与选择项问题
具体场景: datagrid的行中有个combox 每个行是一个实例A 每个行中的SelectedItem是实例A的一个属性B 我希望实现datagrid的复制Command,具体做法是A序列化反序列 ...
- 用 CSS 实现酷炫的动画充电效果
巧用 CSS 实现酷炫的充电动画 循序渐进,看看只使用 CSS ,可以鼓捣出什么样的充电动画效果. 画个电池 当然,电池充电,首先得用 CSS 画一个电池,这个不难,随便整一个: 欧了,勉强就是它了. ...
- python:列表、元组和字典
1.1.特点: 任意对象的有序集合 通过偏移量读取 可变长度,异构以及任意嵌套 属于可变序列的分类 对象引用数组:当把一个对象赋给一个数据结构元素或变量名时,python总会存储对 ...
- OS: 生产者消费者问题(二) ---- 系统V IPC通信-信号量和共享内存
在上一篇“OS: 生产者消费者问题(多进程+共享内存+信号量)”中提到的方法二: 如果进程之间并没有父子关系,但是协商好了共享存储的 KEY , 那么在每个进程中,就可以通过 KEY 以及 shmge ...
- Go语言基础:make,new, len, cap, append, delete方法
前面提到不少Go的内建函数,这篇文章学习下如何使用.. make 先拿 make 开刀,可是一开始我就进入了误区,因为我想先找到他的源码,先是发现 src/builtin/builtin.go 中 ...
- Dynamic partition strict mode requires at least one static partition column.
https://blog.csdn.net/huobumingbai1234/article/details/81099856
- jenkins构建参数
$BUILD_NUMBER 构建序号 $WORKSPACE 工作目录 #!/bin/sh - chmod u+x mvnw./mvnw package -Pprod -Dmaven.test.skip ...
- es概念一句话简介和注意点
1.elasticsearch是什么? 一个实时分布式搜索(全文or结构化)和分析引擎,面向文档(document oriented) 2.主节点(Master Node)职责? 负责集群中的操作(如 ...
- ajax的两种应用方式
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- python--面向对象:多态与封装
一.多态 :python 天生支持多态多态指的是一类事物有多种形态 eg:文件有多种形态:文本文件,可执行文件鸭子类型:python中崇尚鸭子类型,不崇尚根据继承所得来的相似 优点 : 松耦合 每个相 ...