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的更多相关文章
随机推荐
- PHP ftp_chmod() 函数
定义和用法 ftp_chmod() 函数设置 FTP 服务器上指定文件的权限. 如果成功,该函数返回新的权限.如果失败,则返回 FALSE 和一个警告. 语法 ftp_chmod(ftp_connec ...
- 「CTS2019 | CTSC2019」氪金手游 解题报告
「CTS2019 | CTSC2019」氪金手游 降 智 好 题 ... 考场上签到失败了,没想容斥就只打了20分暴力... 考虑一个事情,你抽中一个度为0的点,相当于把这个点删掉了(当然你也只能抽中 ...
- Mysql LOAD DATA读取客户端任意文件漏洞复现(原理分析)
环境搭建 怎么设置Mysql支持外联? use mysql; grant all privileges on *.* to root@'%' identified by '密码'; //授权语句 fl ...
- WebStorm编辑器
Webstorm的安装和免费使用 IntelliJ IDEA 注册码 (webstorm注册码):http://idea.lanyus.com/ (已经不能获取注册码了,之前的注册码可以重复使用) 安 ...
- Linux文件大小 指令&编程
在工作和日常的编程中时常需要确定文件的大小,一些基本的查看方式在此做一个总结. 一. linux shell环境下 df可以查看一级文件夹大小.使用比例.档案系统及其挂入点,但对文件却无能为力. ...
- 转-C++之虚函数不能定义成内联函数的原因
转自:https://blog.csdn.net/flydreamforever/article/details/61429140 在C++中,inline关键字和virtual关键字分别用来定义c+ ...
- ES6_Demo,模拟后台json数据展示
最近在学习ES6,下面,模拟后台传过来json数据,并在页面展示的一个小Demo. 页面简单的不可描述,只有一个button按钮 <button>点击获取json数据</button ...
- python+tushare获取股票每日停复牌信息
接口:suspend 更新时间:不定期 描述:获取股票每日停复牌信息 注:tushare模块下载和安装教程,请查阅我之前的文章 输入参数 名称 | 类型 | 必 ...
- 【gcc】更新下载编译gcc遇到的各种问题
帮学长的oj升级gcc版本.遇到了贼多问题.. [悲惨的开始] 安装gcc版本推荐ustc的mirror的下载,超快der... https://mirrors.ustc.edu.cn/gnu/gcc ...
- No package docker-io available
新手centos6.8安装docker时从遇到No package docker-io available开始的各种不小心的坑... 新安装了CentOS6.8,准备安装docker,执行命令 yum ...