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/* m,n<1000 由题可知,w->a或b x->b或c,y->a或c,z->a,b或c 其实就是转换为这3种后分别求面积最大值,再取其中的最大值 即是正解。 可变成a的有:w y z a 可变成b的有:w x z b 可变成c的有:x y z c */ #include <iostream> #include <cstdio> using namespace std; #define maxn 1008 int h[4][maxn][maxn];//h1是 struct REC { int l,r,h; }rec[1008]; inline int max(int a,int b) { return a>b?a:b; } int main() { int r,c; while(scanf("%d%d",&r,&c)==2) { getchar(); for(int i=1;i<=r;i++) { for(int j=1;j<=c;j++) { char c=getchar(); if(c=='a'||c=='w'||c=='y'||c=='z') { h[1][i][j]=h[1][i-1][j]+1; } else h[1][i][j]=0; if(c=='w'||c=='x'||c=='z'||c=='b') { h[2][i][j]=h[2][i-1][j]+1; } else h[2][i][j]=0; if(c=='x'||c=='y'||c=='z'||c=='c') { h[3][i][j]=h[3][i-1][j]+1; } else h[3][i][j]=0; } getchar(); } int maxs=0;//最大面积 for(int i=1;i<=3;i++)//a b c 得分别找一次最大面积 { for(int j=1;j<=r;j++)//找最大面积得遍历每一行 { for(int k=1;k<=c;k++) { rec[k].h=h[i][j][k]; } for(int k=1;k<=c;k++)//接下来是给每个矩形找到左边第一个比他矮的矩形 { if(k==1) { rec[k].l=0; rec[0].h=-1; continue; } if(rec[k].h>rec[k-1].h) { rec[k].l=k-1; continue; } if(rec[k].h==rec[k-1].h) { rec[k].l=rec[k-1].l; continue; } if(rec[k].h<rec[k-1].h) { int oo=k-1; while(rec[oo].h>=rec[k].h) { oo=rec[oo].l; } rec[k].l=oo; } }//接下来是给每个矩形找到右边第一个比他矮的矩形,得从右往左 for(int k=c;k>=1;k--) { if(k==c) { rec[k].r=c+1; rec[c+1].h=-1; continue; } if(rec[k].h>rec[k+1].h) { rec[k].r=k+1; continue; } if(rec[k].h==rec[k+1].h) { rec[k].r=rec[k+1].r; continue; } if(rec[k].h<rec[k+1].h) { int oo=k+1; while(rec[oo].h>=rec[k].h) { oo=rec[oo].r; } rec[k].r=oo; } }//每个矩形左边第一个比他矮,右边第一个比他矮的矩形我们都已经找到,也就是说矩形面积可以求了 for(int k=1;k<=c;k++) { maxs=max(maxs,rec[k].h*(rec[k].r-rec[k].l-1)); } } } printf("%d\n",maxs); } return 0; }