题意

问边长为n的两个正方形中最大的相等子正方形。(n<=50)

题解

用到了二维hash,感觉和一维的不太一样。

对于列行有两个不同的进制数然后也是通过类似前缀和的方法差分出一个矩形的hash值

这样可以0(1)的算出一个正方形的hash值。

然后我们二分长度x,每一次遍历整个长度为x的子正方形n2判断即可。

 #include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<map>
using namespace std;
const int mod1=;
const int mod2=;
map<unsigned,int> ma;
unsigned long long pow1[],pow2[],hash1[][],hash2[][];
int n,ans,t;
bool judge(int x){
++t;
for(int i=x;i<=n;i++)
for(int j=x;j<=n;j++){
ma[hash1[i][j]-hash1[i-x][j]*pow2[x]-hash1[i][j-x]*pow1[x]+hash1[i-x][j-x]*pow1[x]*pow2[x]]=t;
}
for(int i=x;i<=n;i++)
for(int j=x;j<=n;j++){
if(ma[hash2[i][j]-hash2[i-x][j]*pow2[x]-hash2[i][j-x]*pow1[x]+hash2[i-x][j-x]*pow1[x]*pow2[x]]==t)return true;
}
return false;
}
int main(){
scanf("%d",&n);
pow1[]=pow2[]=;
for(int i=;i<=n;i++){
pow1[i]=pow1[i-]*mod1;
pow2[i]=pow2[i-]*mod2;
}
for(int i=;i<=n;i++)
for(int j=;j<=n;j++){
int a;
scanf("%d",&a);
hash1[i][j]=hash1[i][j-]*mod1+a;
}
for(int i=;i<=n;i++)
for(int j=;j<=n;j++){
int a;
scanf("%d",&a);
hash2[i][j]=hash2[i][j-]*mod1+a;
}
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
hash1[i][j]=hash1[i-][j]*mod2+hash1[i][j];
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
hash2[i][j]=hash2[i-][j]*mod2+hash2[i][j];
int l=;int r=n;
while(l<=r){
int mid=(l+r)>>;
if(judge(mid)){
l=mid+;
ans=mid;
}
else r=mid-;
}
printf("%d",ans);
return ;
}

BZOJ1567 [JSOI2008]Blue Mary的战役地图(二分+二维hash)的更多相关文章

  1. BZOJ 1567: [JSOI2008]Blue Mary的战役地图 矩阵二维hash

    1567: [JSOI2008]Blue Mary的战役地图 Description Blue Mary最近迷上了玩Starcraft(星际争霸) 的RPG游戏.她正在设法寻找更多的战役地图以进一步提 ...

  2. BZOJ1567 [JSOI2008]Blue Mary的战役地图 二分答案 哈希

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ1567 题意概括 给出两个n*n的数字矩阵,问最大公共正方形边长. 题解 先二分答案一个m,对于每一 ...

  3. BZOJ 1567: [JSOI2008]Blue Mary的战役地图( 二分答案 + hash )

    二分答案, 然后用哈希去判断... ------------------------------------------------------------------------- #include ...

  4. bzoj1567: [JSOI2008]Blue Mary的战役地图

    将矩阵hash.s[0]忘了弄成0,输出中间过程发现了. hash.sort.判重.大概这样子的步骤吧. #include<cstdio> #include<cstring> ...

  5. B1567 [JSOI2008]Blue Mary的战役地图 二分答案+hash

    一开始以为是dp,后来看了一下标签...二分答案?之前也想过,但是没往下想,然后之后的算法就顺理成章,先求出第一个地图的所有子矩阵的hash值,然后求第二个,在上一个地图例二分查找,然后就没了. 算法 ...

  6. 【矩阵哈希】【二分答案】【哈希表】bzoj1567 [JSOI2008]Blue Mary的战役地图

    引用题解:http://hzwer.com/5153.html 当然,二分可以换成哈希表. #include<cstdio> #include<iostream> #inclu ...

  7. BZOJ 1567: [JSOI2008]Blue Mary的战役地图

    1567: [JSOI2008]Blue Mary的战役地图 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 1011  Solved: 578[Sub ...

  8. [JSOI2008]Blue Mary的战役地图(二分+哈希)

    Blue Mary最近迷上了玩Starcraft(星际争霸) 的RPG游戏.她正在设法寻找更多的战役地图以进一步提高自己的水平. 由于Blue Mary的技术已经达到了一定的高度,因此,对于用同一种打 ...

  9. bzoj 1567: [JSOI2008]Blue Mary的战役地图【二分+hash】

    二维哈希+二分 说是二维,其实就是先把列hash了,然后再用列的hash值hash行,这样可以O(n)的计算一个正方形的hash值,然后二分边长,枚举左上角点的坐标然后hash判断即可 只要base选 ...

随机推荐

  1. 【AnjularJS系列3 】 — 数据的双向绑定

    第三篇,双向的数据绑定 数据绑定是AnguarJS的特性之一,避免书写大量的初始代码从而节约开发时间 数据绑定指令提供了你的Model投射到view的方法.这些投射可以无缝的,毫不影响的应用到web应 ...

  2. Springboot统一异常处理(@ControllerAdvice)

    import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind ...

  3. MySQL Reading table information for completion of table and column names

    打开数据库是发现提示: mysql> show databases; +--------------------+ | Database | +--------------------+ | b ...

  4. 模板 FFT 快速傅里叶变换

    FFT模板,原理不难,优质讲解很多,但证明很难看太不懂 这模板题在bzoj竟然是土豪题,服了 #include <cmath> #include <cstdio> #inclu ...

  5. freeswitch 中文语音

    1.下载中文语音包 链接:https://pan.baidu.com/s/1UODvqj8NAQw7_CRatfl0kg 提取码:qwdn 创建目录 /usr/local/freeswitch/sou ...

  6. ANY和ALL

    8.在WHERE中使用ANY和ALL条件   字段 >ANY(值1,值2,值3...):字段值大于集合任何一个       值就算满足条件.     字段 >ALL(值1,值2,值3... ...

  7. [ReactVR] Render Custom 3D Objects Using the Model Component in React VR

    React VR isn't limited to simple 3D primitives. By using the <Model/> Component we can place a ...

  8. HDU - 4758 Walk Through Squares (AC自己主动机+DP)

    Description   On the beaming day of 60th anniversary of NJUST, as a military college which was Secon ...

  9. LintCode-交叉字符串

    给出三个字符串:s1.s2.s3,推断s3是否由s1和s2交叉构成. 您在真实的面试中是否遇到过这个题? Yes 例子 比方 s1 = "aabcc" s2 = "dbb ...

  10. 实现 jstl标签foreach 功能

    jsp 页面 <%@ page language="java" contentType="text/html; charset=UTF-8" pageEn ...