UVA11019–Matrix Matcher
题意
从n×m的字符矩阵中找到有多少个x×y的字符矩阵。
分析
运用二维哈希,采用两个base——base1、base2
先对模式矩阵串进行gethash操作,得到H[x][y]的值
然后对原矩阵串进行gethash操作
遍历H,通过get操作寻找结果等于H[x][y]的即为解
我的代码
#include<iostream>
#include<string>
#include<algorithm>
#include<string.h>
#define ull unsigned long long
#define N 1005
#define base1 19760817
#define base2 1610612741
using namespace std;
string s1[N],s2[N];
ull H[N][N],p1[N],p2[N];
void init(){
p1[0]=p2[0]=1;
for(int i=1;i<N;i++){
p1[i]=p1[i-1]*base1;
p2[i]=p2[i-1]*base2;
}
}
void gethash(int a,int b,string s[]){
for(int i=1;i<=a;i++){
for(int j=1;j<=b;j++){
H[i][j]=H[i-1][j]*base1+H[i][j-1]*base2-H[i-1][j-1]*base1*base2+s[i-1][j-1]-'a'+1;
}
}
}
ull get(int lx,int rx,int ly,int ry){