二分答案mid,然后检验是否存在两个相同的mid*mid的正方形

检验方法:

首先对于每个位置,求出它开始长度为mid的横行的hash值

然后对于hash值再求一次竖列的hash值

将第二次求出的hash值排序,如果存在两个相同的hash值则可行

#include<cstdio>
#include<algorithm>
#define N 510
typedef unsigned long long ll;
const ll D1=97,D2=131;
int n,m,i,j,l,r,mid,ans,t;char a[N][N];ll pow1[N],pow2[N],h[N][N],tmp,tmp2,hash[N*N];
bool check(int x){
for(i=1;i<=n;i++){
for(tmp=0,j=1;j<x;j++)tmp=tmp*D1+a[i][j],h[i][j]=0;
for(j=x;j<=m;j++)h[i][j]=tmp=tmp*D1-pow1[x]*a[i][j-x]+a[i][j];
}
for(t=0,i=x;i<=m;i++){
for(tmp=0,j=1;j<x;j++)tmp=tmp*D2+h[j][i];
for(j=x;j<=n;j++)hash[t++]=tmp=tmp*D2-pow2[x]*h[j-x][i]+h[j][i];
}
for(std::sort(hash,hash+t),i=1;i<t;i++)if(hash[i-1]==hash[i])return 1;
return 0;
}
int main(){
scanf("%d%d",&n,&m);
for(i=1;i<=n;i++)for(scanf("%s",a[i]+1),j=1;j<=m;j++)a[i][j]-='a'-1;
l=1,r=n<m?n:m;
for(pow1[0]=pow2[0]=i=1;i<=r;i++)pow1[i]=pow1[i-1]*D1,pow2[i]=pow2[i-1]*D2;
while(l<=r)if(check(mid=(l+r)>>1))l=(ans=mid)+1;else r=mid-1;
return printf("%d",ans),0;
}

  

BZOJ1397 : Ural 1486 Equal squares的更多相关文章

  1. URAL - 1486 Equal Squares 二维哈希+二分

    During a discussion of problems at the Petrozavodsk Training Camp, Vova and Sasha argued about who o ...

  2. 【URAL 1486】Equal Squares(二维哈希+二分)

    Description During a discussion of problems at the Petrozavodsk Training Camp, Vova and Sasha argued ...

  3. 【URAL 1486】Equal Squares

    题意:求给定字符矩阵中相同正方形矩阵的最大边长和这两个相同正方形的位置 第一次写字符串哈希,选两个不同的模数进行二维字符串哈希. 本来应该取模判断相等后再暴力扫矩阵来判断,但是我看到<Hash在 ...

  4. URAL - 1486 二维字符串HASH

    题目链接:http://acm.timus.ru/problem.aspx?space=1&num=1486 题意:给定一个n*m的字符矩阵,问你是否存在两个不重合(可以有交集)的正方形矩阵完 ...

  5. bzoj AC倒序

    Search GO 说明:输入题号直接进入相应题目,如需搜索含数字的题目,请在关键词前加单引号 Problem ID Title Source AC Submit Y 1000 A+B Problem ...

  6. codeforces 629A Far Relative’s Birthday Cake

    A. Far Relative’s Birthday Cake time limit per test 1 second memory limit per test 256 megabytes inp ...

  7. Coder-Strike 2014 - Round 1(A~E)

    题目链接 A. Poster time limit per test:1 secondmemory limit per test:256 megabytesinput:standard inputou ...

  8. CF 316C2(Tidying Up-二分图最大边权)

    C2. Tidying Up time limit per test 4 seconds memory limit per test 256 megabytes input standard inpu ...

  9. Codeforces Round #343 (Div. 2)-629A. Far Relative’s Birthday Cake 629B. Far Relative’s Problem

    A. Far Relative's Birthday Cake time limit per test 1 second memory limit per test 256 megabytes inp ...

随机推荐

  1. 变色龙安装程序 Chameleon Install 2.2 svn 2281发布

    变色龙安装程序 Chameleon Install 2.2 svn 2281发布 1.更好的支持10.9 Mavericks2.更新ATi.nVidia显卡支持列表3.添加新的 CPU Model I ...

  2. 多层神经网络与C++实现

    BP理论部分参考:http://blog.csdn.net/itplus/article/details/11022243 参考http://www.cnblogs.com/ronny/p/ann_0 ...

  3. First Missing Positive

    不好想,用桶排序解决. int findMissingPostive(int A[], int n) { bucket_sort(A, n); ; i < n; i++) ) ; ; } voi ...

  4. FlashDevelop快捷键

    将鼠标点到变量上面后,同时按ctrl+shift+1(左键盘),可以自动添加变量或者函数.复制一行代码.CTRL+D:ctrl+shift+k 颜色代码拾取器 ctrl+shift+b 注释年选代码段 ...

  5. 【云计算】开源装机自动化系统 CloudBoot OSInstall 介绍

    "CloudBoot"(OSinstall) 发布了. 产品更新及特点如下: 新增虚拟化操作系统适配:支持主流操作系统:RedHat.CentOS.SUSE.Ubuntu.Wind ...

  6. Unique Binary Search Trees I & II

    Given n, how many structurally unique BSTs (binary search trees) that store values 1...n? Example Gi ...

  7. 【leetcode】Combination Sum

    Combination Sum Given a set of candidate numbers (C) and a target number (T), find all unique combin ...

  8. 对 Linux 新手非常有用的 20 个命令

    参考:http://www.oschina.net/translate/useful-linux-commands-for-newbies 英文原文:http://www.tecmint.com/us ...

  9. MFC 文件按行读写 CStdioFile

    //写文件 CStdioFile file; file.Open("test.txt",CFile::modeCreate|CFile::modeReadWrite); file. ...

  10. Java内部类的访问规则

    1.内部类可以直接访问外部类中的成员,包括私有      原因:因为在内部类中持有一个外部类的应用,格式:外部类.this class Outer {     private int x = 1; c ...