和《算法进阶》上面的单调栈基本一样,因为都是矩形,我们对于当前这一行,假设矩形的底已经固定,就然后跑一遍书上的单调栈就可以了。
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
const int N = 1006;
int n, m, a[N][N], p, s[N], w[N];
int main() {
scanf("%d%d", &n, &m);
char ss[2];
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++) {
scanf("%s", ss);
if (ss[0] == 'F') a[i][j] = a[i-1][j] + 1;
else a[i][j] = 0;
}
int ans = 0;
for (int i = 1; i <= n; i++) {
a[i][m+1] = p = 0;
for (int j = 1; j <= m + 1; j++) {
if (a[i][j] > s[p]) {
s[++p] = a[i][j], w[p] = 1;
}
else {
int width = 0;
while (s[p] > a[i][j]) {
width += w[p];
ans = max(ans, width * s[p]);
p--;
}
s[++p] = a[i][j], w[p] = width + 1;
}
}
}
printf("%d\n", 3 * ans);
return 0;
}