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

检验方法:

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

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

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

  1. #include<cstdio>
  2. #include<algorithm>
  3. #define N 510
  4. typedef unsigned long long ll;
  5. const ll D1=97,D2=131;
  6. 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];
  7. bool check(int x){
  8. for(i=1;i<=n;i++){
  9. for(tmp=0,j=1;j<x;j++)tmp=tmp*D1+a[i][j],h[i][j]=0;
  10. for(j=x;j<=m;j++)h[i][j]=tmp=tmp*D1-pow1[x]*a[i][j-x]+a[i][j];
  11. }
  12. for(t=0,i=x;i<=m;i++){
  13. for(tmp=0,j=1;j<x;j++)tmp=tmp*D2+h[j][i];
  14. for(j=x;j<=n;j++)hash[t++]=tmp=tmp*D2-pow2[x]*h[j-x][i]+h[j][i];
  15. }
  16. for(std::sort(hash,hash+t),i=1;i<t;i++)if(hash[i-1]==hash[i])return 1;
  17. return 0;
  18. }
  19. int main(){
  20. scanf("%d%d",&n,&m);
  21. for(i=1;i<=n;i++)for(scanf("%s",a[i]+1),j=1;j<=m;j++)a[i][j]-='a'-1;
  22. l=1,r=n<m?n:m;
  23. for(pow1[0]=pow2[0]=i=1;i<=r;i++)pow1[i]=pow1[i-1]*D1,pow2[i]=pow2[i-1]*D2;
  24. while(l<=r)if(check(mid=(l+r)>>1))l=(ans=mid)+1;else r=mid-1;
  25. return printf("%d",ans),0;
  26. }

  

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. Linux EOF使用

    # cat << EOF > fileB   用法 例: vi ceshi.sh cat<<eof>file1 aaaa bbbb cccc dddd eof 操作 ...

  2. Trailing Zeros

    Write an algorithm which computes the number of trailing zeros in n factorial. Have you met this que ...

  3. makefile文件编写

    文件转载自:http://www.cppblog.com/lapcca/archive/2010/11/26/134714.html 下面这篇文章讲的很清楚,基本的用法也很简单.   一.Makefi ...

  4. js获取文本框输入的值

    <script type="text/javascript"> function getPosition(obj) { ; if (obj.selectionStart ...

  5. TypeError: document.formname.submit is not a function

    <form name="formname" ...> .... <input name="submit" type="submit& ...

  6. 转载一篇关于ios静态库的文章

    http://blog.csdn.net/zsomsom/article/details/9163635

  7. 转mysql复制主从集群搭建

    最近搭了个主从复制,中间出了点小问题,排查搞定,记录下来 1环境:虚拟机:OS:centos6.5Linux host2 2.6.32-431.el6.x86_64 #1 SMP Fri Nov 22 ...

  8. hdu 1272 小希的迷宫 解题报告

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1272 第二条并查集,和畅通工程的解法类似.判断小希的迷宫不符合条件,即有回路.我的做法是,在合并两个集 ...

  9. HDU 2147 kiki's game(博弈)

    kiki's game Time Limit: 1000MS   Memory Limit: 10000KB   64bit IO Format: %I64d & %I64u Submit S ...

  10. MFC dfs遍历文件

    //如果涉及到大文件的遍历(大于4GB),可以将以下代码_finddata_t换成__finddata64_t,_findfirst换成_findfirst64,_findnext换成_findnex ...