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

UVA11019 Matrix Matcher【二维哈希】

时间:2022-08-22 01:22:05浏览次数:53  
标签:std Matrix int Matcher long constexpr base 哈希 test

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
From: https://www.cnblogs.com/bingoyes/p/16611533.html

相关文章

  • 一致性哈希算法
    一致性哈希算法主要应用于Redis分布式缓存问题引出在单节点的情况下,Redis缓存不用担心命中率的问题,但是一旦上升到分布式的架构中,可能会造成一台机器有缓存而另一台机器......
  • Fisher Information Matrix when Changing Variables
    CopiedfromthislinkFornormal\(X\simN(\mu,\sigma^2)\),informationmatrixis\[\mathcal{I}_1=\left(\begin{matrix}\frac{1}{\sigma^2}&0\\0&\frac{1......
  • 【2022牛客多校】第五场 Crazy Thursday 二分+字符串哈希
    https://ac.nowcoder.com/acm/contest/33190/G题意给你一个长为n的字符串s,求s中分别以'k'、'f'、'c'结尾的回文串数量\(n<=5e5\)思路首先暴力枚举区间端点加判断,为......
  • codeforces963D. Frequency of String【哈希】
    我的腿让我停下,可是我的心却不许我这么做今天又是为了明知多半不可能的事情奔波一早,一天里,出了很多丑,犯了很多错,见了很多人,有了很多意想不到的收获,我选择了我的生存方式......
  • 哈希
    字符串哈希哈希是一种高效判断字符串相等的算法,准确来说是子串,因为预处理需要\(O(n)\),自然是多次使用才划算常见的哈希式是\(\suma_ibase^i(mod\mod)\)即选取一个......
  • 哈希类型,列表类型,集合类型,有序集合类型,慢查询,pipline与事务,发布订阅,Bitmap,HyperLogLog
    1API的使用1.1哈希类型###1---hget,hset,hdelhgetkeyfield#获取hashkey对应的field的value时间复杂度为o(1)hsetkeyfieldvalue#设置hashkey对应的field......
  • 哈希表4:两数之和(1)
    本题如下:(链接:https://leetcode.cn/problems/two-sum/)题目:给定一个整数数组nums 和一个整数目标值target,请你在该数组中找出和为目标值target 的那 两个 整数,并返......
  • [LeetCode] 1314. Matrix Block Sum 矩阵区域和
    Givena mxn matrix mat andaninteger k,return amatrix answer whereeach answer[i][j] isthesumofallelements mat[r][c] for:i-k<=r<=......
  • Redis---hash哈希散列
    1.前言Redishash(哈希散列)是由字符类型的field(字段)和value组成的哈希映射表结构(也称散列表),它非常类似于表格结构。在hash类型中,field与value一一对应,且不允许重......
  • 子数组异或和(前缀和、哈希)
    题意给定一个长度为\(n\)的整数数组\(a_1,a_2,\dots,a_n\)。请你统计一共有多少个数组\(a\)的非空连续子数组能够同时满足以下所有条件:该连续子数组的长度为偶数。......