您的位置:首页 > 其它

poj 3690 Constellations 二维滚动哈希匹配

2015-10-15 00:07 316 查看
//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());
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  poj 算法