HDU--2870--Largest Submatrix

来源:互联网 发布:压缩文件数据错误 编辑:程序博客网 时间:2024/04/27 18:34

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 4abcwwxyz

Sample Output
3
/*m,n<1000由题可知,w->a或bx->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 1008int 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;}

原创粉丝点击