HDU-2870最大矩形问题(四)

上原题
Largest Submatrix
Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

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



把所有能转化的全部依次转化为'a','b','c',用解第二题的方法对每种情况用一次就行了。。
//Li Wenjun
//emai:1542113545@qq.com
/*
「“04.24,サクラと东京スカイツリーに行った。そこは世界で一番暖かいところだ。”
“04.26,サクラと明治神宫に行った。そこで结婚式お挙げる人がいた。”
“04.25,サクラとデイズニーに行った。お化け屋敷が怖かったけど、サクラがいたから、全然怖くわなかった。”
“サクラのことが大好き。”」
*/
#include<stdio.h>
#include<string.h>
#include<math.h>
#include <iostream>
#include <algorithm>
using namespace std;
int dp[1010][1010];
char a[1010][1010];
int r[1005],l[1005];
int m,n,maxs;
int FindMax(int H)
{
    int temp,i,j;
    for(i=1;i<=m;i++)
    {
        r[i]=l[i]=i;
        while(dp[H][l[i]-1]>=dp[H][i])
            l[i]=l[l[i]-1];
    }
    for(j=m;j>=1;j--)
    {
        while(dp[H][r[j]+1]>=dp[H][j])
            r[j]=r[r[j]+1];
          temp=dp[H][j]*(r[j]-l[j]+1);
        if(maxs<temp)
            maxs=temp;
    }
    return 0;
}
int dps(char x1,char x2,char x3,char x4)
{
  /*  cout<<x1<<' ';
    cout<<x2<<' ';
    cout<<x3<<' ';
    cout<<x4<<' ';
    cout<<endl;     */
    memset(dp,0,sizeof(dp));
    memset(r,0,sizeof(r));
    memset(l,0,sizeof(l));
    dp[n+1][0]=-2;
    for(int i=1;i<=n;i++)
        {
            dp[i][0]=-1;
            for(int j=1;j<=m;j++)
            {
            //    cout<<a[i][j]<<' ';
                if(a[i][j]==x1||a[i][j]==x2||a[i][j]==x3||a[i][j]==x4)
                {
                    dp[i][j]=dp[i-1][j]+1;
                }
                else
                {
                    dp[i][0]=-2;
                    dp[i][j]=0;
                }
            }
            dp[i][m+1]=-1;
          //  cout<<endl;
        }
   /*     for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=m;j++)
            {
                cout<<dp[i][j]<<' ';
            }
            cout<<endl;
        }
        */
        for(int i=1;i<=n;i++)
        {
            if(dp[i+1][0]==-2)
             FindMax(i);
        }
   //     cout<<maxs<<endl;
    return 0;
}
int main()
{
    cin.tie(0);
    cin.sync_with_stdio(false);
  //  freopen("in.txt","r",stdin);
    while(cin>>n>>m)
    {
        memset(dp,0,sizeof(dp));
        memset(a,0,sizeof(a));
        for(int i=1;i<=n;i++)
        {
            cin>>a[i]+1;
        }
        maxs=0;

        dps('a','w','y','z');
        dps('b','w','x','z');
        dps('c','x','y','z');
        cout<<maxs<<endl;
    }
}







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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值