UVALive 6893 The Big Painting hash
2016-07-16 23:51
417 查看
The Big Painting
题目连接:
http://acm.hust.edu.cn/vjudge/contest/view.action?cid=122283#problem/JDescription
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 overlapSample Input
4 4 10 10oxxo
xoox
xoox
oxxo
xxxxxxoxxo
oxxoooxoox
xooxxxxoox
xooxxxoxxo
oxxoxxxxxx
ooooxxxxxx
xxxoxxoxxo
oooxooxoox
oooxooxoox
xxxoxxoxxo
Sample Output
4Hint
题意
给你两个只含有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 ; long long hl[N*N],h ,xh ,a ,b ,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; }
相关文章推荐
- HDU 2476 String painter (区间DP)
- UVALive 6885 Flowery Trails 最短路
- HighAvailability
- UVA 442 Matrix Chain Multiplication
- Mybaits深入了解(三)----mybatis开发Dao的方法
- Mybaits深入了解(二)—-入门实例
- main中的数组args传值
- LeetCode-336.Palindrome Pairs
- hdu 1789 Doing Homework again 贪心
- 邮件服务器(postfix + dovecot + extmail + extman + MailScanner + spamassassin + ClamAV )
- 微软开源Malmo AI项目平台 利用Minecraft构建复杂机器人任务
- Training2:位操作训练
- HDU 5319 Painter(暴力枚举)
- 磁盘阵列 RAID 技术原理详解
- 浅谈RAID写惩罚(Write Penalty)与IOPS计算
- AVAudioplayer时error解决 创建失败 ERror creating player: Error Domain=NSOSStatusErrorDomain Code=2003334207 "(null)"
- 安装cocoapod提示certificate verify failed错误
- 【BNUOJ】【第十四届北京师范大学程序设计竞赛决赛】D. Air Hockey
- 如何解决failed to push some refs to git
- Service跨进程调用服务三部曲之AIDL详解(三)