HDU OJ Largest Submatrix

本文介绍了一种解决矩阵中字母转换后形成相同字母最大子矩阵的问题,通过将特定字母转换为'a'、'b'、'c',并分别求解关于每个字母的最大子矩阵,最终找出最大值作为结果。

Largest Submatrix

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 11   Accepted Submission(s) : 6
Font: Times New Roman | Verdana | Georgia
Font Size: ← →
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
分析:将w,x,y,z全部转化为a,b,c,然后分别求关于a,b,c,的最大子矩阵,tempa,tempb,tempc,三者的最大值即为结果。
代码如下:

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

int numa[1005],numb[1005],numc[1005];
int Maxa,Maxb,Maxc;
int Mina,Minb,Minc;
int result;
char ch;
int m,n;
int bit[30];
int counta,countb,countc,tempa,tempb,tempc;


int main()
{
    int i,j,k;
    bit[0]=4;
    bit[1]=2;
    bit[2]=1;
    bit[22]=6;
    bit[23]=3;
    bit[24]=5;
    bit[25]=7;
    while(cin>>m>>n)
    {
        getchar();
        memset(numa,0,sizeof(numa));
        memset(numb,0,sizeof(numb));
        memset(numc,0,sizeof(numc));
        tempa=tempb=tempc=0;
        for(i=0;i<m;i++)
        {
            Maxa=Maxb=Maxc=-1;
            Mina=Minb=Minc=0x7fffffff;
            for(j=0;j<n;j++)
            {
                ch=getchar();
                if((bit[ch-'a']&4)!=0)
                {
                    numa[j]++;
                }
                else
                {
                    numa[j]=0;
                }
                Maxa=max(numa[j],Maxa);
                Mina=min(Mina,numa[j]);
                if((bit[ch-'a']&2)!=0)
                {
                    numb[j]++;
                }
                else
                {
                    numb[j]=0;
                }
                Maxb=max(numb[j],Maxb);
                Minb=min(Minb,numb[j]);
                if((bit[ch-'a']&1)!=0)
                {
                    numc[j]++;
                }
                else
                {
                    numc[j]=0;
                }
                Maxc=max(numc[j],Maxc);
                Minc=min(Minc,numc[j]);
            }
            getchar();
            for(j=Mina;j<=Maxa;j++)
            {
                counta=0;
                for(k=0;k<n;k++)
                {
                    if(j<=numa[k])
                    {
                        counta+=j;
                        tempa=max(counta,tempa);
                    }
                    else
                    {
                        counta=0;
                    }
                }
            }
            for(j=Minb;j<=Maxb;j++)
            {
                countb=0;
                for(k=0;k<n;k++)
                {
                    if(j<=numb[k])
                    {
                        countb+=j;
                        tempb=max(countb,tempb);
                    }
                    else
                    {
                        countb=0;
                    }
                }
            }
            for(j=Minc;j<=Maxc;j++)
            {
                countc=0;
                for(k=0;k<n;k++)
                {
                    if(j<=numc[k])
                    {
                        countc+=j;
                        tempc=max(countc,tempc);
                    }
                    else
                    {
                        countc=0;
                    }
                }
            }
        }
        result=max(tempa,max(tempb,tempc));
        cout<<result<<endl;
    }
    return 0;
}

转载于:https://www.cnblogs.com/lzmfywz/articles/2358221.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值