Bzoj 1567 JSOI2008 Blue Mary's battle map hash+ two points

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.