Topic: Given two matrices, find the maximum common sub-square edge length
First the two-point answer and then check the "a" matrix of all sides of the X-length of the child square exists in the hash table and then enumerate each sub-square of the B matrix to find
Note that the two-dimensional hash of the two base can not be the same, or when two matrices about diagonal symmetry will be judged equal
My hash table is actually slower than map ... Not alive.
#include <map> #include <cstdio> #include <cstring> #include <iostream> #include <algorithm > #define M 60#define SIZE 1001001#define BASE1 999911657#define BASE2 999911659using namespace std;typedef unsigned int U;int n,t;u a[m][m],b[m][m],power1[m],power2[m];map<u,int>table;bool Judge (int x) {int i,j;++t;for (i=x;i<=n ; i++) for (j=x;j<=n;j++) {u hash=a[i][j]-a[i-x][j]*power1[x]-a[i][j-x]*power2[x]+a[i-x][j-x]*power1[x]*power2[x ];table[hash]=t;} for (i=x;i<=n;i++) for (j=x;j<=n;j++) {u hash=b[i][j]-b[i-x][j]*power1[x]-b[i][j-x]*power2[x]+b[i-x][j-x]* Power1[x]*power2[x];if (table[hash]==t) return true;} return false;} int bisection () {int L=0,r=n;while (l+1<r) {int mid=l+r>>1;if (Judge (mid)) L=mid;elser=mid;} if (Judge (r)) return R;return l;} int main () {int i,j;cin>>n;for (i=1;i<=n;i++) for (j=1;j<=n;j++) scanf ("%u", &a[i][j]); for (i=1;i<=n ; i++) for (j=1;j<=n;j++) a[i][j]+=a[i-1][j]*base1;for (i=1;i<=n;i++) for (j=1;j<=n;j++) a[i][j]+=a[i][j-1]*base2;for (i=1;i<=n;i++) for (j=1;j<=n;j++) scanf ("%u", &b[i][j]); for (i=1;i<=n;i++) for (j=1;j<=n;j + +) B[i][j]+=b[i-1][j]*base1;for (i=1;i<=n;i++) for (j=1;j<=n;j++) b[i][j]+=b[i][j-1]*base2;power1[0]=power2[ 0]=1;for (i=1;i<=n;i++) power1[i]=power1[i-1]*base1,power2[i]=power2[i-1]*base2;cout<<bisection () < <endl;}
Bzoj 1567 JSOI2008 Blue Mary's battle map hash+ two points