#include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int N = 1005; char s[N][N]; int mat[N][N], l[N][N], r[N][N], h[N][N], n, m, ans;
voidmakeMat (char a, char b, char c) { memset (mat, 0, sizeof (mat)); memset (l, 0, sizeof (l)); memset (r, 0, sizeof (r)); memset (h, 0, sizeof (h)); for (int i = 1; i <= n; ++i) { h[i][0] = h[i][m + 1] = -1; for (int j = 1; j <= m; ++j) { l[i][j] = r[i][j] = j; if (s[i][j] == a || s[i][j] == b || s[i][j] == c) mat[i][j] = 1; if (mat[i][j] == 0) h[i][j] = h[i - 1][j] + 1; } } }
int solve (char a, char b, char c) { makeMat (a, b, c); int aans = 0; for (int i = 1; i <= n; ++i) { for (int j = m; j >= 1; --j) while (h[i][r[i][j] + 1] >= h[i][j]) r[i][j] = r[i][r[i][j] + 1];
for (int j = 1; j <= m; ++j) { while (h[i][l[i][j] - 1] >= h[i][j]) l[i][j] = l[i][l[i][j] - 1]; aans = max (aans, (r[i][j] - l[i][j] + 1) * h[i][j]); } } return aans; }
int main() { while (~scanf ("%d%d", &n, &m)) { for (int i = 1; i <= n; ++i) scanf ("%s", s[i] + 1); ans = max (solve ('x', 'b', 'c'), solve ('a', 'y', 'c')); ans = max (ans, solve ('a', 'b', 'w')); printf ("%d\n", ans); } return0; }
Largest Submatrix
Problem Description
Now here is a matrix with letter ‘a’,’b’,’c’,’w’,’x’,’y’,’z’ and you can change ‘w’ to ‘a’ or ‘b’, change ‘x’ to ‘b’ or ‘c’, change ‘y’ to ‘a’ or ‘c’, and change ‘z’ to ‘a’, ‘b’ or ‘c’. After you changed it, what’s the largest submatrix with the same letters you can make? Input
The input contains multiple test cases. Each test case begins with m and n (1 ≤ m, n ≤ 1000) on line. Then come the elements of a matrix in row-major order on m lines each with n letters. The input ends once EOF is met. Output
For each test case, output one line containing the number of elements of the largest submatrix of all same letters. Sample Input