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()); } }
相关文章推荐
- 动易2006序列号破解算法公布
- Ruby实现的矩阵连乘算法
- C#插入法排序算法实例分析
- 超大数据量存储常用数据库分表分库算法总结
- C#数据结构与算法揭秘二
- C#冒泡法排序算法实例分析
- 算法练习之从String.indexOf的模拟实现开始
- C#算法之关于大牛生小牛的问题
- C#实现的算24点游戏算法实例分析
- c语言实现的带通配符匹配算法
- 浅析STL中的常用算法
- 算法之排列算法与组合算法详解
- C++实现一维向量旋转算法
- Ruby实现的合并排序算法
- C#折半插入排序算法实现方法
- 基于C++实现的各种内部排序算法汇总
- C++线性时间的排序算法分析
- C++实现汉诺塔算法经典实例
- PHP实现克鲁斯卡尔算法实例解析
- C#获取关键字附近文字算法实例