Largest Submatrix
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 694 Accepted Submission(s): 331
#include<stdio.h>
#include<string.h>
#define N 1005
#define MX(a,b) (a>b?a:b)
int num[N];
char s[N][N];
int n,m;
bool check(char c,char ss)
{
if(c=='a')
return ss=='a'||ss=='w'||ss=='y'||ss=='z';
if(c=='b')
return ss=='b'||ss=='w'||ss=='x'||ss=='z';
if(c=='c')
return ss=='c'||ss=='x'||ss=='y'||ss=='z';
}
int main()
{
while(scanf("%d%d",&n,&m)!=-1)
{
for(int i=0;i<n;i++)
scanf("%s",s[i]);
int max=0;
for(char c='a';c<='c';c++)
{
memset(num,0,sizeof(num));
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
if(check(c,s[i][j]))
num[j]++;
else
num[j]=0;
}
for(int j=0;j<m;j++)
{
int cnt=1;
if(!num[j])
continue;
//枚举每个高度,然后以它做基准,向左向右扩张,扩张条件是h[i-k]>=h[k]、h[i+k]>=h[k]。记录扩张的个数即可。
for(int k=1;j-k>=0&&num[j-k]>=num[j];k++)
cnt++;
for(int k=1;j+k<m&&num[j+k]>=num[j];k++ )
cnt++;
max=MX(max,cnt*num[j]);
}
}
}
printf("%d\n",max);
}
return 0;
}