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

During a discussion of problems at the Petrozavodsk Training Camp, Vova and Sasha argued about who of them could in 300 minutes find a pair of equal squares of the maximal size in a matrix of size  N ×  M containing lowercase English letters. Squares could overlap each other but could not coincide. He who had found a pair of greater size won. Petr walked by, looked at the matrix, said that the optimal pair of squares had sides  K, and walked on. Vova and Sasha still cannot find this pair. Can you help them?

Input

The first line contains integers  N and  M separated with a space. 1 ≤   N,   M ≤ 500. In the next  N lines there is a matrix consisting of lowercase English letters,  M symbols per line.

Output

In the first line, output the integer  K which Petr said. In the next two lines, give coordinates of upper left corners of maximal equal squares. If there exist more than one pair of equal squares of size K, than you may output any of them. The upper left cell of the matrix has coordinates (1, 1), and the lower right cell has coordinates ( N,   M). If there are no equal squares in the matrix, then output 0.

Example

inputoutput
5 10
ljkfghdfas
isdfjksiye
pgljkijlgp
eyisdafdsi
lnpglkfkjl
3
1 1
3 3

 

 

    先要对每一个字符串进行哈希预处理,哈希处理后,把哈希的值在根据行数哈希一遍

     没存过的就存一遍, 找到有没有相同的,

    这个二分还是比较好写的

   这个二维哈希就有点懵逼  

 

   

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <string>
 4 #include <map>
 5 #include <cstring>
 6 #include <algorithm>
 7 using namespace std;
 8 
 9 typedef long long LL;
10 typedef unsigned long long ull;
11 typedef pair<int, int> ii;
12 const int maxn = 510;
13 const ull p = 1e12 + 7, pt = 19191919191919;
14 
15 struct h {
16     ull ha[maxn][maxn], xp[maxn], sz;
17     void init1(int n) {
18         sz = n;
19         xp[0] = 1;
20         for (int i = 1 ; i <= sz ; i++)
21             xp[i] = xp[i - 1] * p;
22     }
23     void init2(int id, const string &str) {
24         ha[id][sz] = 0;
25         for (int i = sz - 1 ; i >= 0 ; i--)
26             ha[id][i] = ha[id][i + 1] * p + (str[i] - 'a' + 1);
27     }
28     ull gethash(int id, int st, int len) {
29         return ha[id][st] - ha[id][st + len] * xp[len];
30     }
31 } Hash;
32 string s[maxn];
33 map< ull, ii>mp;
34 int n, m, ans;
35 ii ans1, ans2;
36 ull hat[maxn], xpt[maxn];
37 void initxpt(int n) {
38     xpt[0] = 1;
39     for (int i = 1 ; i <= n ; i++ )
40         xpt[i] = xpt[i - 1] * pt;
41 }
42 int check(int x) {
43     mp.clear();
44     ull t;
45     for (int j = 0 ; j + x - 1 < m ; j++ ) {
46         hat[0] = 0;
47         for (int i = 1 ; i <= n ; i++)
48             hat[i] = hat[i - 1] * pt + Hash.gethash(i, j, x);
49         for (int k = x ; k <= n ; k++) {
50             t = hat[k] - hat[k - x] * xpt[x];
51             if (mp.find(t) != mp.end()) {
52                 ans1 = mp[t];
53                 ans2 = ii(k - x + 1, j + 1);
54                 return 1;
55             } else mp[t] = ii(k - x + 1, j + 1);
56         }
57     }
58     return 0;
59 }
60 int main() {
61     cin >> n >> m;
62     Hash.init1(m);
63     for (int i = 1 ; i <= n ; i++) {
64         cin >> s[i];
65         Hash.init2(i, s[i]);
66     }
67     initxpt(n);
68     ans = 0;
69     int l = 0, r = min(n, m) + 1, mid;
70     while(l <= r) {
71         mid = (l + r) >> 1;
72         if(check(mid)) {
73             ans = mid;
74             l = mid + 1;
75         } else r = mid - 1;
76     }
77     if(ans) cout << ans << "\n" << ans1.first << " " << ans1.second << "\n" << ans2.first << " " << ans2.second << endl;
78     else cout << ans << "\n";
79     return 0;
80 }

 

转载于:https://www.cnblogs.com/qldabiaoge/p/9157229.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值