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 的pip方法安装第三方库
超级开心啊!!!!!!!!!!!!! win10 打开cmd Installing with get-pip.py To install pip, securely download get-pip. ...
- Linux编程之Epoll高并发
网络上所有资料都说epoll是高并发.单线程.IO重叠服用的首选架构,比select和poll性能都要好,特别是在有大量不活跃连接的情况下.具体原理就不阐述了,下面说说使用. 具有有三个函数: #in ...
- JAVA 泛型之类型擦除
★ 泛型是 JDK 1.5 版本引进的概念,之前是没有泛型的概念的,但泛型代码能够很好地和之前版本的代码很好地兼容. CollectionTest.java ---编译成CollectionTest. ...
- java web项目使用ant编译将不同的功能代码打包成jar,进而分局点将项目打包成不同的tar.gz包进而部署
使用ant可以轻松的将一个项目分离代码,直接打包成不同需求的tar.gz包使用 1.build.properties (属性) version.num=1.0 #版本信息 2.build.xml (a ...
- 如何保证HashMap线程安全
可使用Java 1.5推荐的java.util.concurrent包ConcurrentHashMap来实现,内部不再使用类似HashTable的synchronized同步锁,而是使用Reentr ...
- python2.7练习小例子(二十二)
22):题目:有一分数序列:2/1,3/2,5/3,8/5,13/8,21/13...求出这个数列的前20项之和. 程序分析:请抓住分子与分母的变化规律. #!/usr/bin/pyt ...
- 12 TCP服务器 进程 线程 非阻塞
1.单进程服务器 from socket import * serSocket = socket(AF_INET, SOCK_STREAM) # 重复使用绑定的信息 serSocket.setsock ...
- LeetCode题目解答
LeetCode题目解答——Easy部分 Posted on 2014 年 11 月 3 日 by 四火 [Updated on 9/22/2017] 如今回头看来,里面很多做法都不是最佳的,有的从复 ...
- java存储位置经典例子
String a="a";String b="b";String c="ab";String d="ab";String ...
- VectorDrawable在Android中的配置
一.让Android支持VectorDrawable apply plugin: 'com.android.application' android { defaultConfig { vectorD ...