• URAL


    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 sizeK, 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 << "
    " << ans1.first << " " << ans1.second << "
    " << ans2.first << " " << ans2.second << endl;
    78     else cout << ans << "
    ";
    79     return 0;
    80 }
  • 相关阅读:
    show processlist 输出ID 和 information_schema.PROCESSLIST 的id,information_schema.innodb_trx的TRX_MYSQL_T
    用 Flask 来写个轻博客 (15) — M(V)C_实现博文页面评论表单
    第十二章 对象(下)
    十大最值得关注的国内大数据分析厂商
    第十二章 对象(上)
    mysql 区间锁 对于没有索引 非唯一索引 唯一索引 各种情况
    insert into select 堵塞update
    监控持有sql和被堵塞的sql
    人生应该有间隔年——北漂18年(75)
    ERROR: transport error 202: connect failed: Connection timed out
  • 原文地址:https://www.cnblogs.com/qldabiaoge/p/9157229.html
Copyright © 2020-2023  润新知