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

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

本来应该取模判断相等后再暴力扫矩阵来判断,但是我看到《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. 探索 OpenStack 之(17):计量模块 Ceilometer 中的数据收集机制

    本文将阐述 Ceilometer 中的数据收集机制.Ceilometer 使用三种机制来收集数据: Notifications:Ceilometer 接收 OpenStack 其它服务发出的 noti ...

  2. (五)适配器模式-C++实现

    将一个类的接口转换成客户希望的另外一个接口.Adapter模式使得原本由于接口不兼容而不能一起工作的那些类可以一起工作. 该模式中有三种角色: 1.目标:是一个抽象类,它是客户想使用的接口 2.被适配 ...

  3. Hibernate一对一关系映射

    Hibernate提供了两种一对一映射关联关系的方式: 1)按照外键映射 2)按照主键映射 下面以员工账号表和员工档案表(员工账号和档案表之间是一对一的关系)为例,介绍这两种映射关系,并使用这两种 映 ...

  4. jvm虚拟机性能监控与故障处理工具

    java开发人员肯定知道jdk的bin目录中有java.exe javac.exe这两个命令行工具,但并非所有程序员都了解过jdk的bin目录之中其他命令行的作用.jdk的工具,体积都比较小,这些命令 ...

  5. 时间就像Hourglass一样,积累(沉淀)越多,收获越大

    package cn.bdqn; public class Hourglass { public static void main(String[] args) { for (int i = 2; i ...

  6. 技术专题—Python黑客【优质内容聚合贴】

    作者:坏蛋链接:https://zhuanlan.zhihu.com/p/24645819来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处. 一.前言 本着知识分享,聚合优 ...

  7. splay总结

    以此文纪念人生首次竞赛大选 这里主要讲一讲splay的区间操作,我讲的是指针实现,程序的效率可能比较低,更偏重代码的可读可写性,语言风格不是很优美有效,不喜勿喷 零.初始化结构体 1)这里主要是初始化 ...

  8. HTML 学习笔记 CSS样式(外边框 外边框合并)

    CSS外边距 围绕在元素边框的空白区域就是外边距  设置外边距会在元素外创建额外的空白 设置外边距的最简单的方法就是使用 margin 属性,这个属性接受任何长度单位.百分数值甚至负值. CSS ma ...

  9. SharePoint 2010自定义母版页小技巧——JavaScript和CSS引用

    通常在我们的项目中,都会涉及到母版页的定制.并且必不可少的,需要配合以一套自己的JavaScript框架和CSS样式.你有没有遇到过这样的情况呢,在开发环境和UAT时都还算顺利,但是当最终部署到生产服 ...

  10. Windows 8.1 新增控件之 TimePicker

    之前已经为大家介绍过DatePicker 控件的相关内容,有日期控件当然就得有时间控件,本篇将和各位一起了解TimePicker 的使用方法. 先来介绍一下ClockIdentifier 属性,默认情 ...