【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 ...
随机推荐
- POJ 2540 Hotter Colder --半平面交
题意: 一个(0,0)到(10,10)的矩形,目标点不定,从(0,0)开始走,如果走到新一点是"Hotter",那么意思是离目标点近了,如果是"Colder“,那么就是远 ...
- 第27章 结构型模式大PK
27.1 代理模式 VS 装饰模式 27.1.1 代理模式 (1)场景:客人找运动员代理要求安排运动员参加比赛 (2)说明:代理人有控制权,可以拒绝客人的要求,也可以答应安排,甚至自己下去跑(因为有些 ...
- java 24 - 3 GUI之添加按钮
需求:把按钮添加到窗体,并对按钮添加一个点击事件. A:创建窗体对象 B:创建按钮对象 C:把按钮添加到窗体 D:窗体显示 注意:这里对按钮添加点击事件,同样使用监听器. 但是,这里的按钮是组件,所以 ...
- 转:eclipse的workspace和working set
from: http://iyuanbo.iteye.com/blog/1158136 eclipse的workspace和working set 2015-05-20 09:28:48 标签:e ...
- 利用substring()方法,把一个表的不同分级所对应的字段名取出来。
实例:现在有一个物料分类的表.知道表的第四级的值,要取前面的2,3级值. 例如,如图所示: 可以通过取前面几个字段的值,得到对应级别的值.利用substring(),但是因为要写在一句话里. 因此可以 ...
- XAMPP中proftpd的简明配置方法
XAMPP中proftpd的简明配置方法 用LAMPP的安装方法可以开一个默认的nobody用户,用lampp security就可以初始设置相应的默认用户密码.如果要有多用户,又怎样管理.目录怎 ...
- 10 Things Every Java Programmer Should Know about String
String in Java is very special class and most frequently used class as well. There are lot many thin ...
- C#将JSON字符串对象序列化与反序列化
C#将对象序列化成JSON字符串 public string GetJsonString() { List<Product> products = new List<Product& ...
- 协程python
python中协程 在引出协成概念之前先说说python的进程和线程. 进程: 进程是正在执行程序实例.执行程序的过程中,内核会讲程序代码载入虚拟内存,为程序变量分配空间,建立 bookkeeping ...
- Generate Parentheses
Generate Parentheses Given n pairs of parentheses, write a function to generate all combinations of ...