URAL 1486 Equal Squares

来源:互联网 发布:php curl 发送请求头 编辑:程序博客网 时间:2024/05/20 05:11
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 ≤  NM ≤ 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 10ljkfghdfasisdfjksiyepgljkijlgpeyisdafdsilnpglkfkjl
31 13 3
最大的相同矩阵,hash判断

#include<map>#include<cmath>    #include<queue>    #include<vector>#include<cstdio>    #include<cstring>    #include<algorithm>    using namespace std;#define ms(x,y) memset(x,y,sizeof(x))    #define rep(i,j,k) for(int i=j;i<=k;i++)    #define per(i,j,k) for(int i=j;i>=k;i--)    #define loop(i,j,k) for (int i=j;i!=-1;i=k[i])    #define inone(x) scanf("%d",&x)    #define intwo(x,y) scanf("%d%d",&x,&y)    #define inthr(x,y,z) scanf("%d%d%d",&x,&y,&z)    typedef long long LL;const int low(int x) { return x&-x; }const int INF = 0x7FFFFFFF;const int mod = 1e9 + 7;const int Mod = 276079;const int p = 131, pp = 811, ppp = 239;const int N = 1e3 + 10;char s[N][N];int n, m, x, y, xx, yy;int h1[N][N], h2[N][N];int ft[Mod], nt[Mod], u[Mod];map<int, int> M;bool check(int L){int P = 1, PP = 1, PPP = 1, tot = 0, cnt = 0;rep(i, 1, L) P = 1LL * P * p % mod, PP = 1LL * PP * pp % mod, PPP = 1LL * PPP * ppp % mod;rep(i, 1, n){int h = 0;rep(j, 1, m){h = (1LL * h * p + s[i][j]) % mod;if (j > L) h = (h + mod - 1LL * s[i][j - L] * P % mod) % mod;if (j >= L) h1[i][j - L + 1] = h;}}M.clear();rep(i, 1, m - L + 1){int h = 0, H = 0;rep(j, 1, n){h = (1LL * h * pp + h1[j][i]) % mod;H = (1LL * H * ppp + h1[j][i]) % mod;if (j > L){h = (h + mod - 1LL * h1[j - L][i] * PP % mod) % mod;H = (H + mod - 1LL * h1[j - L][i] * PPP% mod) % mod;}if (j >= L){h2[j - L + 1][i] = H;if (int v = M[h]){xx = j - L + 1; yy = i;loop(k, ft[v], nt){x = (u[k] - 1) / m + 1;y = (u[k] - 1) % m + 1;if (h2[x][y] == h2[xx][yy]) return true;}u[tot] = (j - L) * m + i;nt[tot] = ft[v]; ft[v] = tot++;}else{M[h] = ++cnt;u[tot] = (j - L) * m + i;nt[tot] = -1; ft[cnt] = tot++;}}}}return false;}int main(){while (~intwo(n, m)){rep(i, 1, n) scanf("%s", s[i] + 1);int l = 1, r = min(n, m);while (l <= r){int mid = l + r >> 1;if (check(mid)) l = mid + 1; else r = mid - 1;}printf("%d\n", r);if (r) check(r), printf("%d %d\n%d %d\n", x, y, xx, yy);}return 0;}

0 0