zoukankan      html  css  js  c++  java
  • 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 }
  • 相关阅读:
    Top 10 Product Manager Skills To Boost Your Resume In 2021
    大数据知识梳理
    B端产品如何设计权限系统?
    华三盒式交换机MAC、ARP、Route性能表项参数查询
    中了传说中的挖矿病毒
    SqlServer 2019 事务日志传送
    docker中生成的pdf中文是方框的解决方案
    The Live Editor is unable to run in the current system configuration
    2021 面试题大纲
    五分钟搞定Docker安装ElasticSearch
  • 原文地址:https://www.cnblogs.com/qldabiaoge/p/9157229.html
Copyright © 2011-2022 走看看