【URAL 1486】Equal Squares
题意:求给定字符矩阵中相同正方形矩阵的最大边长和这两个相同正方形的位置
第一次写字符串哈希,选两个不同的模数进行二维字符串哈希。
本来应该取模判断相等后再暴力扫矩阵来判断,但是我看到《Hash在信息学竞赛中的一类应用》中这么写道:

于是我还会再次判重吗?肯定不会!!!
于是这样写完后就调啊调,调出几个像没用unsigned long long这样的脑残错误后交上去就A了233
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef unsigned long long ll;
const int N = 503;
const ll p = 100007;
const int p1 = 1007;
const int p2 = 10007; struct node {
ll to; int nxt, x, y;
node (ll _to = 0, int _nxt = 0, int _x = 0, int _y = 0) : to(_to), nxt(_nxt), x(_x), y(_y) {}
} E[N * N]; char s[N][N];
int point[p + 3], cnt, n, m;
ll hash1[N][N], hash[N][N], pow1[N], pow2[N]; void ins(ll from, ll to, int x, int y) {E[++cnt] = node(to, point[from], x, y); point[from] = cnt;}
int __(ll t) {
ll f = t % p;
for(int i = point[f]; i; i = E[i].nxt)
if (E[i].to == t) return i;
return 0;
}
bool _(int t) {
memset(point, 0, sizeof(point)); cnt = 0;
ll num;
for(int i = 1; i <= n - t + 1; ++i)
for(int j = 1; j <= m - t + 1; ++j) {
num = hash[i + t - 1][j + t - 1] - hash[i + t - 1][j - 1] * pow1[t] - hash[i - 1][j + t - 1] * pow2[t] + hash[i - 1][j - 1] * pow1[t] * pow2[t];
if (__(num)) return 1;
ins(num % p, num, i, j);
}
return 0;
} int main() {
scanf("%d%d", &n, &m);
for(int i = 1; i <= n; ++i) scanf("%s", s[i] + 1);
pow1[0] = 1; pow2[0] = 1;
for(int i = 1; i < N; ++i) pow1[i] = pow1[i - 1] * p1, pow2[i] = pow2[i - 1] * p2;
for(int i = 1; i <= n; ++i)
for(int j = 1; j <= m; ++j) {
hash1[i][j] = hash1[i][j - 1] * p1 + (int) s[i][j];
hash[i][j] = hash[i - 1][j] * p2 + hash1[i][j];
}
int left = 0, right = n == m ? n - 1 : min(n, m), mid;
while (left < right) {
mid = (left + right + 1) >> 1;
if (_(mid)) left = mid;
else right = mid - 1;
}
if (left == 0) puts("0");
else {
printf("%d\n", left);
memset(point, 0, sizeof(point)); cnt = 0;
for(int i = 1; i <= n - left + 1; ++i)
for(int j = 1; j <= m - left + 1; ++j) {
ll num = hash[i + left - 1][j + left - 1] - hash[i + left - 1][j - 1] * pow1[left] - hash[i - 1][j + left - 1] * pow2[left] + hash[i - 1][j - 1] * pow1[left] * pow2[left];
if (right = __(num)) {
printf("%d %d\n%d %d\n", E[right].x, E[right].y, i, j);
return 0;
}
ins(num % p, num, i, j);
}
} return 0;
}
神奇的哈希啊,我到现在都对你的时间复杂度感到迷茫~
【URAL 1486】Equal Squares的更多相关文章
- 【URAL 1486】Equal Squares(二维哈希+二分)
Description During a discussion of problems at the Petrozavodsk Training Camp, Vova and Sasha argued ...
- 【BZOJ 1486】 [HNOI2009]最小圈
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 我们可以只想那个均值最小的环. 我们不知道那个环由哪些边构成 但我们可以把每条边都减掉mid 那个环受到的影响是什么呢? 如果这个均 ...
- 【URAL 1519】Formula 1
http://acm.timus.ru/problem.aspx?space=1&num=1519 调了好久啊.参考(抄)的iwtwiioi的题解. 如果想要题解,题解在<基于连通性状态 ...
- 【URAL 1018】Binary Apple Tree
http://vjudge.net/problem/17662 loli蜜汁(面向高一)树形dp水题 #include<cstdio> #include<cstring> #i ...
- 【URAL 1297】Palindrome 最长回文子串
模板题,,,模板打错查了1h+QAQ #include<cmath> #include<cstdio> #include<cstring> #include< ...
- 【URAL 1917】Titan Ruins: Deadly Accuracy(DP)
题目 #include<cstdio> #include<algorithm> using namespace std; #define N 1005 int n, m, cn ...
- 【URAL 1989】 Subpalindromes(线段树维护哈希)
Description You have a string and queries of two types: replace i'th character of the string by char ...
- 【赛时总结】◇赛时·V◇ Codeforces Round #486 Div3
◇赛时·V◇ Codeforces Round #486 Div3 又是一场历史悠久的比赛,老师拉着我回来考古了……为了不抢了后面一些同学的排名,我没有做A题 ◆ 题目&解析 [B题]Subs ...
- URAL - 1486 Equal Squares 二维哈希+二分
During a discussion of problems at the Petrozavodsk Training Camp, Vova and Sasha argued about who o ...
随机推荐
- Javascript的DOM操作 - 你真的了解吗?
摘要 想稍微系统的说说对于DOM的操作,把Javascript和jQuery常用操作DOM的内容归纳成思维导图方便阅读,同时加入性能上的一些问题. 前言 在前端开发的过程中,javascript极为重 ...
- 03 Hibernate错题分析
1.在Hibernate中,以下关于主键生成器说法错误的是( C). A.increment可以用于类型为long.short或byte的主键 B.identity用于如SQL Server.DB2. ...
- 在Flex4中嵌入字体
如果要使用的字体不是系统字体,可以把字体嵌入到Flash中,然后引用该字体.不过字体文件一般都比较大,慎重使用该功能. 官方例子 http://help.adobe.com/en_US/flex/us ...
- java 28 - 2 设计模式之 模版设计模式
模版设计模式 模版设计模式概述 模版方法模式就是定义一个算法的骨架,而将具体的算法延迟到子类中来实现 优点 使用模版方法模式,在定义算法骨架的同时,可以很灵活的实现具体的算法,满足用户灵活多变的需求 ...
- 使用javascript实现的雪花飞舞的效果
原作者是在body中不停的插入多个小div雪花来向下慢慢飘,一直飘到body的底部后,将雪花移除,于是,将原来的代码稍加修改,让他只是从屏幕的顶部飘落到屏幕底部(不是body的底部)后,就将雪花移除, ...
- HTML 学习笔记 JavaScript (实现)
HTML中的脚本 必须位于<script></script>标签之间 脚本可被放置在HTML页面的<body>和<head>部分中 <script ...
- Java:对象的强、软、弱和虚引用
1.对象的强.软.弱和虚引用 在JDK 1.2以前的版本中,若一个对象不被任何变量引用,那么程序就无法再使用这个对象.也就是说,只有对象处于可触及(reachable)状态,程序才能使用它.从JDK ...
- 数据类型之记录(record)..With XXX do begin... end;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 type Mai ...
- DLL放在指定目录 以及设置dll调用路径
一.DLL放在指定目录 在编写C# winform程序中,不免一个项目会有多个工程文件,而这些工程文件之间是相互引用的,所以不想将工程的生成结果(exe或者dll)放在当前工程bin目录下的Debug ...
- android 中退出程序的两种方式
转自:http://blog.sina.com.cn/s/blog_5da93c8f0100t76l.html 思考:如何安全的退出程序? finish是Activity的类,仅仅针对Activity ...