其他分享
首页 > 其他分享> > UVA11019 Matrix Matcher【二维哈希】

UVA11019 Matrix Matcher【二维哈希】

作者:互联网

The trees have shed their leafy clothing
and their colors have faded to grays and browns
I saw a millions of trees all dusted with snow
just like out of a fairy tale
I would count the hours
minutes and seconds
until you are in my arms

今天建了什么都没发的公众号,晋江上尝试了近两千字然后文章被锁死了,本来说练练\(AC\)自动机的,结果还是用\(hash\)解决了。重新开始阅读《economist》,不禁想起高三时拼命读外刊学英语的样子,像追赶繁星的萤火一样,我追逐着那个女孩。后来我没有去她的学校,尽管分数足够。我还是选择了武汉大学,在远去的情感和茫然的前途间我做出了选择。很多选择都不是无悔的,我们只是在昨日自己建立的废墟上努力行走,就像信部图书馆门口风起而叶落,我至今没能看完《秒速五厘米》,果然两分钟的美女跳舞视频更吸引失路者们。今晚接到消息,有导师愿意接纳我参与一个简单的项目,同时ACM的关注者拜我所赐,越来越多了,昨晚有个培训机构的对我计划三个月过日语N2也表示了震惊……今天又被喊大佬,他们与其说是在开玩笑,不如说是在试探虚实。上一次被喊大佬是什么时候呢,那之后的展开可不有趣。

当他们认为你有枪的时候,你最好真的有

简单写法
# include "bits/stdc++.h"
using namespace std;
constexpr int N = 1e3 + 3;
constexpr unsigned long long base_1 = 131, base_2 = 1331;
char txt[N][N], pat[N][N];
unsigned long long b_1[N], b_2[N], h_1[N][N], h_2[N][N]; 
int main() {
	int test;
	scanf("%d", &test);
	b_1[0] = 1, b_2[0] = 1;
	for(int i = 1; i < N; ++i) b_1[i] = b_1[i - 1] * base_1;
	for(int i = 1; i < N; ++i) b_2[i] = b_2[i - 1] * base_2;
	while(test--) {
		int n, m;
		scanf("%d%d", &n, &m);
		for(int i = 1; i <= n; ++i) {
			scanf("%s", txt[i] + 1);
		}
		int X, Y;
		scanf("%d%d", &X, &Y);
		for(int i = 1; i <= X; ++i) {
			scanf("%s", pat[i] + 1);
		}
		
		for(int i = 1; i <= n; ++i) { // step 1
			for(int j = 1; j <= m; ++j) {
				h_1[i][j] = h_1[i][j - 1] * base_1 + txt[i][j];
			}
		}
		for(int i = 1; i <= n; ++i) { // step 2
			for(int j = 1; j <= m; ++j) {
				h_1[i][j] = h_1[i][j] + h_1[i - 1][j] * base_2;
			}
		}
		
		for(int i = 1; i <= X; ++i) {
			for(int j = 1; j <= Y; ++j) {
				h_2[i][j] = h_2[i][j - 1] * base_1 + pat[i][j];
			}
		}
		for(int i = 1; i <= X; ++i) {
			for(int j = 1; j <= Y; ++j) {
				h_2[i][j] = h_2[i][j] + h_2[i - 1][j] * base_2;
			}
		}
		
		int ans = 0;
		for(int i = 1; i + X - 1 <= n; ++i) {
			for(int j = 1; j + Y - 1 <= m; ++j) {
				unsigned long long tmp = h_1[i + X - 1][j + Y - 1] - h_1[i + X - 1][j - 1] * b_1[Y] - h_1[i - 1][j + Y - 1] * b_2[X] + h_1[i - 1][j - 1] * b_1[Y] * b_2[X];
				if(h_2[X][Y] == tmp) {
					++ans;
				}
			}
		}
		printf("%d\n", ans);
	}
	return 0;
}
简便写法(个人偏好)
# include "bits/stdc++.h"
using namespace std;
constexpr int N = 1e3 + 3;
constexpr unsigned long long base_1 = 131, base_2 = 1331;
char txt[N][N], pat[N][N];
unsigned long long b_1[N], b_2[N], h_1[N][N], h_2[N][N]; 
int main() {
	int test;
	scanf("%d", &test);
	b_1[0] = 1, b_2[0] = 1;
	for(int i = 1; i < N; ++i) b_1[i] = b_1[i - 1] * base_1;
	for(int i = 1; i < N; ++i) b_2[i] = b_2[i - 1] * base_2;
	while(test--) {
		int n, m;
		scanf("%d%d", &n, &m);
		for(int i = 1; i <= n; ++i) {
			scanf("%s", txt[i] + 1);
		}
		int X, Y;
		scanf("%d%d", &X, &Y);
		for(int i = 1; i <= X; ++i) {
			scanf("%s", pat[i] + 1);
		}
		
		for(int i = 1; i <= n; ++i) { // one-step
			for(int j = 1; j <= m; ++j) {
				h_1[i][j] = h_1[i - 1][j] * base_1 + h_1[i][j - 1] * base_2 - h_1[i - 1][j - 1] * base_1 * base_2 + txt[i][j];
			}
		}
		
		for(int i = 1; i <= X; ++i) {
			for(int j = 1; j <= Y; ++j) {
				h_2[i][j] = h_2[i - 1][j] * base_1 + h_2[i][j - 1] * base_2 - h_2[i - 1][j - 1] * base_1 * base_2 + pat[i][j];
			}
		}
		
		int ans = 0;
		for(int i = 1; i + X - 1 <= n; ++i) {
			for(int j = 1; j + Y - 1 <= m; ++j) {
				unsigned long long tmp = h_1[i + X - 1][j + Y - 1] - h_1[i - 1][j + Y - 1] * b_1[X] - h_1[i + X - 1][j - 1] * b_2[Y] + h_1[i - 1][j - 1] * b_1[X] * b_2[Y];
				// where there is a difference in one dimension, the corresponding B[] of it shall be used
				// pay attention, note where i - 1 exists
				if(h_2[X][Y] == tmp) {
					++ans;
				}
			}
		}
		printf("%d\n", ans);
	}
	return 0;
}

标签:std,Matrix,int,Matcher,long,constexpr,base,哈希,test
来源: https://www.cnblogs.com/bingoyes/p/16611533.html