Largest Submatrix
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1690 Accepted Submission(s): 805
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
2 4 abcw wxyz
Sample Output
3
AC代码如下:
//
// HDU 2870 Largest Submatrix
//
// Created by TaoSama on 2015-02-12
// Copyright (c) 2014 TaoSama. All rights reserved.
//
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <set>
#include <vector>
#define CLR(x,y) memset(x, y, sizeof(x))
using namespace std;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
const int N = 1e5 + 10;
int n, m, h[1005], l[1005], r[1005];
char a[1005][1005];
int cal(char c) {
int ret = -INF;
memset(h, 0, sizeof h); h[0] = h[m + 1] = -1;
for(int i = 1; i <= n; ++i) {
for(int j = 1; j <= m; ++j) {
if(c == 'a') {
if(a[i][j] == 'w' || a[i][j] == 'y' || a[i][j] == 'z' || a[i][j] == c)
++h[j];
else h[j] = 0;
} else if(c == 'b') {
if(a[i][j] == 'w' || a[i][j] == 'x' || a[i][j] == 'z' || a[i][j] == c)
++h[j];
else h[j] = 0;
} else {
if(a[i][j] == 'x' || a[i][j] == 'y' || a[i][j] == 'z' || a[i][j] == c)
++h[j];
else h[j] = 0;
}
}
for(int j = 1; j <= m; ++j) l[j] = r[j] = j;
for(int j = 1; j <= m; ++j)
while(h[l[j] - 1] >= h[j])
l[j] = l[l[j] - 1];
for(int j = m; j >= 1; --j)
while(h[r[j] + 1] >= h[j])
r[j] = r[r[j] + 1];
for(int j = 1; j <= m; ++j)
ret = max(ret, (r[j] - l[j] + 1) * h[j]);
}
return ret;
}
int main() {
#ifdef LOCAL
freopen("in.txt", "r", stdin);
// freopen("out.txt","w",stdout);
#endif
ios_base::sync_with_stdio(0);
while(cin >> n >> m) {
for(int i = 1; i <= n; ++i) cin >> a[i] + 1;
int ans = max(cal('a'), max(cal('b'), cal('c')));
cout << ans << endl;
}
return 0;
}