这道题虽然名字叫,但并不属于最大子矩阵和问题(或许这类问题可以看成只有1
和-INF
的最大子矩阵和的问题?2333不过没什么用)。还是直方图dp二维化。和上一篇基本一样,只不过这个题还得重复三遍。
这道题还是让你找一个单一字母构成的最大矩形,a
,b
,c
三种字母不能换成别的,而别的都能换成a
,b
,c
。所以只用考虑a
,b
,c
这三种,每考虑一种时,都让这种字母尽可能多,将其他不是这种字母的都看成另一种。
这道题还是需要注意字符输入,每行字符输入前先getchar()
掉换行符。
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <vector>
#include <cstring>
#include <string>
#include <queue>
using namespace std; // 无非分成三个问题,分别求解罢了
const int MAX = 1001;
int N, M;
int a[MAX][MAX]; // 能变换a的都变成a
int b[MAX][MAX];
int c[MAX][MAX];
int l[MAX]; // 下标代表列id
int r[MAX];
int ans;
void init()
{
for (int i = 1; i <= M; i++)
a[0][i] = b[0][i] = c[0][i] = 0;
ans = -1;
}
void solve(int x[MAX][MAX]) // 数组作为参数
{
int t;
for (int i = 1; i <= N; i++)
{
l[1] = 1;
for (int j = 2; j <= M; j++)
{
for (t = j; t - 1 >= 1 && x[i][t - 1] >= x[i][j]; t = l[t - 1]);
l[j] = t;
}
r[M] = M;
for (int j = M - 1; j >= 1; j--)
{
for (t = j; t + 1 <= M && x[i][t + 1] >= x[i][j]; t = r[t + 1]);
r[j] = t;
}
for (int j = 1; j <= M; j++)
ans = max(ans, x[i][j] * (r[j] - l[j] + 1));
}
}
int main()
{
char ch;
for (; ~scanf("%d%d", &N, &M);)
{
init();
for (int i = 1; i <= N; i++)
{
getchar(); // 吸收换行
for (int j = 1; j <= M; j++)
{
scanf("%c", &ch);
if (ch == 'a' || ch == 'w' || ch == 'y' || ch == 'z')
{
a[i][j] = a[i - 1][j] + 1;
}
else a[i][j] = 0;
if (ch == 'b' || ch == 'w' || ch == 'x' || ch == 'z')
{
b[i][j] = b[i - 1][j] + 1;
}
else b[i][j] = 0;
if (ch == 'c' || ch == 'x' || ch == 'y' || ch == 'z')
{
c[i][j] = c[i - 1][j] + 1;
}
else c[i][j] = 0;
}
}
solve(a);
solve(b);
solve(c);
printf("%d\n", ans);
}
return 0;
}