HOJ 13383 The Big Painting 二维字符串hash
来源:互联网 发布:游奇网络加班 编辑:程序博客网 时间:2024/05/22 17:31
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?
给你两个字符矩阵,求一个字符矩阵在另一个矩阵里出现的次数
直接搜了一个二维字符串hash的板子,交上去了
#include<cstdio>#include<iostream>using namespace std;#define maxn 2005#define ULL unsigned long longULL maxSize = 4000007;ULL seedx = 233;ULL seedy = 23333;struct HashString {ULL xp[maxn];ULL yp[maxn];ULL H[maxn][maxn];char s[maxn][maxn];int x,y;void init(int n, int m) {xp[0] = yp[0] = 1;x = n,y = m;for(int i = 1; i <= x; i++) xp[i] = xp[i - 1]*seedx;for(int i = 1; i <= y; i++) yp[i] = yp[i - 1]*seedy;return;}void initHash() {for(int i = 0; i < x; i++) {H[i][0] = s[i][0] - 'A' + 1;for(int j = 1; j < y; j++)H[i][j] = H[i][j - 1]*seedy + s[i][j] - 'A' + 1;}for(int i = 1; i < x; i++)//从1开始, 第0行是不变的for(int j = 0; j < y; j++)H[i][j] += H[i - 1][j]*seedx;return;}ULL askHash(int x1, int y1, int x2, int y2) { //询问以(x1, y1)为左上角, (x2, y2)为右上角的字符矩阵的hash值ULL ret = H[x2][y2];if(x1 > 0) ret -= H[x1 - 1][y2]*xp[x2 - x1 + 1];if(y1 > 0) ret -= H[x2][y1 - 1]*yp[y2 - y1 + 1];if(x1 > 0 && y1 > 0) ret += H[x1 - 1][y1 - 1]*xp[x2 - x1 + 1]*yp[y2 - y1 + 1];return ret;}void read(){ for(int i =0;i<x;i++) scanf("%s",s[i]);}}s1,s2;int main() { #ifdef ACBang freopen("1.in","r",stdin); #endif // ACBang int x,y,n,m; while(scanf("%d%d%d%d",&x,&y,&n,&m)!=EOF) { s1.init(x,y); s1.read(); s2.init(n,m); s2.read(); s1.initHash(); s2.initHash(); ULL tt = s1.askHash(0,0,x-1,y-1); int ans = 0; for(int i = 0;i<n-x+1;i++) { for(int j = 0;j<m-y+1;j++) { if(tt == s2.askHash(i,j,i+x-1,j+y-1)) ans ++; } } printf("%d\n",ans); }return 0;}
- HOJ 13383 The Big Painting 二维字符串hash
- UVALive 6893 The Big Painting(二维hash,暴力)
- HNU13383:The Big Painting
- 字符串hash(二维)-poj3690
- HOJ 1296 Big Number
- URAL 1486(二维字符串hash)
- POJ 3690 字符串的二维hash
- Painting the Web
- sgu183 Painting the balls_dp
- [cf398B]Painting The Wall
- Painting The Wall CodeForces
- codeforces 869E The Untended Antiquity 二维BIT + hash
- URAL 1486 Equal Squares 二维字符串Hash + 邻接表分组
- UVA 261 - The Window Property(字符串Hash)
- HOJ The Colored Cubes
- HOJ 3074 The Position
- HOJ 2785 The Game
- Hoj 1140 The Game
- 二分查找详细介绍
- Hdoj 2467 Reward(拓扑排序)
- POJ-1830 开关问题(高斯消元)
- Num 30 : NYOJ : 0047 过河问题 [ 贪心 ]
- 新浪微博OAuth2.0授权
- HOJ 13383 The Big Painting 二维字符串hash
- C/C++ extern与static关键字
- 周记——20150810
- backbone.js快速入门(二)
- MIUI 6 沉浸式状态栏(Android 4.4以上)
- 华三,思科ACL命令解析
- LeetCode(21)Merge Two Sorted Lists
- 解包Assets.car
- 正则表达式基本语法