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 sizeK, 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.

Example

input output
5 10
ljkfghdfas
isdfjksiye
pgljkijlgp
eyisdafdsi
lnpglkfkjl
3
1 1
3 3

先要对每一个字符串进行哈希预处理,哈希处理后,把哈希的值在根据行数哈希一遍

没存过的就存一遍, 找到有没有相同的,

这个二分还是比较好写的

这个二维哈希就有点懵逼

 #include <iostream>
#include <cstdio>
#include <string>
#include <map>
#include <cstring>
#include <algorithm>
using namespace std; typedef long long LL;
typedef unsigned long long ull;
typedef pair<int, int> ii;
const int maxn = ;
const ull p = 1e12 + , pt = ; struct h {
ull ha[maxn][maxn], xp[maxn], sz;
void init1(int n) {
sz = n;
xp[] = ;
for (int i = ; i <= sz ; i++)
xp[i] = xp[i - ] * p;
}
void init2(int id, const string &str) {
ha[id][sz] = ;
for (int i = sz - ; i >= ; i--)
ha[id][i] = ha[id][i + ] * p + (str[i] - 'a' + );
}
ull gethash(int id, int st, int len) {
return ha[id][st] - ha[id][st + len] * xp[len];
}
} Hash;
string s[maxn];
map< ull, ii>mp;
int n, m, ans;
ii ans1, ans2;
ull hat[maxn], xpt[maxn];
void initxpt(int n) {
xpt[] = ;
for (int i = ; i <= n ; i++ )
xpt[i] = xpt[i - ] * pt;
}
int check(int x) {
mp.clear();
ull t;
for (int j = ; j + x - < m ; j++ ) {
hat[] = ;
for (int i = ; i <= n ; i++)
hat[i] = hat[i - ] * pt + Hash.gethash(i, j, x);
for (int k = x ; k <= n ; k++) {
t = hat[k] - hat[k - x] * xpt[x];
if (mp.find(t) != mp.end()) {
ans1 = mp[t];
ans2 = ii(k - x + , j + );
return ;
} else mp[t] = ii(k - x + , j + );
}
}
return ;
}
int main() {
cin >> n >> m;
Hash.init1(m);
for (int i = ; i <= n ; i++) {
cin >> s[i];
Hash.init2(i, s[i]);
}
initxpt(n);
ans = ;
int l = , r = min(n, m) + , mid;
while(l <= r) {
mid = (l + r) >> ;
if(check(mid)) {
ans = mid;
l = mid + ;
} else r = mid - ;
}
if(ans) cout << ans << "\n" << ans1.first << " " << ans1.second << "\n" << ans2.first << " " << ans2.second << endl;
else cout << ans << "\n";
return ;
}

URAL - 1486 Equal Squares 二维哈希+二分的更多相关文章

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

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

  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. 理解Canvas像素边界

    大家看下面的例子 var context = document.getElementById('canvas').getContext('2d'); context.lineWidth = 1; co ...

  2. shell重温---基础篇(函数操作)

        linux shell 可以用户定义函数,然后在shell脚本中可以随便调用.shell中函数的定义格式如下: [ function ] funname [()] { action; [ret ...

  3. SAN---第二网的概念

    网络技术的优缺点:优点:连接能力,超强路由,管理能力,远距离缺点:低速以及高负载,强烈的软件需求,错误检测能力 SAN:storage area network(存储区域网络)--是一种基于光网的特殊 ...

  4. Android面试收集录 电话、短信和联系人、多媒体技术

    1.请写出调用系统拨号界面? Intent intent=new Intent(Intent.ACTION_DIAL,Uri.pase("tel:12345678910")); s ...

  5. 对C语言连等式的学习

    例子如下 [pgsql@localhost soft]$ cat test1.c #include <stdlib.h> #include <stdio.h> int main ...

  6. Java工程师笔试题整理[校招篇]

    Java工程师笔试题整理[校招篇]     隔着两个月即将开始校招了.你是不是也想借着这个机会崭露头角,拿到某些大厂的offer,赢取白富美.走上人生巅峰?当然如果你还没能打下Java基础,一定要先打 ...

  7. Java笔试题-线程编程方面

      Ja 线程编程方面 60.java中有几种方法可以实现一个线程?用什么关键字修饰同步方法?stop()和suspend()方法为何不推荐使用? 答:有两种实现方法,分别是继承Thread类与实现R ...

  8. java存储位置经典例子

    String a="a";String b="b";String c="ab";String d="ab";String ...

  9. fildder抓包工具详解

    fildder页面介绍名称和含义 名称 含义 # 抓取HTTP Request的顺序,从1开始,以此递增 Result HTTP状态码 Protocol 请求使用的协议,如HTTP/HTTPS/FTP ...

  10. Python网络编程(基础总结、 入门经典)

    Linux下文件类型:     bcd -lsp          b(块.设备文件)          c(字符设备文件)          d(目录)          -(普通文件)       ...