The main topic: give two m*m map, ask two map of the largest sub-square matrix side length is how big.
Idea: Hash The two matrices first, then enumerate the maximum lengths, from the large to the small enumerations. All cases of the first matrix are inserted into the hash table, and then all cases of the second matrix are queried.
Keep in mind that those arrays in the hash table must be larger.
CODE:
#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #define MAX 60# Define RANGE 100100using namespace Std;const unsigned long long BASE1 = 1333;const unsigned long long BASE2 = 23333;const int MO = 999997; int m;unsigned Long Long hash[max][max],_hash[max][max];unsigned long long pow1[max],pow2[max]; struct hashset{int head[mo],total; int Next[range]; unsigned long long hash[range]; BOOL Check (unsigned long long h) {int x = h% MO; for (int i = head[x]; i; i = Next[i]) if (hash[i] = = h) return true; return false; } void Insert (unsigned long long h) {int x = h% MO; Next[++total] = head[x]; Hash[total] = h; HEAD[X] = total; }}set; int main () {cin >> m; for (int i = 1; I <= m; ++i) for (int j = 1; j <= m; ++j) scanf ("%lld", &hash[i][j]); for (int i = 1, i <= m; ++i) for (int j = 1; j <= m; + +)j) scanf ("%lld", &_hash[i][j]); for (int i = 1, i <= m; ++i) for (int j = 1; j <= m; ++j) {Hash[i][j] + = hash[i-1][j] * BASE1; _HASH[I][J] + = _hash[i-1][j] * BASE1; } for (int i = 1, i <= m; ++i) for (int j = 1; j <= m; ++j) {Hash[i][j] + = hash[i][j-1] * BASE 2; _HASH[I][J] + = _hash[i][j-1] * BASE2; } Pow1[0] = pow2[0] = 1; for (int i = 1; I <= m; ++i) pow1[i] = pow1[i-1] * Base1,pow2[i] = pow2[i-1] * BASE2; int ans; for (ans = m; ans,--ans) {for (int i = ans, i <= m; ++i) for (int j = ans; j <= m; ++j) { unsigned long long h = hash[i][j]; H-= hash[i-ans][j] * Pow1[ans]; H-= Hash[i][j-ans] * Pow2[ans]; H + = Hash[i-ans][j-ans] * Pow1[ans] * Pow2[ans]; Set. Insert (h); } for (int i = ans, i <= m; ++i) for (int j = ans; J <= m; ++J) {unsigned long long h = _hash[i][j]; H-= _hash[i-ans][j] * Pow1[ans]; H-= _hash[i][j-ans] * Pow2[ans]; H + = _hash[i-ans][j-ans] * Pow1[ans] * Pow2[ans]; if (set. Check (h)) {cout << ans << endl; return 0; }}} cout << 0 << Endl; return 0;}
Bzoj 1567 Jsoi Blue Mary's battle map two-dimensional hash