hdu 2870 Largest Submatrix

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

 

题目描述:

 

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

 

 

 

题目大意:

给你一个m行n列的字符矩阵,其中w可以被a和b代替,x可以被b和c代替,y可以被a和c代替,z可以被a、b、c代替,叫你在这个矩阵中找出一个面积最大的由相同字母组成的矩阵的面积

 

题目分析:

由于只有三种字母,a、b和c,那么我们可以把这三种情况都算出来求最大值,依次把w、x、y、z当中能变成a的都换成a,来求得全a矩阵的最大面积,同理,求出b和c,三者的较大者就是我们要求的最大矩阵面积

 

AC代码:

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

int a[1005][1005],b[1005][1005],c[1005][1005];
int l[1005],r[1005];

int m,n;
int Max,tmp;

char ch;

int main()
{
    while(scanf("%d%d",&m,&n)!=EOF)
    {
        memset(a,0,sizeof(a));
        memset(b,0,sizeof(b));
        memset(c,0,sizeof(c));
        Max=-0xfffffff;
        for(int i=1;i<=m;i++)
        {
            getchar();
            for(int j=1;j<=n;j++)
            {
                scanf("%c",&ch);
                if(ch=='a'||ch=='w'||ch=='y'||ch=='z')
                    a[i][j]++;
                if(ch=='b'||ch=='w'||ch=='x'||ch=='z')
                    b[i][j]++;
                if(ch=='c'||ch=='x'||ch=='y'||ch=='z')
                    c[i][j]++;
                if(a[i][j])
                    a[i][j]+=a[i-1][j];
                if(b[i][j])
                    b[i][j]+=b[i-1][j];
                if(c[i][j])
                    c[i][j]+=c[i-1][j];
            }
            memset(l,0,sizeof(l));
            memset(r,0,sizeof(r));
            l[1]=1;
            r[n]=n;
            for(int j=2;j<=n;j++)
            {
                tmp=j;
                if(a[i][j]==0)
                    continue;
                while(tmp>1&&a[i][j]<=a[i][tmp-1])
                    tmp=l[tmp-1];
                l[j]=tmp;
            }
            for(int j=n-1;j>=1;j--)
            {
                tmp=j;
                if(a[i][j]==0)
                    continue;
                while(tmp<n&&a[i][j]<=a[i][tmp+1])
                    tmp=r[tmp+1];
                r[j]=tmp;
            }
            for(int j=1;j<=n;j++)
            {
                Max=max(Max,a[i][j]*(r[j]-l[j]+1));
            }

            memset(l,0,sizeof(l));
            memset(r,0,sizeof(r));
            l[1]=1;
            r[n]=n;
            for(int j=2;j<=n;j++)
            {
                tmp=j;
                if(b[i][j]==0)
                    continue;
                while(tmp>1&&b[i][j]<=b[i][tmp-1])
                    tmp=l[tmp-1];
                l[j]=tmp;
            }
            for(int j=n-1;j>=1;j--)
            {
                tmp=j;
                if(b[i][j]==0)
                    continue;
                while(tmp<n&&b[i][j]<=b[i][tmp+1])
                    tmp=r[tmp+1];
                r[j]=tmp;
            }
            for(int j=1;j<=n;j++)
            {
                Max=max(Max,b[i][j]*(r[j]-l[j]+1));
            }

            memset(l,0,sizeof(l));
            memset(r,0,sizeof(r));
            l[1]=1;
            r[n]=n;
            for(int j=2;j<=n;j++)
            {
                tmp=j;
                if(c[i][j]==0)
                    continue;
                while(tmp>1&&c[i][j]<=c[i][tmp-1])
                    tmp=l[tmp-1];
                l[j]=tmp;
            }
            for(int j=n-1;j>=1;j--)
            {
                tmp=j;
                if(c[i][j]==0)
                    continue;
                while(tmp<n&&c[i][j]<=c[i][tmp+1])
                    tmp=r[tmp+1];
                r[j]=tmp;
            }
            for(int j=1;j<=n;j++)
            {
                Max=max(Max,c[i][j]*(r[j]-l[j]+1));
            }
        }
        cout<<Max<<endl;
    }
    return 0;
}

 

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

 

题目描述:

 

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

 

 

题目大意:

给你一个m行n列的字符矩阵,其中w可以被a和b代替,x可以被b和c代替,y可以被a和c代替,z可以被a、b、c代替,叫你在这个矩阵中找出一个面积最大的由相同字母组成的矩阵的面积

 

题目分析:

由于只有三种字母,a、b和c,那么我们可以把这三种情况都算出来求最大值,依次把w、x、y、z当中能变成a的都换成a,来求得全a矩阵的最大面积,同理,求出b和c,三者的较大者就是我们要求的最大矩阵面积

 

AC代码:

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值