UVALive 6893 The Big Painting hash
The Big Painting
题目连接:
http://acm.hust.edu.cn/vjudge/contest/view.action?cid=122283#problem/J
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
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
Hint
题意
给你两个只含有ox的矩阵,问你第二个矩阵内有多少个第一个矩阵
题解:
标准的二维矩阵匹配,但是这道题数据范围太大了,ac自动机T成傻逼,不要问我为什么
所以就写hash吧。。。。
代码
#include <bits/stdc++.h>
using namespace std;
const long long pr=1e9+7;
const int N=2005;
const long long p=233LL;
char s[N];
long long hl[N*N],h[N][N],xh[N][N],a[N][N],b[N][N],tt;
int main()
{
int hp,wp,hm,wm;
while(scanf("%d%d%d%d",&hp,&wp,&hm,&wm)!=EOF)
{
memset(h,0,sizeof(h));
memset(xh,0,sizeof(xh));
memset(a,0,sizeof(a));
memset(b,0,sizeof(b));
hl[0]=1LL;
for(int i=1;i<=wp*hp;i++)
{
hl[i]=(hl[i-1]*p)%pr;
}
tt=0;
for(int i=1;i<=hp;i++)
{
scanf("%s",s+1);
for(int j=1;j<=wp;j++)
{
if(s[j]=='o') a[i][j]=0;else a[i][j]=1;
tt=(tt*p+a[i][j])%pr;
}
}
for(int i=1;i<=hm;i++)
{
scanf("%s",s+1);
xh[i][0]=0;
for(int j=1;j<=wm;j++)
{
if(s[j]=='o') b[i][j]=0;else b[i][j]=1;
xh[i][j]=(xh[i][j-1]*p+b[i][j])%pr;
}
}
int cnt=0;
for(int j=1;j+wp-1<=wm;j++)
{
h[1][j]=0;
for(int k=1;k<=hp;k++)
h[1][j]=(h[1][j]*hl[wp]+(xh[k][j+wp-1]-xh[k][j-1]*hl[wp]%pr)+pr)%pr;
if(h[1][j]==tt) cnt++;
for(int i=2;i+hp-1<=hm;i++)
{
h[i][j]=((h[i-1][j]-(xh[i-1][j+wp-1]-xh[i-1][j-1]*hl[wp]%pr)*hl[wp*hp-wp]%pr)*hl[wp]%pr+
(xh[i+hp-1][j+wp-1]-xh[i+hp-1][j-1]*hl[wp]%pr)+pr+pr)%pr;
if(h[i][j]==tt) cnt++;
}
}
cout<<cnt<<endl;
}
return 0;
}
UVALive 6893 The Big Painting hash的更多相关文章
- UVALive - 6893 The Big Painting 字符串哈希
题目链接: http://acm.hust.edu.cn/vjudge/problem/129730 The Big Painting Time Limit: 5000MS 题意 给你一个模板串和待匹 ...
- LA 6893 The Big Painting(矩阵Hash)
https://vjudge.net/problem/UVALive-6893 题意: 给出一个小矩阵和大矩阵,在大矩阵中能找到相同的小矩阵. 思路: 矩阵Hash,先对小矩阵计算出它的Hash值,然 ...
- UVALive - 4513 Stammering Aliens ——(hash+二分 || 后缀数组加二分)
题意:找一个出现了m次的最长子串,以及这时的最右的位置. hash的话代码还是比较好写的,,但是时间比SA多很多.. #include <stdio.h> #include <alg ...
- 矩阵hash + KMP - UVA 12886 The Big Painting
The Big Painting Problem's Link: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=88791 M ...
- UVALive 7274 Canvas Painting (优先队列)
Canvas Painting 题目链接: http://acm.hust.edu.cn/vjudge/contest/127406#problem/C Description http://7xjo ...
- LA 6893 矩阵HASH (模板)
#include<stdio.h> #include<string.h> typedef unsigned long long ULL; ; ; int test,n,m,x, ...
- 一些简单二分题,简单的hash,H(i),字符串题
说在前面: 题是乱七八糟的. 几个二分的题. (但是我的做法不一定是二分,有些裸暴力. 1. Equations HDU - 1496 输入a,b,c,d问你这个方程有多少解.a*x1^2+b*x2^ ...
- HASH算法小结
一.简述 HASH算法的本质是特征提取——将某种不太好表示的特征,通过某种压缩的方式映射成一个值.这样,就可以优雅解决一部分难以解决的特征统计问题. 同时考虑到hash算法的本质是个概率算法,因此并不 ...
- UVALive 6507 Passwords
Passwords Time Limit: 3000ms Memory Limit: 131072KB This problem will be judged on UVALive. Original ...
随机推荐
- JS种this的四种用法
记住以下四点: 1.没调用对象就指向全局对象 2.有对象就指向调用对象 3.用new构造就指向新对象 4.通过 apply 或 call 或 bind 来改变 this 的所指. 1.测试一:没调用对 ...
- python之pip安装mysql-python失败
前言 由于公司使用的python版本是python2,并且连接mysql的包是mysql-python,但是mysql-python 使用pip安装报错,需要C++环境等依赖,于是使用wheel直接安 ...
- elasticsearch常见异常及解决办法
报错信息:Java HotSpot(TM) 64-Bit Server VM warning: INFO: os::commit_memory(0x0000000085330000, 20602552 ...
- Gitlab & Github
windwos上Git的使用 软件下载地址:https://github.com/git-for-windows/git/releases/download/v2.15.1.windows.2/Git ...
- js各种小知识
1.获取函数里面的参数个数 function test(x,y,z){} // 获取test参数的个数 console.log(test.length)
- Laravel 的计划任务
避免并发执行 $schedule->command('emails:send')->withoutOverlapping(); 这里需要注意,对于 call function 定义的计划任 ...
- laravel 辅助函数
数组&对象 1.array_divide() array_divide 函数返回两个数组,一个包含原始数组的健,另一个包含原始数组的值 [$keys, $values] = array_div ...
- MVC:分页改进URL
http://localhost/?page=2 可以根据"可组合URL"创建一种更具吸引力的URL方案: http://localhost/page2 publi ...
- sklearn GMM模型介绍
参考 SKlearn 库 EM 算法混合高斯模型参数说明及代码实现 和 sklearn.mixture.GaussianMixture 以前的推导内容: GMM 与 EM 算法 记录下 ...
- 2018 Arab Collegiate Programming Contest (ACPC 2018) H - Hawawshi Decryption 数学 + BSGS
H - Hawawshi Decryption 对于一个给定的生成数列 R[ 0 ] 已知, (R[ i - 1 ] * a + b) % p = R[ i ] (p 是 质数), 求最小的 x 使得 ...