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(二维哈希+二分)的更多相关文章

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

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

  2. BZOJ1397 : Ural 1486 Equal squares

    二分答案mid,然后检验是否存在两个相同的mid*mid的正方形 检验方法: 首先对于每个位置,求出它开始长度为mid的横行的hash值 然后对于hash值再求一次竖列的hash值 将第二次求出的ha ...

  3. 【BZOJ 2462】矩阵模板 (二维哈希)

    题目 给定一个M行N列的01矩阵,以及Q个A行B列的01矩阵,你需要求出这Q个矩阵哪些在 原矩阵中出现过. 所谓01矩阵,就是矩阵中所有元素不是0就是1. 输入 输入文件的第一行为M.N.A.B,参见 ...

  4. AcWing - 156 矩阵(二维哈希)

    题目链接:矩阵 题意:给定一个$m$行$n$列的$01$矩阵$($只包含数字$0$或$1$的矩阵$)$,再执行$q$次询问,每次询问给出一个$a$行$b$列的$01$矩阵,求该矩阵是否在原矩阵中出现过 ...

  5. poj-3739. Special Squares(二维前缀和)

    题目链接: I. Special Squares There are some points and lines parellel to x-axis or y-axis on the plane. ...

  6. POJ3690:Constellations(二维哈希)

    Constellations Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 6822   Accepted: 1382 题目 ...

  7. UVA-11019 二维哈希算法

    UVA-11019 题意: 就是给你AB两个字符矩阵,问你B矩阵在A矩阵中的出现次数. 题解:  参考链接:https://blog.csdn.net/qq_38891827/java/article ...

  8. [算法][LeetCode]Search a 2D Matrix——二维数组的二分查找

    题目要求 Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the ...

  9. loj #535. 「LibreOJ Round #6」花火 树状数组求逆序对+主席树二维数点+整体二分

    $ \color{#0066ff}{ 题目描述 }$ 「Hanabi, hanabi--」 一听说祭典上没有烟火,Karen 一脸沮丧. 「有的哦-- 虽然比不上大型烟花就是了.」 还好 Shinob ...

随机推荐

  1. Centos 配置 Mysql 主从双向同步

    配置之前,请先阅读mysql主从复制: Mysql-主从复制 原:  主从环境: 主服务器:192.168.153.130 从服务器:192.168.153.131 1.从数据库创建同步用户,将主数据 ...

  2. BFS Codeforces Round #297 (Div. 2) D. Arthur and Walls

    题目传送门 /* 题意:问最少替换'*'为'.',使得'.'连通的都是矩形 BFS:搜索想法很奇妙,先把'.'的入队,然后对于每个'.'八个方向寻找 在2*2的方格里,若只有一个是'*',那么它一定要 ...

  3. pwa-serviceWorker与页面通信postMessage

    https://ppt.geekbang.org/list/gmtc2018?from=groupmessage&amp%3Bisappinstalled=0 http://www.sohu. ...

  4. Appium + Python自动化 - 元素定位uiautomatorviewer

    元素定位主要介绍如何使用uiautiomatorviewer,通过定位到页面上的元素,然后进行相应的点击等操作.uiautiomatorviewer是android-sdk自带的一个元素定位工具,非常 ...

  5. LVS实现负载均衡

    三台主机模拟 sishen_63(分发器): eth0(Bridge):192.168.1.63 eth1(vmnet4):192.168.2.63 sishen_64(RealServer1): e ...

  6. Spring------自动化装配Bean(一)

    一.创建 CompactDisc接口和SgetPeppers实现类 CompactDisc接口方法为播放.SgtPeppers实现CompactDisc接口. package soundsystem; ...

  7. C#基础学习4

    流程控制!

  8. net MVC 四种基本 Filter

    四种基本 Filter 概述 MVC框架支持的Filter可以归为四类,每一类都可以对处理请求的不同时间点引入额外的逻辑处理.这四类Filter如下表:   使用内置的Authorization Fi ...

  9. iOS中使用 Reachability 检测网络区分手机网络类型 WiFi 和2 3 4 G

    如果你想在iOS程序中提供一仅在wifi网络下使用(Reeder),或者在没有网络状态下提供离线模式(Evernote).那么你会使用到Reachability来实现网络检测. 写本文的目的 了解Re ...

  10. Java编译时根据调用该方法的类或对象所属的类决定

    class Base{     int x = 1;     static int y = 2; } class Subclass extends Base{     int x = 4;     i ...