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 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
哈哈,博客排版真的漂亮呢~
[***.90.31.176]2024年04月14日 19时54分33秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
spring bean的生命周期
2021-06-29
计算机网络子网划分详解
2021-06-29
计算机网络生成树算法STP简介
2021-06-29
三郎数据结构算法学习笔记:哈希表查找
2021-06-29
三郎数据结构算法学习笔记:二叉树的三种遍历及增删改查
2021-06-29
三郎数据结构算法学习笔记:顺序存储二叉树
2021-06-29
三郎数据结构算法学习笔记:线索二叉树
2021-06-29
解释Spring支持的几种bean的作用域。
2021-06-29
Spring框架中都用到了哪些设计模式?
2019-04-26
什么是MyBatis?
2019-04-26
MyBatis的优点和缺点
2019-04-26
#{}和${}的区别是什么?
2019-04-26
LeetCode题解(1508):子数组和排序后的区间和(Python)
2019-04-26
LeetCode题解(1509):三次操作后最大值与最小值的最小差(Python)
2019-04-26
LeetCode题解(1510):石子游戏IV(Python)
2019-04-26
LeetCode题解(1497):检查数组对是否可以被k整除(Python)
2019-04-26
LeetCode题解(0781):森林中的兔子(Python)
2019-04-26
LeetCode题解(0930):和相同的二元子数组(Python)
2019-04-26
LeetCode题解(0939):最小面积矩形(Python)
2019-04-26
LeetCode题解(0954):二倍数对数组(Python)
2019-04-26