题意:求给定字符矩阵中相同正方形矩阵的最大边长和这两个相同正方形的位置

第一次写字符串哈希,选两个不同的模数进行二维字符串哈希。

本来应该取模判断相等后再暴力扫矩阵来判断,但是我看到《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的更多相关文章

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

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

  2. 【BZOJ 1486】 [HNOI2009]最小圈

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 我们可以只想那个均值最小的环. 我们不知道那个环由哪些边构成 但我们可以把每条边都减掉mid 那个环受到的影响是什么呢? 如果这个均 ...

  3. 【URAL 1519】Formula 1

    http://acm.timus.ru/problem.aspx?space=1&num=1519 调了好久啊.参考(抄)的iwtwiioi的题解. 如果想要题解,题解在<基于连通性状态 ...

  4. 【URAL 1018】Binary Apple Tree

    http://vjudge.net/problem/17662 loli蜜汁(面向高一)树形dp水题 #include<cstdio> #include<cstring> #i ...

  5. 【URAL 1297】Palindrome 最长回文子串

    模板题,,,模板打错查了1h+QAQ #include<cmath> #include<cstdio> #include<cstring> #include< ...

  6. 【URAL 1917】Titan Ruins: Deadly Accuracy(DP)

    题目 #include<cstdio> #include<algorithm> using namespace std; #define N 1005 int n, m, cn ...

  7. 【URAL 1989】 Subpalindromes(线段树维护哈希)

    Description You have a string and queries of two types: replace i'th character of the string by char ...

  8. 【赛时总结】◇赛时·V◇ Codeforces Round #486 Div3

    ◇赛时·V◇ Codeforces Round #486 Div3 又是一场历史悠久的比赛,老师拉着我回来考古了……为了不抢了后面一些同学的排名,我没有做A题 ◆ 题目&解析 [B题]Subs ...

  9. URAL - 1486 Equal Squares 二维哈希+二分

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

随机推荐

  1. stanford coursera 机器学习编程作业 exercise 5(正则化线性回归及偏差和方差)

    本文根据水库中蓄水标线(water level) 使用正则化的线性回归模型预 水流量(water flowing out of dam),然后 debug 学习算法 以及 讨论偏差和方差对 该线性回归 ...

  2. 校园导游系统(C++实现,VC6.0编译,使用EasyX图形库)

    运行效果: 说明: 由于当年还不会使用多线程,所以很多获取用户点击的地方都是使用循环实现的...CPU占用率会比较高. 代码: //校园导游系统.cpp 1 #include <graphics ...

  3. AC日记——字符串P型编码 openjudge 1.7 31

    31:字符串p型编码 总时间限制:  1000ms 内存限制:  65536kB 描述 给定一个完全由数字字符('0','1','2',…,'9')构成的字符串str,请写出str的p型编码串.例如: ...

  4. [No000021]跟维多利亚学英语

  5. [No000014]听说不背单词,考英语会是这种下场-我们为什么必须背单词?

    由于英语对于一个程序员来说,重要性你懂得.因此我会开始逐渐在博客上加入英语的一些东西. 听说不背单词,考英语会是这种下场 在中国, 「学英语」大抵遵循着这样一条 罗蒙诺索夫质量守恒定律 因为英语学着学 ...

  6. 那些强悍的PHP一句话后门

    强悍的PHP一句话后门这类后门让网站.服务器管理员很是头疼,经常要换着方法进行各种检测,而很多新出现的编写技术,用普通的检测方法是没法发现并处理的.今天我们细数一些有意思的PHP一句话木马.利用404 ...

  7. Oracle Update 语句语法与性能分析 - 多表关联

    Oracle Update 语句语法与性能分析 - 多表关联   为了方便起见,建立了以下简单模型,和构造了部分测试数据: 在某个业务受理子系统BSS中, SQL 代码 --客户资料表 create ...

  8. css3动画中的steps值详解

    css3的动画的animation-timing-function属性定义了动画的速度曲线,一般的速度曲线大家都知道,什么ease,linear,ease-in,ease-out,还有自定义贝塞尔曲线 ...

  9. (原创)mybatis学习一,夯实基础

    一,what?(是什么) MyBatis是一个支持普通SQL查询,存储过程和高级映射的优秀持久层框架.MyBatis消除了几乎所有的JDBC代码和参数的手工设置以及对结果集的检索封装.MyBatis可 ...

  10. HTTP 错误 500.22 - Internal Server Error

    HTTP 错误 500.22 - Internal Server Error 检测到在集成的托管管道模式下不适用的 ASP.NET 设置. 最可能的原因: 此应用程序在 system.web/http ...