BZOJ1397 Ural 1486 Equal squares

首先二分答案ans,然后只要进行判断答案ans是否可行即可。

验证方法:首先对每一个位置,求出它开始长度为ans的横行的hash值

然后求出每一个hash值的长度为ans的竖列的Hash值

查看是否有两个Hash值相同即可(比如我们可以基数排序。。。做什么大死!)

 1 /**************************************************************
 2     Problem: 1397
 3     User: rausen
 4     Language: C++
 5     Result: Accepted
 6     Time:2084 ms
 7     Memory:5052 kb
 8 ****************************************************************/
 9
10 #include <cstdio>
11 #include <algorithm>
12
13 using namespace std;
14 typedef long long ll;
15
16 const ll B1 = 103;
17 const ll B2 = 211;
18 const int N = 505;
19
20 int n, m;
21 char a[N][N];
22 ll p1[N], p2[N];
23 ll h[N][N], hash[N * N];
24
25 bool check(int x) {
26   int i, j, t;
27   ll tmp;
28   for (i = 1; i <= n; ++i) {
29     for (tmp = 0, j = 1; j < x; ++j)
30       (tmp *= B1) += a[i][j], h[i][j] = 0;
31     for (j = x; j <= m; ++j)
32       h[i][j] = tmp = tmp * B1 - p1[x] * a[i][j - x] + a[i][j];
33   }
34   for (t = 0, i = x; i <= m; ++i) {
35     for (tmp = 0, j = 1; j < x; ++j)
36       (tmp *= B2) += h[j][i];
37     for (j = x; j <= n; ++j)
38       hash[t++] = tmp = tmp * B2 - p2[x] * h[j - x][i] + h[j][i];
39   }
40   sort(hash, hash + t);
41   for (i = 1; i < t; ++i)
42     if (hash[i] == hash[i - 1]) return 1;
43   return 0;
44 }
45
46 int main() {
47   int i, j, l, r, mid;
48   scanf("%d%d\n", &n, &m);
49   for (i = 1; i <= n; ++i) {
50     gets(a[i] + 1);
51     for (j = 1; j <= m; ++j)
52       a[i][j] -= ‘a‘ - 1;
53   }
54   l = 1, r = min(n, m) + 1, p1[0] = p2[0] = 1;
55   for (i = 1; i <= r; ++i)
56     p1[i] = p1[i - 1] * B1, p2[i] = p2[i - 1] * B2;
57   while (l + 1 < r) {
58     mid = (l + r) >> 1;
59     if (check(mid)) l = mid;
60     else r = mid;
61   }
62   printf("%d\n", l);
63   return 0;
64 }

时间: 2024-10-16 13:13:17

BZOJ1397 Ural 1486 Equal squares的相关文章

URAL 1486 Equal Squares 二维Hash

Hash在信息学竞赛中的一类应用 论文中的第一道例题,关于二维hash的一些处理方法和小技巧 通过RK法计算hash值如果不取模的话可以O(n)预处理,然后O(1)得到任意一个字串的hash值 得到任意子串的hash值的时候不能用除和取模运算了,显然是错的 二维hash如果使用RK法每一次的p值必须不一样 如果不能确定hash值一定不是唯一的,可以计算一个用来确定pos 的hash值和一个用来确定值的hash值 代码写的相当挫,不忍直视 #include <cstdio> #include &

【URAL 1486】Equal Squares

题意:求给定字符矩阵中相同正方形矩阵的最大边长和这两个相同正方形的位置 第一次写字符串哈希,选两个不同的模数进行二维字符串哈希. 本来应该取模判断相等后再暴力扫矩阵来判断,但是我看到<Hash在信息学竞赛中的一类应用>中这么写道: 于是我还会再次判重吗?肯定不会!!! 于是这样写完后就调啊调,调出几个像没用unsigned long long这样的脑残错误后交上去就A了233 #include<cstdio> #include<cstring> #include<

URAL 1486(二维字符串hash)

题意:一个最大500*500的字符矩阵,求最大的两个相同的字符正方形.正方形可以有重叠部分但不能重合. 解法:首先是二分正方形的长度,然后判断某个长度存在时候计算字符矩阵的二维hash值,二维hash的方法是: 这样子拓展的hash算法可以O(1) 获取任意一个子矩阵的hash值. 代码: /****************************************************** * @author:xiefubao *****************************

Polyomino Composer(UVA12291)

Description   Polyomino Composer  A polyomino is a plane geometric figure formed by joining one or more equal squares edge to edge. - Wikipedia Given a large polyomino and a small polyomino, your task is to determine whether you can compose the large

CodeForces 316c1 Tidying Up

Tidying Up Time Limit: 4000ms Memory Limit: 262144KB This problem will be judged on CodeForces. Original ID: 316C164-bit integer IO format: %I64d      Java class name: (Any) Smart Beaver is careful about his appearance and pays special attention to s

时间不够了,先保存一下

polyomino is a plane geometric figure formed by joining one or more equal squares edge to edge. - Wikipedia Given a large polyomino and a small polyomino, your task is to determine whether you can compose the large one with two copies of the small on

CodeForces-Round524 A~D

A. Petya and Origami time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Petya is having a party soon, and he has decided to invite his nn friends. He wants to make invitations in the form of o

URAL 1572. Yekaterinozavodsk Great Well(数学啊)

题目链接:http://acm.timus.ru/problem.aspx?space=1&num=1572 About a month ago two teams of the Ural State University returned from the Yekaterinozavodsk training camp. This northern city impressed them so much that they decided to return there by any mean

Codeforces Round #337 (Div. 2) B. Vika and Squares 水题

B. Vika and Squares Vika has n jars with paints of distinct colors. All the jars are numbered from 1 to n and the i-th jar contains ai liters of paint of color i. Vika also has an infinitely long rectangular piece of paper of width 1, consisting of s