hdu2870 Largest Submatrix--DP
发布日期:2021-10-03 20:31:56 浏览次数:7 分类:技术文章

本文共 2124 字,大约阅读时间需要 7 分钟。

原题链接:

一:原题内容

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

二:分析理解

这题用栈的方法竟然超时了,可能是stack的内存分配时间大了,只能改成比较麻烦的定义两个数组,left和right。。。。。。

这题是hdu1506-->

和hdu1505--> 的进阶版

三:AC代码

#include
#include
#include
using namespace std;char ch[1005][1005];int a[1005][1005];int b[1005][1005];int c[1005][1005];int l[1005];int r[1005];int n, m;int Solve(int x[1005][1005]){ int finalAns = -1; for (int i = 1; i <= n; i++) { l[1] = 1; r[m] = m; for (int j = 2; j <= m; j++) { l[j] = j; while (l[j] != 1 && x[i][j] <= x[i][l[j] - 1]) l[j] = l[l[j] - 1]; } for (int j = m - 1; j >= 1; j--) { r[j] = j; while (r[j] != m&&x[i][j] <= x[i][r[j] + 1]) r[j] = r[r[j] + 1]; } for (int j = 1; j <= m; j++) if (x[i][j] * (r[j] - l[j] + 1) > finalAns) finalAns = x[i][j] * (r[j] - l[j] + 1); } return finalAns;}int main(){ char ch; while (~scanf("%d%d", &n, &m)) { for (int i = 1; i <= n; i++) { getchar(); for (int j = 1; j <= m; j++) { scanf("%c", &ch); //a if (ch == 'a' || ch == 'w' || ch == 'y' || ch == 'z') a[i][j] = a[i - 1][j] + 1; else a[i][j] = 0; //b if (ch == 'b' || ch == 'w' || ch == 'x' || ch == 'z') b[i][j] = b[i - 1][j] + 1; else b[i][j] = 0; //c if (ch == 'c' || ch == 'x' || ch == 'y' || ch == 'z') c[i][j] = c[i - 1][j] + 1; else c[i][j] = 0; } } int a_ans = Solve(a); int b_ans = Solve(b); int c_ans = Solve(c); printf("%d\n", max(max(a_ans, b_ans), c_ans)); } return 0;}

转载地址:https://blog.csdn.net/LaoJiu_/article/details/50960727 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:hdu2830 Matrix Swapping II--DP
下一篇:hdu1505 City Game--DP/栈

发表评论

最新留言

哈哈,博客排版真的漂亮呢~
[***.90.31.176]2024年04月14日 19时54分33秒