BZOJ2462: [BeiJing2011]矩阵模板

哈希

题目传送门

二维哈希就好了。注意不要用unsigned long long,会T的(BZOJ 32位机子)。

代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
#define N 1005
#define M 105
#define F inline
using namespace std;
typedef unsigned int uint;
const uint bs1=3,bs2=5,p=99999971;
int n,m,a,b,q; char s[N][N],c[M][M];
uint pw1[N],pw2[N],h[N][N],tmp[M][M];
bool hsh[p];
int main(){
	scanf("%d%d%d%d",&n,&m,&a,&b);
	for (int i=1;i<=n;i++) scanf("%s",s[i]+1);
	pw1[0]=pw2[0]=1;
	for (int i=1;i<=m;i++) pw1[i]=pw1[i-1]*bs1;
	for (int i=1;i<=n;i++) pw2[i]=pw2[i-1]*bs2;
	for (int i=1;i<=n;i++)
		for (int j=1;j<=m;j++)
			h[i][j]+=h[i][j-1]*bs1+s[i][j]-48;
	for (int j=1;j<=m;j++)
		for (int i=1;i<=n;i++)
			h[i][j]+=h[i-1][j]*bs2;
	for (int i=a;i<=n;i++)
		for (int j=b;j<=m;j++)
			hsh[(h[i][j]+h[i-a][j-b]*pw1[b]*pw2[a]-h[i-a][j]*pw2[a]-h[i][j-b]*pw1[b])%p]=true;
	for (scanf("%d",&q);q;q--){
		for (int i=1;i<=a;i++)
			for (int j=1;j<=b;j++) tmp[i][j]=0;
		for (int i=1;i<=a;i++){
			scanf("%s",c[i]+1);
			for (int j=1;j<=b;j++)
				tmp[i][j]+=tmp[i][j-1]*bs1+c[i][j]-48;
		}
		for (int j=1;j<=b;j++)
			for (int i=1;i<=a;i++)
				tmp[i][j]+=tmp[i-1][j]*bs2;
		puts(hsh[(tmp[a][b]+p)%p]?"1":"0");
	}
	return 0;
}
发布了459 篇原创文章 · 获赞 479 · 访问量 11万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 书香水墨 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览