poj 3690 Constellations 二维滚动哈希匹配

//poj 3690
//sep9
#include <iostream>
#include <set>
using namespace std;
typedef unsigned long long ull;

const int MAXN=1024;
const int MAXT=128;
const int MAXP=64;
const int B1=9973;
const int B2=100000007;
char sky[MAXN][MAXN];
char patterns[MAXT][MAXN][MAXN];
int n,m,t,p,q;
ull hash[MAXN][MAXN];

void compute_hash(char a[MAXN][MAXN],int row,int col)
{
	ull x=1;
	for(int i=0;i<q;++i)	
		x=x*B1;
	for(int i=0;i<row;++i){
		ull e=0;
		for(int j=0;j<q;++j)
			e=B1*e+a[i][j];
		for(int j=0;j+q<=col;++j){
			hash[i][j]=e;
			if(j+q<m)
		 		e=B1*e+a[i][j+q]-x*a[i][j];
		}	
	}
	ull y=1;
	for(int i=0;i<p;++i)
		y=y*B2;
	for(int i=0;i+q<=col;++i){
		ull e=0,tmp;
		for(int j=0;j<p;++j)
			e=B2*e+hash[j][i];
		for(int j=0;j+p<=row;++j){
			tmp=hash[j][i];
			hash[j][i]=e;
			if(j+p<n)
				e=B2*e+hash[j+p][i]-y*tmp;
		}
	}
}

int main()
{
	int cases=0;
	while(scanf("%d%d%d%d%d",&n,&m,&t,&p,&q)==5){
		if(n==0&&m==0&&t==0&&p==0&&q==0) break;
		for(int i=0;i<n;++i)
			scanf("%s",sky[i]);
		for(int i=0;i<t;++i)
			for(int j=0;j<p;++j)
				scanf("%s",patterns[i][j]);
		multiset<ull> myset;
		for(int i=0;i<t;++i){
			compute_hash(patterns[i],p,q);
			myset.insert(hash[0][0]);	
		}
		compute_hash(sky,n,m);
		for(int i=0;i+p<=n;++i)
			for(int j=0;j+q<=m;++j)
				myset.erase(hash[i][j]);	
		printf("Case %d: %d\n",++cases,t-myset.size());
	}
} 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值