您的位置:首页 > 其它

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

2014-07-31 10:10 411 查看
题目链接: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

,le
,ri
;
char a

;

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


//感觉还是很神奇的方法,我也是看别人好久才懂得,
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: