hdu 2870 Largest Submatrix

 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

这道题直接把可替换的分别全换为a,b,c然后计算分别由a,b,c最大子矩阵的面积,此处求法可参照http://blog.csdn.net/ffgcc/article/details/78491268,这里我们多次对每一行求解即可.

#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
const int N=1e3+10;
char app[N][N];
int arr[N][N];
int dp[N][N];
int num[N];
int l[N];
int r[N];
int n,m;
int fi()
{
    memset(dp,0,sizeof dp);
    int mx=0;
    for(int i=1;i<=n;i++)
    {
        for(int j=0;j<m;j++)
        {
            if(arr[i][j]==1)
                dp[i][j]=dp[i-1][j]+1;
            else
                dp[i][j]=0;
            num[j]=dp[i][j];
        }
        l[0]=0;r[m-1]=m-1;

        for(int j=1;j<m;j++)
        {
            int tt=j;
            while(tt>0&&num[j]<=num[tt-1]) tt=l[tt-1];
            l[j]=tt;
        }
        for(int j=m-1;j>=0;j--)
        {
            int tt=j;
            while(tt<m-1&&num[j]<=num[tt+1])tt=r[tt+1];
            r[j]=tt;
        }
        for(int j=0;j<m;j++)
            mx=max((r[j]-l[j]+1)*num[j],mx);
    }
    return mx;

}
int main()
{
    ios::sync_with_stdio(false);
    while(cin>>n>>m&&n)
    {
        for(int i=1;i<=n;i++)
            cin>>app[i];
        memset(arr,0,sizeof arr);
        int mx=0;
        for(int i=1;i<=n;i++)
        {
            for(int j=0;j<m;j++)
                if(app[i][j]=='w'||app[i][j]=='y'||app[i][j]=='z'||app[i][j]=='a')
                    arr[i][j]=1;
        }
        //cout<<fi()<<endl;
        mx=max(mx,fi());
        memset(arr,0,sizeof arr);
        for(int i=1;i<=n;i++)
        {
            for(int j=0;j<m;j++)
                if(app[i][j]=='w'||app[i][j]=='x'||app[i][j]=='z'||app[i][j]=='b')
                    arr[i][j]=1;
        }
        mx=max(mx,fi());
                //cout<<fi()<<endl;
        memset(arr,0,sizeof arr);
        for(int i=1;i<=n;i++)
        {
            for(int j=0;j<m;j++)
                if(app[i][j]=='x'||app[i][j]=='y'||app[i][j]=='z'||app[i][j]=='c')
                arr[i][j]=1;
        }
                //cout<<fi()<<endl;
        mx=max(mx,fi());
        cout<<mx<<endl;
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值