Largest Submatrix-最大子矩陣-HDU-2870

來源:互聯網
上載者:User
Largest Submatrix

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 970    Accepted Submission(s): 470


Problem DescriptionNow 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? 


InputThe 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. 


OutputFor 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
 

此題類似求最大全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 bif (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;elseh[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;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.