难度:5
前面刚做过一维的求最大子矩阵面积,然后这道题其实就是一维变二维,转化一下就可以了,做的时候不会做瞄了一眼题解,真的是太巧妙了,只要理解了二维怎么转一维,就和前面的一模一样了,然后求法也都是用单调栈,
#include <bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define all(x) (x).begin(), (x).end()
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pa;
int main() {
int t;
cin >> t;
int ok = 1;
while (t--) {
if (ok) ok = 0;
else cout << endl;
string s[30];
cin >> s[0];
int n = s[0].size();
int mat[30][30] = {};
for (int i = 0; i < n; i++) {
if (s[0][i] == '0') mat[0][i] = 0;
else mat[0][i] = 1;
}
for (int i = 1; i < n; i++) {
cin >> s[i];
for (int j = 0; j < n; j++) {
if (s[i][j] == '0') mat[i][j] = 0;
else mat[i][j] = mat[i - 1][j] + 1;
}
}
int ans = 0;
for (int i = 0; i < n; i++) {
stack<pa> st;
for (int j = 0; j < n; j++) {
if (!j) st.push(make_pair(mat[i][j], 1));
else {
if (mat[i][j] >= st.top().fi) st.push(make_pair(mat[i][j], 1));
else {
int tmp = 0;
while (!st.empty() && mat[i][j] < st.top().fi) {
pa p = st.top(); st.pop();
tmp += p.se;
ans = max(ans, tmp * p.fi);
}
st.push(make_pair(mat[i][j], tmp + 1));
}
}
}
int tmp = 0;
while (!st.empty()) {
pa p = st.top(); st.pop();
tmp += p.se;
ans = max(ans, tmp * p.fi);
}
}
cout << ans << endl;
}
return 0;
}