/*http://acm.hdu.edu.cn/diy/diy_previewproblem.php?cid=19572&pid=1019
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 1 Accepted Submission(s) : 1
Font: Times New Roman | Verdana | Georgia
Font Size: ← →
Problem Description
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
Source
2009 Multi-University Training Contest 7 - Host by FZU
2009 Multi-University Training Contest 2 - Host by TJU
解析:求最大矩形,跟hdu1505,1506类似,
分别枚举a,b,c情况
*/
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
using namespace std;
const int maxn=1000+5;
double h[maxn];
int r[maxn],l[maxn];
int n,m;
char ch[maxn][maxn];
void init()//寻找大于等于h[i]的边界点
{
int i;
for(i=1;i<=m;i++)
{r[i]=l[i]=i;
}
h[0]=h[m+1]=-1;
for(i=1;i<=m;i++)
{
if(h[l[i]-1]>=h[i])//寻找左边界点
{
while(h[l[i]-1]>=h[i])
{l[i]=l[l[i]-1];
}
}
}
for(i=m;i>=1;i--)
{
if(h[r[i]+1]>=h[i])
{
while(h[r[i]+1]>=h[i])//寻找右边界点
{r[i]=r[r[i]+1];
}
}
}
}
int check(char c1,char c2 )
{
if(c2=='a')
return c1=='a'||c1=='w'||c1=='y'||c1=='z';
if(c2=='b')
return c1=='b'||c1=='w'||c1=='x'||c1=='z';
if(c2=='c')
return c1=='c'||c1=='y'||c1=='z'||c1=='x';
}
int main()
{
int i,j,k,g;
int t;
int ans;
while(scanf("%d%d",&n,&m)!=EOF)
{ans=0;
for(i=1;i<=n;i++)
scanf("%s",ch[i]);
for(g='a';g<='c';g++)//分别枚举a,b,c情况
{
memset(h,0,sizeof(h));
for(i=1;i<=n;i++)
{
for(j=0;j<m;j++)//以行为底进行其高度
{
if(check(ch[i][j],g)==1)
h[j+1]++;
else//如果不连续则将本行j+1列置为0
h[j+1]=0;
}
init();
for(k=1;k<=m;k++)
{//printf("r[%d]=%d, l[%d]=%d",k,r[k],k,l[k]);
t=h[k]*(r[k]-l[k]+1);
if(ans<t)
ans=t;
}
}
}
printf("%d\n",ans);
}
return 0;
}