HDU 2870 最大子矩阵,联系1505, 1506

题目意思:w  x  y  z  可以转化,求最大子矩阵。

思路:枚举后转换为 hdu 1505 hdu 1506 的情况。

可以转化为a的有:w,y, z

可以转化为b的有:w,x, z

可以转换为c的有:x, y, z

所以

a, w,  y, z一组

b, w,  x, z一组

c, x,  y,  z一组

分别dp

#include <cstdlib>
#include <iostream>
using namespace std;
const int maxn=1005;
int h[maxn][maxn];
int l[maxn],r[maxn];
char str[maxn][maxn];
int n,m,i,j;
int ans;

int max(int a,int b)
{
	if(a>b)
		return a;
	else
		return b;
}
void DP()
{
    for(i=1;i<=n;++i)
    {
        for(j=1;j<=m;++j)
            l[j]=r[j]=j;
        h[i][0]=h[i][m+1]=-1;
        for(j=1;j<=m;++j)
            while(h[i][j]<=h[i][l[j]-1])
                l[j]=l[l[j]-1];
        for(j=m;j>=1;j--)
            while(h[i][j]<=h[i][r[j]+1])
                r[j]=r[r[j]+1];
        for(j=1;j<=m;++j)
            ans=max(ans,h[i][j]*(r[j]-l[j]+1));
    }
}
int main()
{
    memset(h[0],0,sizeof(h[0]));
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        ans=0;
        for(i=1;i<=n;++i)
            scanf("%s",str[i]+1);
        for(i=1;i<=n;++i)
            for(j=1;j<=m;++j)
                if(str[i][j]=='a'||str[i][j]=='w'||str[i][j]=='y'||str[i][j]=='z')
                    h[i][j]=h[i-1][j]+1;
                else
                    h[i][j]=0;
            DP();
            for(i=1;i<=n;++i)
            for(j=1;j<=m;++j)
                if(str[i][j]=='b'||str[i][j]=='w'||str[i][j]=='x'||str[i][j]=='z')
                    h[i][j]=h[i-1][j]+1;
                else
                    h[i][j]=0;
            DP();
            for(i=1;i<=n;++i)
            for(j=1;j<=m;++j)
                if(str[i][j]=='c'||str[i][j]=='x'||str[i][j]=='y'||str[i][j]=='z')
                    h[i][j]=h[i-1][j]+1;
                else
                    h[i][j]=0;
            DP();
            printf("%d\n",ans);
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值