题目

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

题解

可以看出是>最大矩形问题<
由于 wxyz 都可以转换成 abc
可以得知,最后结果必定在转换成 abc 中选择

分别转换成 abc ,分别求取最大矩形,最大的那个就是答案

代码

/*
By:OhYee
Github:OhYee
HomePage:http://www.oyohyee.com
Email:oyohyee@oyohyee.com

かしこいかわいい?
エリーチカ!
要写出来Хорошо的代码哦~
*/

#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <string>
#include <iostream>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <map>
#include <set>
using namespace std;

const int maxn = 1005;

char Mapa[maxn][maxn];
char Mapb[maxn][maxn];
char Mapc[maxn][maxn];

int H[maxn][maxn];
int Left[maxn][maxn];
int Right[maxn][maxn];
int MaxMatrix(char Matrix[maxn][maxn],int n,int m,char target) {
    memset(H,0,sizeof(H));
    memset(Left,0,sizeof(Left));
    memset(Right,0,sizeof(Right));

    for(int i = 1;i <= n;i++)
        for(int j = 1;j <= m;j++) {
            if(Matrix[i][j] == target) {
                if(Matrix[i - 1][j])
                    H[i][j] = H[i - 1][j] + 1;
                else
                    H[i][j] = 1;
            }
        }
    for(int i = 1;i <= n;i++)
        for(int j = 1;j <= m;j++) {
            if(Matrix[i][j] == target) {
                int t = j;
                while(t >= 1 && H[i][j] <= H[i][t - 1])
                    t = Left[i][t - 1];
                Left[i][j] = t;
            }
        }
    for(int i = 1;i <= n;i++)
        for(int j = m;j >= 1;j--) {
            if(Matrix[i][j] == target) {
                int t = j;
                while(t <= m && H[i][j] <= H[i][t + 1])
                    t = Right[i][t + 1];
                Right[i][j] = t;
            }
        }
    int Max = 0;
    for(int i = 1;i <= n;i++)
        for(int j = 1;j <= m;j++) {
            if(Matrix[i][j] == target) {
                int S = H[i][j] * (Right[i][j] - Left[i][j] + 1);
                Max = max(Max,S);
            }
        }
    return Max;
}

bool  Do() {
    int n,m;
    if(scanf("%d%d",&n,&m) == EOF)
        return false;
    int a = 0,b = 0,c = 0;
    for(int i = 1;i <= n;i++)
        for(int j = 1;j <= m;j++) {
            char t;
            scanf("\n%c",&t);
            Mapa[i][j] = (t == 'w' || t == 'y' || t == 'z') ? 'a' : t;
            Mapb[i][j] = (t == 'w' || t == 'x' || t == 'z') ? 'b' : t;
            Mapc[i][j] = (t == 'x' || t == 'y' || t == 'z') ? 'c' : t;
        }

    int ans = 0;
    ans = max(ans,MaxMatrix(Mapa,n,m,'a'));
    ans = max(ans,MaxMatrix(Mapb,n,m,'b'));
    ans = max(ans,MaxMatrix(Mapc,n,m,'c'));

    printf("%d\n",ans);
    return true;
}

int main() {
    while(Do());
    return 0;
}