HDU 2870 (最大0 1 矩阵面积)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2870


这个题是  1505,1506的变形,建议先去看看1506,那是基础,解释都在代码中,感觉关键还是掌握下面我强调的难点处(对我还说是难点)



Problem Description
Now here is a matrix with letter 'a','b','c','w','x','y','z' and you can change 'w' to 'a' or 'b', change 'x' to 'b' or 'c', change 'y' to 'a' or 'c', and change 'z' to 'a', 'b' or 'c'. After you changed it, what's the largest submatrix with the same letters you can make?
 

Input
The input contains multiple test cases. Each test case begins with m and n (1 ≤ m, n ≤ 1000) on line. Then come the elements of a matrix in row-major order on m lines each with n letters. The input ends once EOF is met.
 

Output
For each test case, output one line containing the number of elements of the largest submatrix of all same letters.
 

Sample Input
  
  
2 4 abcw wxyz
 

Sample Output
  
  
3
 

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

#define N 1005

int h[N][N],le[N],ri[N];
char a[N][N];

int main()
{
    int n,m,i,j;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        getchar();

        char c;
        int ans=0,t; 

        for(i=1;i<=n;i++)
        {
            for(j=1;j<=m;j++)
               scanf("%c",&a[i][j]);
            getchar();
        }

        for(t=1;t<=3;t++)      //分三种情况,a,(b,c),当可以把转化a的字符全部转化成a(不是真的变成a),
                                //计算高度,求得最大矩形面积(方法详解在上篇 1505)
        {                      //然后比较这三种情况的最大值
            memset(h,0,sizeof(h));

            if(t==1)
            {
                for(i=1;i<=n;i++)
                    for(j=1;j<=m;j++)
                      if(a[i][j]=='a'||a[i][j]=='w'||a[i][j]=='y'||a[i][j]=='z')
                          h[i][j]=h[i-1][j]+1;
                      else
                          h[i][j]=0;
            }
            else
                if(t==2)
            {
                 for(i=1;i<=n;i++)
                    for(j=1;j<=m;j++)
                      if(a[i][j]=='b'||a[i][j]=='w'||a[i][j]=='x'||a[i][j]=='z')
                          h[i][j]=h[i-1][j]+1;
                      else
                          h[i][j]=0;
            }
            else
            {
                for(i=1;i<=n;i++)
                    for(j=1;j<=m;j++)
                      if(a[i][j]=='c'||a[i][j]=='x'||a[i][j]=='y'||a[i][j]=='z')
                          h[i][j]=h[i-1][j]+1;
                      else
                          h[i][j]=0;
            }

            for(i=1;i<=n;i++)    //以上高度确定,下面是算面积
            {

                for(j=1;j<=m;j++)
                {
                    le[j]=j;
                    while(le[j]>1&&h[i][le[j]-1]>=h[i][j])  //此处是难点,不懂看 上篇1505
                        le[j]=le[le[j]-1];
                }

                for(j=m;j>=1;j--)
                {
                    ri[j]=j;
                    while(ri[j]<m&&h[i][ri[j]+1]>=h[i][j])   //此处是难点,不懂看 上篇1505
                        ri[j]=ri[ri[j]+1];
                }

                for(j==1;j<=m;j++)
                  ans=max(ans,(ri[j]-le[j]+1)*h[i][j]);
            }
      }
        printf("%d\n",ans);
    }
    return 0;
}

//感觉还是很神奇的方法,我也是看别人好久才懂得,


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值