【URAL 1486】Equal Squares(二维哈希+二分)
Description
During a discussion of problems at the Petrozavodsk Training Camp, Vova and Sasha argued about who of them could in 300 minutes find a pair of equal squares of the maximal size in a matrix of size N × M containing lowercase English letters. Squares could overlap each other but could not coincide. He who had found a pair of greater size won. Petr walked by, looked at the matrix, said that the optimal pair of squares had sides K, and walked on. Vova and Sasha still cannot find this pair. Can you help them?
Input
The first line contains integers N and M separated with a space. 1 ≤ N, M ≤ 500. In the next N lines there is a matrix consisting of lowercase English letters, M symbols per line.
Output
In the first line, output the integer K which Petr said. In the next two lines, give coordinates of upper left corners of maximal equal squares. If there exist more than one pair of equal squares of size K, than you may output any of them. The upper left cell of the matrix has coordinates (1, 1), and the lower right cell has coordinates (N, M). If there are no equal squares in the matrix, then output 0.
Sample input
5 10
ljkfghdfas
isdfjksiye
pgljkijlgp
eyisdafdsi
lnpglkfkjl
Sample output
3
1 1
3 3
题解
二分答案,然后利用二维哈希\(O(n^2)\)check,总的时间为\(O(n^2 logn)\)
参考代码
#include <map>
#include <queue>
#include <cstdio>
#include <complex>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#define ll unsigned long long
#define inf 1000000000
#define PI acos(-1)
#define bug puts("here")
#define REP(i,x,n) for(int i=x;i<=n;i++)
#define DEP(i,n,x) for(int i=n;i>=x;i--)
#define mem(a,x) memset(a,x,sizeof(a))
using namespace std;
inline int read(){
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-') f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
inline void Out(int a){
if(a<0) putchar('-'),a=-a;
if(a>=10) Out(a/10);
putchar(a%10+'0');
}
const int N=505;
char a[N][N];
ll ha[N][N];
ll p1[N],p2[N];
map<ll,int>vis;
int ansx1,ansy1,ansx2,ansy2;
int n,m;
struct node{
int x,y;
}book[N*N];
bool check(int s){
vis.clear();
ll tmp;int tot=0;
REP(i,s,n) REP(j,s,m){
tmp=ha[i][j]-ha[i-s][j]*p2[s]-ha[i][j-s]*p1[s]+ha[i-s][j-s]
*p1[s]*p2[s];
if(vis[tmp]){
int id=vis[tmp];
ansx1=book[id].x;ansy1=book[id].y;
ansx2=i-s+1;ansy2=j-s+1;
return true;
}
++tot;
book[tot].x=i-s+1;book[tot].y=j-s+1;
vis[tmp]=tot;
}
return false;
}
int main(){
cin>>n>>m;
REP(i,1,n) cin>>(a[i]+1);
int seed1=123,seed2=1789;
REP(i,1,n) REP(j,1,m) ha[i][j]=ha[i][j-1]*seed1+a[i][j];
REP(i,1,n) REP(j,1,m) ha[i][j]=ha[i-1][j]*seed2+ha[i][j];
p1[0]=p2[0]=1;
REP(i,1,m) p1[i]=p1[i-1]*seed1;
REP(i,1,n) p2[i]=p2[i-1]*seed2;
int l=1,r=min(n,m),ans=-1;
while(l<=r){
int mid=(l+r)>>1;
if(check(mid)){
l=mid+1;
ans=mid;
}else r=mid-1;
}
if(ans!=-1){
printf("%d\n",ans);
printf("%d %d\n%d %d\n",ansx1,ansy1,ansx2,ansy2);
}else puts("0");
return 0;
}
【URAL 1486】Equal Squares(二维哈希+二分)的更多相关文章
- URAL - 1486 Equal Squares 二维哈希+二分
During a discussion of problems at the Petrozavodsk Training Camp, Vova and Sasha argued about who o ...
- BZOJ1397 : Ural 1486 Equal squares
二分答案mid,然后检验是否存在两个相同的mid*mid的正方形 检验方法: 首先对于每个位置,求出它开始长度为mid的横行的hash值 然后对于hash值再求一次竖列的hash值 将第二次求出的ha ...
- 【BZOJ 2462】矩阵模板 (二维哈希)
题目 给定一个M行N列的01矩阵,以及Q个A行B列的01矩阵,你需要求出这Q个矩阵哪些在 原矩阵中出现过. 所谓01矩阵,就是矩阵中所有元素不是0就是1. 输入 输入文件的第一行为M.N.A.B,参见 ...
- AcWing - 156 矩阵(二维哈希)
题目链接:矩阵 题意:给定一个$m$行$n$列的$01$矩阵$($只包含数字$0$或$1$的矩阵$)$,再执行$q$次询问,每次询问给出一个$a$行$b$列的$01$矩阵,求该矩阵是否在原矩阵中出现过 ...
- poj-3739. Special Squares(二维前缀和)
题目链接: I. Special Squares There are some points and lines parellel to x-axis or y-axis on the plane. ...
- POJ3690:Constellations(二维哈希)
Constellations Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 6822 Accepted: 1382 题目 ...
- UVA-11019 二维哈希算法
UVA-11019 题意: 就是给你AB两个字符矩阵,问你B矩阵在A矩阵中的出现次数. 题解: 参考链接:https://blog.csdn.net/qq_38891827/java/article ...
- [算法][LeetCode]Search a 2D Matrix——二维数组的二分查找
题目要求 Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the ...
- loj #535. 「LibreOJ Round #6」花火 树状数组求逆序对+主席树二维数点+整体二分
$ \color{#0066ff}{ 题目描述 }$ 「Hanabi, hanabi--」 一听说祭典上没有烟火,Karen 一脸沮丧. 「有的哦-- 虽然比不上大型烟花就是了.」 还好 Shinob ...
随机推荐
- Zeppelin的入门使用系列之创建新的Notebook
前期博客 hadoop-2.6.0.tar.gz + spark-1.6.1-bin-hadoop2.6.tgz + zeppelin-0.5.6-incubating-bin-all.tgz(mas ...
- Cloudera Manager是啥?主要是干啥的?
简单来说,Cloudera Manager是一个拥有集群自动化安装.中心化管理.集群监控.报警功能的一个工具(软件),使得安装集群从几天的时间缩短在几个小时内,运维人员从数十人降低到几人以内,极大的提 ...
- JS判断字符串长度(英文占1个字符,中文汉字占2个字符)
//计算字符串长度(英文占1个字符,中文汉字占2个字符) 方法一: String.prototype.gblen = function() { var len = 0; for (var i=0; i ...
- HDU - 6066 RXD's date
Bryce1010模板 http://acm.hdu.edu.cn/showproblem.php?pid=6066 #include <iostream> using namespace ...
- 1051 - Good or Bad DFS 记忆化搜索
http://lightoj.com/volume_showproblem.php?problem=1051 对于每个位置,设dfs(cur, one, two)表示前i个字母,拥有辅音字母one个, ...
- RHEL6.5----LVS(NAT)
主机名 IP 所需软件 master 192.168.30.130(Nat) 192.168.17.130(VMnet4) ipvsadm node-1 192.168.17.131 http ...
- P1554 梦中的统计
题目背景 Bessie 处于半梦半醒的状态.过了一会儿,她意识到她在数数,不能入睡. 题目描述 Bessie的大脑反应灵敏,仿佛真实地看到了她数过的一个又一个数.她开始注意每一个数码(0..9):每一 ...
- python工具之日志记录
''' 写日志类 日志存放目录为当前应用程序的目录下的log目录中 日志产生规则:每小时产生一个文件 write by :wujf 2017-02-24 ''' import sys import o ...
- AJPFX浅谈Java性能优化之finalize 函数
★finalize 函数的调用机制 俺经常啰嗦:“了解本质机制的重要性”.所以今天也得先谈谈 finalize 函数的调用机制.在聊之前,先声明一下:Java虚拟机规范,并没有硬性规定垃圾回收该不该搞 ...
- 【学习笔记】深入理解js原型和闭包系列学习笔记——精华
深入理解js原型和闭包笔记: 1.“一切皆是对象”,对象是属性的集合. 丨 函数也是对象,但是使用typeof时为什么函数返回function而 丨 不是object呢,js为何要对函数做这样的区分 ...