hdu 2870 Largest Submatrix(hdu 1505的加强版)

1.ans=max(全为a的最大矩阵,全为b的最大矩阵,全为c的最大矩阵)

2.代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;

const int INF=1<<30;
char mat[1005][1005];
int a[1005][1005];
int b[1005][1005];
int c[1005][1005];
int La[1005],Ra[1005];
int Lb[1005],Rb[1005];
int Lc[1005],Rc[1005];

int main()
{
    int m,n;
    int ans;
    memset(a,0,sizeof(a));
    memset(b,0,sizeof(b));
    memset(c,0,sizeof(c));
    while(scanf("%d%d",&m,&n)==2)
    {
        for(int i=1;i<=m;i++)
        {
            scanf("%s",mat[i]+1);
        }
        ans=-INF;
        for(int i=1;i<=m;i++)
        {
            for(int j=1;j<=n;j++)
            {
                if(mat[i][j]=='a'||mat[i][j]=='w'||mat[i][j]=='y'||mat[i][j]=='z')
                    a[i][j]=1;
                else
                    a[i][j]=0;
                if(mat[i][j]=='b'||mat[i][j]=='w'||mat[i][j]=='x'||mat[i][j]=='z')
                    b[i][j]=1;
                else
                    b[i][j]=0;
                if(mat[i][j]=='c'||mat[i][j]=='x'||mat[i][j]=='y'||mat[i][j]=='z')
                    c[i][j]=1;
                else
                    c[i][j]=0;
            }
        }
        for(int i=1;i<=m;i++)
        {
            for(int j=1;j<=n;j++)
            {
                if(a[i][j]==1)
                    a[i][j]+=a[i-1][j];
                if(b[i][j]==1)
                    b[i][j]+=b[i-1][j];
                if(c[i][j]==1)
                    c[i][j]+=c[i-1][j];
            }
            for(int j=1;j<=n;j++)
            {
                La[j]=j;
                while(La[j]>1&&a[i][La[j]-1]>=a[i][j])
                    La[j]=La[La[j]-1];
            }
            for(int j=1;j<=n;j++)
            {
                Lb[j]=j;
                while(Lb[j]>1&&b[i][Lb[j]-1]>=b[i][j])
                    Lb[j]=Lb[Lb[j]-1];
            }
            for(int j=1;j<=n;j++)
            {
                Lc[j]=j;
                while(Lc[j]>1&&c[i][Lc[j]-1]>=c[i][j])
                    Lc[j]=Lc[Lc[j]-1];
            }
            for(int j=n;j>=1;j--)
            {
                Ra[j]=j;
                while(Ra[j]<n&&a[i][Ra[j]+1]>=a[i][j])
                    Ra[j]=Ra[Ra[j]+1];
            }
            for(int j=n;j>=1;j--)
            {
                Rb[j]=j;
                while(Rb[j]<n&&b[i][Rb[j]+1]>=b[i][j])
                    Rb[j]=Rb[Rb[j]+1];
            }
            for(int j=n;j>=1;j--)
            {
                Rc[j]=j;
                while(Rc[j]<n&&c[i][Rc[j]+1]>=c[i][j])
                    Rc[j]=Rc[Rc[j]+1];
            }
            for(int j=1;j<=n;j++)
            {
                ans=max(ans,a[i][j]*(Ra[j]-La[j]+1));
            }
            for(int j=1;j<=n;j++)
            {
                ans=max(ans,b[i][j]*(Rb[j]-Lb[j]+1));
            }
            for(int j=1;j<=n;j++)
            {
                ans=max(ans,c[i][j]*(Rc[j]-Lc[j]+1));
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值