BZOJ 2462 矩阵模板(二维hash)
题意:给出一个n*m的01矩阵,以及k个a*b的01矩阵,问每个是否能匹配原来的01矩阵。
由于k个矩阵的长和宽都是一样的,所以把原矩阵的所有a*b的子矩阵给hash出来。然后依次查找是否存在即可。
map被卡,用lower_bound即可。
# include <cstdio> # include <cstring> # include <cstdlib> # include <iostream> # include <vector> # include <queue> # include <stack> # include <map> # include <bitset> # include <set> # include <cmath> # include <algorithm> using namespace std; # define lowbit(x) ((x)&(-x)) # define pi acos(-1.0) # define eps 1e-8 # define MOD 100000000 # define INF 1000000000 # define mem(a,b) memset(a,b,sizeof(a)) # define FOR(i,a,n) for(int i=a; i<=n; ++i) # define FO(i,a,n) for(int i=a; i<n; ++i) # define bug puts("H"); # define lch p<<1,l,mid # define rch p<<1|1,mid+1,r # define mp make_pair # define pb push_back typedef pair<int,int> PII; typedef vector<int> VI; # pragma comment(linker, "/STACK:1024000000,1024000000") typedef long long LL; int Scan() { int x=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return x*f; } const int N=1005; //Code begin... char s[N][N], str[N][N]; int hah[N][N], has[N][N], M1[N], M2[N], key1=1789, key2=131; VI v; int main () { int n, m, a, b, K, x, y; M1[0]=M2[0]=1; FO(i,1,N) M1[i]=M1[i-1]*key1, M2[i]=M2[i-1]*key2; scanf("%d%d%d%d",&n,&m,&a,&b); FOR(i,1,n) scanf("%s",s[i]+1); FOR(i,1,n) FOR(j,1,m) hah[i][j]=hah[i][j-1]*key1+s[i][j]; FOR(i,1,n) FOR(j,1,m) hah[i][j]+=hah[i-1][j]*key2; FOR(i,a,n) FOR(j,b,m) { x=hah[i][j]-hah[i-a][j]*M2[a]-hah[i][j-b]*M1[b]+hah[i-a][j-b]*M2[a]*M1[b]; v.pb(x); } sort(v.begin(),v.end()); scanf("%d",&K); while (K--) { FOR(i,1,a) scanf("%s",str[i]+1); FOR(i,1,a) FOR(j,1,b) has[i][j]=has[i][j-1]*key1+str[i][j]; FOR(i,1,a) FOR(j,1,b) has[i][j]+=has[i-1][j]*key2; y=lower_bound(v.begin(),v.end(),has[a][b])-v.begin(); puts(y<v.size()&&v[y]==has[a][b]?"1":"0"); } return 0; }