2 4 abcw wxyz
3
此题类似求最大全1子矩阵(参考这里:http://blog.csdn.net/gaotong2055/article/details/8960118),只不过这里要分别求abc的。
#include <stdio.h> char matrix[1001][1001]; int h[1005], left[1005], right[1001]; char ms[3][1001][1001]; // 转换后a,b,c 的矩阵 char c3[4] = "abc"; char chage(char a, char b) { //转换char a 到 char b if (a == 'w' && (b == 'a' || b == 'b')) return b; if (a == 'x' && (b == 'b' || b == 'c')) return b; if (a == 'y' && (b == 'c' || b == 'a')) return b; if (a == 'z') return b; return a; } int main() { int m, n; //freopen("in.txt", "r", stdin); while (scanf("%d %d", &m, &n) != EOF) { getchar(); //读取行尾 for (int i = 0; i < m; i++) { gets(matrix[i]); } int max = 0, tmp, tmax; //循环3次),求(a,b,c)最大子矩阵 for (int k = 0; k < 3; k++) { for (int i = 0; i < m; i++) { for (int j = 1; j <= n; j++) ms[k][i][j] = chage(matrix[i][j-1], c3[k]); //转换矩阵 } //求高 for (int j = 0; j <= n; j++) { h[j] = left[j] = right[j] = 0; } //求最大子面积 for (int i = 0; i < m; i++) { for (int j = 1; j <= n; j++) { if (ms[k][i][j] == c3[k]) h[j] += 1; else h[j] = 0; } h[0] = h[n+1] = -1; for (int j = 1; j <= n; j++) { tmp = j; while (h[j] <= h[tmp - 1] ) { tmp = left[tmp - 1] ; } if (tmp < 0) tmp = 0; left[j] = tmp; } // for (int j = 1; j <= n; j++) // printf("%d ", left[j]); // printf("\n"); for (int j = n ; j > 0; j--) { tmp = j; while (h[j] <= h[tmp + 1] ) { tmp = right[tmp + 1] ; } right[j] = tmp; tmax = (tmp - left[j] + 1) * h[j]; if(max < tmax){ max = tmax; } } // for (int j = 1; j <= n; j++) // printf("%d ", right[j]); // printf("\n"); } } printf("%d\n",max); } return 0; }