感觉二维哈希的思想跟一维的差不多,比如要匹配的字符串是p *q的大小,先保存好以下标为i,j为一列的末尾元素的hash值,然后一q大小的列 进行 行的拓展,行列的思想都一样,滚动。
//#include<bits/stdc++.h>
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> P;
#define fi first
#define se second
#define INF 0x3f3f3f3f
#define clr(x,y) memset(x,y,sizeof x)
#define PI acos(-1.0)
#define ITER set<int>::iterator
const int Mod = 1e9 + 7;
const int maxn = 1000 + 10;
const int N = 2;
const ull D1 = 97,D2 = 131;
int n,m,T,p,q;
ull pow1[maxn],pow2[maxn];
ull h[maxn][maxn];
char s[maxn][maxn],s1[maxn][maxn];
ull a[maxn * maxn];
int main()
{
int ii = 1;pow1[0] = pow2[0] = 1;for(int i = 1; i < maxn; i ++)pow1[i] = pow1[i - 1] * D1,pow2[i] = pow2[i - 1] * D2;
while( ~ scanf("%d%d%d%d%d",&n,&m,&T,&p,&q))
{
if(!n && !m)break;ull temp = 0;int cnt = 0;
for(int i = 1; i <= n;i ++)scanf("%s",s[i] + 1);
for(int i = 1; i <= n; i ++)
{
temp = 0;s[i][0] = 0;
for(int j = 1; j < q;j ++)temp = temp * D2 + s[i][j];
for(int j = q; j <= m; j ++)h[i][j] = temp = temp * D2 - s[i][j - q] * pow2[q] + s[i][j];
}
for(int j = q; j <= m; j ++)
{
temp = 0;s[0][j] = 0;
for(int i = 1; i < p; i ++)temp = temp * D1 + h[i][j];
for(int i = p; i <= n;i ++)a[cnt ++] = temp = temp * D1 - h[i - p][j] * pow1[p] + h[i][j];
}
sort(a,a + cnt);int ans = 0;
while(T --)
{
for(int i = 1; i <= p; i ++)scanf("%s",s1[i] + 1);
temp = 0;
for(int i = 1; i <= p; i ++)
{
ull t = 0;
for(int j = 1; j <= q; j ++)t = t * D2 + s1[i][j];
temp = temp * D1 + t;
}
int pos = lower_bound(a,a + cnt,temp) - a;
if( pos < cnt && a[pos] == temp)ans ++;
}
if(n < p || m < q)ans = 0;
printf("Case %d: %d\n",ii ++,ans);
}
return 0;
}
A 矩阵(https://www.nowcoder.com/acm/contest/submit/f8363c912a4c48a28b80f47e7102b6b8?ACMContestId=2)
题意:给出一个n * m矩阵,然后求出T个p * q的矩阵。求出能匹配的矩阵的个数。
思路:先根据模式串对p*q大小的子矩阵进行打表,然后算出给出的矩阵的hash值,然后二分查找会不会出现。
//#include<bits/stdc++.h>
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> P;
#define fi first
#define se second
#define INF 0x3f3f3f3f
#define clr(x,y) memset(x,y,sizeof x)
#define PI acos(-1.0)
#define ITER set<int>::iterator
const int Mod = 1e9 + 7;
const int maxn = 500 + 10;
const int N = 2;
const ull D1 = 97,D2 = 131;
int n,m;
ull pow1[maxn],pow2[maxn];
char s[maxn][maxn];
ull h[maxn][maxn];
ull a[maxn * maxn];
bool check(int k)
{
ll temp;
for(int i = 1; i <= n; i ++)
{
temp = 0;
for(int j = 1;j < k; j ++)temp = temp * D2 + s[i][j];s[i][0] = 0;
for(int j = k; j <= m; j ++)h[i][j] = temp = temp * D2 - s[i][j - k] * pow2[k] + s[i][j];
}
int cnt = 0;
for(int j = k; j <= m; j ++)
{
temp = 0;h[0][j] = 0;
for(int i = 1; i < k; i ++)temp = temp * D1 + h[i][j];
for(int i = k; i <= n; i ++)a[cnt ++] = temp = temp * D1 - h[i - k][j] * pow1[k] + h[i][j];
}
sort(a,a + cnt);for(int i = 1; i < cnt; i ++)if(a[i] == a[i - 1])return true;return false;
}
int main()
{
pow1[0] = pow2[0] = 1;
for(int i = 1; i < maxn; i ++)pow1[i] = pow1[i - 1] * D1,pow2[i] = pow2[i - 1] * D2;
while( ~ scanf("%d%d",&n,&m))
{
for(int i = 1; i <= n; i ++)scanf("%s",s[i] + 1);
int l = 0,r = min(n,m),ans = 0;
while(l <= r)
{
int mid = (l + r) >> 1;if(check(mid))ans = mid,l = mid + 1;else r = mid - 1;
}
printf("%d\n",ans);
}
return 0;
}