HDU 2870(动态规划-最大子矩阵)

问题描述:

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

题目题意:题目给了一个矩阵,其中w,x,y,z可以去替换成a,b,c其中几个,问最大一个子矩阵(全部都是相同的字符)的面积。

题目分析:最大子矩阵的题意,但是替换比较麻烦,所以我们要先固定字符,我们假设我们求得最大子矩阵的字符都是'a',接下来就好办了,因为字符已经确定了,所以我们预处理dp数组的时候,只需要确定能否接在'a'后面就可以了,同理处理其他字符'b','c'


代码如下:

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

const int maxn=1e3+10;
char G[maxn][maxn];
int dp[maxn][maxn],area,n,m;

void get_area()//求最大子矩阵
{
    for (int i=1;i<=n;i++) {
        int left[maxn],right[maxn],t;
        left[1]=1;right[m]=m;
        for (int j=2;j<=m;j++) {
            t=j;
            while (t>1&&dp[i][j]<=dp[i][t-1])
                t=left[t-1];
            left[j]=t;
        }
        for (int j=m-1;j>=1;j--) {
            t=j;
            while (t<m&&dp[i][j]<=dp[i][t+1])
                t=right[t+1];
            right[j]=t;
        }
        for (int j=1;j<=m;j++) {
            area=max(area,(right[j]-left[j]+1)*dp[i][j]);
        }
    }
}
int main()
{
    while (scanf("%d%d",&n,&m)!=EOF) {
        for (int i=1;i<=n;i++) {
            scanf("%s",G[i]+1);
        }
        area=0;
        memset (dp,0,sizeof (dp));
        for (int j=1;j<=m;j++) {//处理'a'
            for (int i=n;i>=1;i--) {
                if (G[i][j]=='a'||G[i][j]=='w'||G[i][j]=='y'||G[i][j]=='z') dp[i][j]=dp[i+1][j]+1;
                else dp[i][j]=0;
            }
        }
        get_area();
        memset (dp,0,sizeof (dp));
        for (int j=1;j<=m;j++) {//处理'b'
            for (int i=n;i>=1;i--) {
                if (G[i][j]=='b'||G[i][j]=='w'||G[i][j]=='x'||G[i][j]=='z') dp[i][j]=dp[i+1][j]+1;
                else dp[i][j]=0;
            }
        }
        get_area();
        memset (dp,0,sizeof (dp));
        for (int j=1;j<=m;j++) {//处理'c'
            for (int i=n;i>=1;i--) {
                if (G[i][j]=='c'||G[i][j]=='x'||G[i][j]=='y'||G[i][j]=='z') dp[i][j]=dp[i+1][j]+1;
                else dp[i][j]=0;
            }
        }
        get_area();
        printf("%d\n",area);
    }
    return 0;
}












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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值