URAL - 1486 Equal Squares 二维哈希+二分
Input
Output
Example
input | output |
---|---|
5 10 |
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 二维哈希+二分的更多相关文章
- 【URAL 1486】Equal Squares(二维哈希+二分)
Description During a discussion of problems at the Petrozavodsk Training Camp, Vova and Sasha argued ...
- 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 ...
随机推荐
- python入门——Anaconda安装
初学Python,可以选择python原始的IDE,但原始的IDE在使用过程中需要自己安装各种包,个人觉得初学者不需要将时间花在这些上面,而是应该直接学习python程序,这些比较杂的事情可以在以后的 ...
- java 第六章 面向对象基础
1.面向对象编程思想 面向过程编程 传统的C语言属于面向过程编程.面向过程解决问题的思路:通常是分析出解决问题所需要的步骤,然后用方法把这些步骤一步一步实现,最后一个一个依次调用方法来解决. 面向过程 ...
- P1991 无线通讯网
P1991 无线通讯网 题目描述 国防部计划用无线网络连接若干个边防哨所.2 种不同的通讯技术用来搭建无线网络: 每个边防哨所都要配备无线电收发器:有一些哨所还可以增配卫星电话. 任意两个配备了一条卫 ...
- XenServer设置master,摧毁故障主机
XenServer pool 移除server 设置master 这分为Pool Master是正常还是异常2种情况: 正常情况下可能要对Pool Master做一些停机维护,比如换内存条啥的,此时在 ...
- 《Cracking the Coding Interview》读书笔记
<Cracking the Coding Interview>是适合硅谷技术面试的一本面试指南,因为题目分类清晰,风格比较靠谱,所以广受推崇. 以下是我的读书笔记,基本都是每章的课后习题解 ...
- Selenium LoadableComponent加载组件
继承LoadableComponent类可以在打开地址时, 判断浏览器是否打开了预期的网址, 需要重写load()与isLoad()方法: 即使没有定义get()方法, 也可以进行get()方法的调用 ...
- Django数据模型--字段详解
一.字段 1.CharField: 字段数据类型为字符串 class Test(models.Model): test = models.CharField(max_length=) 2.Intege ...
- [USACO18DEC]Cowpatibility(容斥 or bitset优化暴力)
题面 题意: 给出n个五元组(一个五元组的五个数互不相同),我们称两个五元组不和谐,当且仅当任意元素都不相同,求有多少对五元组不和谐. \(Solution:\) 很容易想到 Ans = 总共对数-和 ...
- linux备忘录-程序管理与SELinux
知识点 程序与程序(Process and Program) 通过执行一条指令或程序,则可以触发一个事件,并获得一个PID.当我们需要启动一个程序时,我们是启动一个二进制文件(binary file) ...
- php+Mysql中网页出现乱码的解决办法详解
$conn = mysql_connect("$host","$user","$password");mysql_query("S ...