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. 插头DP(基于连通性状态压缩的动态规划问题)(让你从入门到绝望)

    今天,我,Monkey king 又为大家带来大(ju)佬(ruo)的算法啦!--插头DP 例题(菜OJ上的网址:http://caioj.cn/problem.php?id=1489): 那么,这道 ...

  2. 1139: [POI2009]Wie

    1139: [POI2009]Wie https://www.lydsy.com/JudgeOnline/problem.php?id=1139 分析: Dijkstra.状压最短路,dis[i][j ...

  3. java中array,arrayList,iterator;

    Array        String []a = new String[10] ;  a[0] = "test" ; String []a = new String[]{&quo ...

  4. 响应式js设置

    <script> (function anonymous() { // 声明一个函数,并直接的执行 function computed() { let HTML = document.do ...

  5. allegro导入网表过程中出现的错误信息

    1. 找不到焊盘PAD,下面这句话的意思是器件封装找不到焊盘46.pad WARNING(SPMHNI-): Unable to load symbol ): Could not find padst ...

  6. vs2015-Cordova开发安卓应用环境搭建

    之前看到过用html5+css+js就可以开发跨平台的应用,然后发现vs2015里就有个Cordova项目所以就想试试,但并不是这么顺利.刚开始对安卓环境一点也不了解,就到处百度搜索.终于成功了. 首 ...

  7. fastlane自动化打包ipa并发布到firim或者蒲公英

    1.打开终端,确保Xcode Command Line Tools 安装了最新版 xcode-select --install 2.安装fastlane sudo gem install -n /us ...

  8. 【功能笔记】Ubuntu查看系统资源占用(内存,cpu和进程) {转载}

    转载自http://bluexp29.blog.163.com/blog/static/33858148201071534450856/ linux真是太强大了. 查看ubuntu的资源占用的命令为$ ...

  9. localStorage简析

    声明:引用自http://www.cnblogs.com/st-leslie/p/5617130.html 一.什么是localStorage.sessionStorage 在HTML5中,新加入了一 ...

  10. 解析车辆VIN码识别(车架号识别)系统

    很多人在购买车辆的时候,只关注性能.外观.内饰等,其实真正的内行是首先看车辆的VIN码,也叫车架号码. VIN码(车架号码)是一辆车的唯一身份证明,一般在车辆的挡风玻璃处,有的在车辆防火墙上,或B柱铭 ...