HDU 2870 Largest Submatrix

传送门

这道题虽然名字叫,但并不属于最大子矩阵和问题(或许这类问题可以看成只有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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值