1.ans=max (the largest matrix of all a, all the largest matrices of B, all the largest matrices of c)
2. Code:
#include <cstdio> #include <cstring> #include <algorithm>using namespace Std;const int inf=1<< 30;char mat[1005][1005];int a[1005][1005];int b[1005][1005];int c[1005][1005];int La[1005],Ra[1005];int Lb[1005],Rb[ 1005];int Lc[1005],rc[1005];int Main () {int m,n; int ans; memset (A,0,sizeof (a)); Memset (b,0,sizeof (b)); Memset (C,0,sizeof (c)); while (scanf ("%d%d", &m,&n) ==2) {for (int i=1;i<=m;i++) {scanf ("%s", mat[i]+1); } Ans=-inf; for (int i=1;i<=m;i++) {for (int j=1;j<=n;j++) {if (mat[i][j]== ' A ' | | mat[i][j]== ' W ' | | mat[i][j]== ' Y ' | | mat[i][j]== ' z ') a[i][j]=1; else a[i][j]=0; if (mat[i][j]== ' B ' | | mat[i][j]== ' W ' | | mat[i][j]== ' x ' | | mat[i][j]== ' z ') b[i][j]=1; else b[i][j]=0; if (mat[i][j]== ' C ' | | mat[i][j]== ' x ' | | mat[i][j]== ' Y ' | | MatI [j]== ' z ') c[i][j]=1; else c[i][j]=0; }} for (int i=1;i<=m;i++) {for (int j=1;j<=n;j++) {if (a[i ][j]==1) A[i][j]+=a[i-1][j]; if (b[i][j]==1) b[i][j]+=b[i-1][j]; if (c[i][j]==1) c[i][j]+=c[i-1][j]; } for (int j=1;j<=n;j++) {la[j]=j; while (La[j]>1&&a[i][la[j]-1]>=a[i][j]) la[j]=la[la[j]-1]; } for (int j=1;j<=n;j++) {lb[j]=j; while (Lb[j]>1&&b[i][lb[j]-1]>=b[i][j]) lb[j]=lb[lb[j]-1]; } for (int j=1;j<=n;j++) {lc[j]=j; while (Lc[j]>1&&c[i][lc[j]-1]>=c[i][j]) lc[j]=lc[lc[j]-1]; } for (int j=n;j>=1;j--) {ra[j]=j; while (Ra[j]<n&&a[i][ra[j]+1]>=a[i][j]) ra[j]=ra[ra[j]+1]; } for (int j=n;j>=1;j--) {rb[j]=j; while (Rb[j]<n&&b[i][rb[j]+1]>=b[i][j]) rb[j]=rb[rb[j]+1]; } for (int j=n;j>=1;j--) {rc[j]=j; while (Rc[j]<n&&c[i][rc[j]+1]>=c[i][j]) rc[j]=rc[rc[j]+1]; } for (int j=1;j<=n;j++) {Ans=max (ans,a[i][j]* (ra[j]-la[j]+1)); } for (int j=1;j<=n;j++) {Ans=max (ans,b[i][j]* (rb[j]-lb[j]+1)); } for (int j=1;j<=n;j++) {Ans=max (ans,c[i][j]* (rc[j]-lc[j]+1)); }} printf ("%d\n", ans); } return 0;}
HDU 2870 Largest Submatrix (enhanced version of HDU 1505)