码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2015-05-12 09:30:36      阅读:134      评论:0      收藏:0      [点我收藏+]

标签:dp

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;
}


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

标签:dp

原文地址:http://blog.csdn.net/xky1306102chenhong/article/details/45651141

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!