【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 ...
随机推荐
- C++ 的输出格式
0 在C语言中很简单对输出的要求,然而在C++中有一丝的麻烦. 在下面的代码中所需要的是 #include<iostream> 基本输入/输出库 #include<iomanip&g ...
- JQUERY 获取 DIV 宽度与高度(width,padding,margin,border)
一般讲的宽度指的是内容宽度,但一个 div 的实际宽度不仅只于内容宽度,尤其在做 CSS 排版时更不能搞错,必须同时考虑 Padding.Border 与 Margin 的宽度,四个加起来才是 di ...
- MySQL、SqlServer、Oracle三大主流数据库分页查询 (MySQL分页不能用top,因为不支持)
一. MySQL 数据库 分页查询MySQL数据库实现分页比较简单,提供了 LIMIT函数.一般只需要直接写到sql语句后面就行了.LIMIT子 句可以用来限制由SELECT语句返回过来的数据数量,它 ...
- Backbone.js入门教程第二版笔记(3)
视图渲染 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <titl ...
- 搭建一个高可用的redis环境
一.环境准备 我的环境: Fedora 25 server 64位版 6台: 192.168.10.204 192.168.10.205 192.168.10.206 192.168.10.203 ...
- javascript 找出数字数组中最大的数
找出数字数组中最大的数 var Match = (function(){ var arr = null; var len = 0; return { max:function(arr,len){ ar ...
- JAVA 运用流编程实现简单的"记事本"功能
一.概要 1.功能介绍 2.实现的思路及步骤代码 3.完整代码 二.功能 运用IO流和Swing实现简单的记事本功能(打开.保存.退出) 三.思路及实现步骤 1.在构造函数中画出操作界面 //创建jt ...
- Elasticsearch (1) - 索引库 文档 分词
创建索引库 ES的索引库是一个逻辑概念,它包括了分词列表及文档列表,同一个索引库中存储了相同类型的文档.它就相当于MySQL中的表,或相当于Mongodb中的集合. 关于索引这个语: 索引(名词):E ...
- Android小玩意儿-- 从头开发一个正经的MusicPlayer(三)
MusicService已经能够接收广播,通过广播接收的内容来做出相应的MediaPlayer对象的处理,包括播放,暂停,停止等,并当MediaPlayer对象的生命周期发生变化的时候,同样通过发送广 ...
- ubuntu 下service php5-fpm restart 报错 stop: Unknown instance: 解决
问题描述: 在安装完扩展后,重启php-fpm,发现一直停止报错 stop: Unknown instance: 通过查看进程,也查询不到该主进程 解决办法: 干掉现在正在执行的进程 pkill ph ...