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的更多相关文章
随机推荐
- Java异常架构与异常关键字
Java异常简介 Java异常是Java提供的一种识别及响应错误的一致性机制. Java异常机制可以使程序中异常处理代码和正常业务代码分离,保证程序代码更加优雅,并提高程序健壮性.在有效使用异常的情况 ...
- 管理员技术(五): 配置文档的访问权限、 配置附加权限、绑定到LDAP验证服务、配置LDAP家目录漫游
一.配置文档的访问权限 问题: 本例要求将文件 /etc/fstab 拷贝为 /var/tmp/fstab,并调整文件 /var/tmp/fstab的权限,满足以下要求: 1> 此文件的拥有者 ...
- Win7下使用DbgPrint
在Win7下默认DbgPrint输出信息后,使用DbgView看不到内容. 新建一个reg文件,双击导出就行了. Windows Registry Editor Version 5.00 [HKEY_ ...
- python 读取设备的另一个方法
import time,sys templist = []#设置一个空列表,用来放设备内容deviceslist =[]#设置一个空列表,用来放分割后的设备内容devices = [] #设置一 ...
- git分布式版本控制系统权威指南学习笔记(二):git add暂存区的三个状态以及暂存区的理解
文章目录 不经过git add(到暂存区),能直接进行commit吗? 举个
- Java List T 去掉重复对象-java8
Stream语法详解 Stream当成一个高级版本的Iterator.原始版本的Iterator,用户只能一个一个的遍历元素并对其执行某些操作:高级版本的Stream,用户只要给出需要对其包含的元素执 ...
- 6.2_springboot2.x分布式整合Dubbo
1.分布式应用 在分布式系统中,国内常用zookeeper+dubbo组合,而Spring Boot推荐使用全栈的Spring,Spring Boot+Spring Cloud. 分布式系统: 特 ...
- java-day25
. 标签学习: 1. 文件标签:构成html最基本的标签 * html:html文档的根标签 * head:头标签.用于指定html文档 ...
- jquery插件小集合
一.滑动轮播插件Swiper Swiper官网http://www.swiper.com.cn/, 这款插件移动端,pc端均试用 二.jquery-tmpl----让你从拼接字符串中解放出来 官方下载 ...
- 不在B中的A的子串数量 HDU - 4416 (后缀自动机模板题目)
题目: 给定一个字符串a,又给定一系列b字符串,求字符串a的子串不在b中出现的个数. 题解: 先将所有的查询串放入后缀自动机(每次将sam.last=1)(算出所有子串个数) 然后将母串放入后缀自动机 ...