HDU-2870 Largest Submatrix (线性dp 最大01矩阵)(2009 Multi-University Training Contest 7 )
来源:互联网 发布:小米手机清空用户数据 编辑:程序博客网 时间:2024/06/05 08:03
Largest Submatrix
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2656 Accepted Submission(s): 1298
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
#include <bits/stdc++.h>using namespace std;int R, C, a[1001][1001], h[1001][1001], l[1001], r[1001];char s[1001][1001];int solve(int kind){for(int i = 1; i <= R; ++i){for(int j = 1; j <= C; ++j){if(kind == 0){if(s[i][j] == 'a' || s[i][j] == 'w' || s[i][j] == 'y' || s[i][j] == 'z') a[i][j] = 1;else a[i][j] = 0;}if(kind == 1){if(s[i][j] == 'b' || s[i][j] == 'w' || s[i][j] == 'x' || s[i][j] == 'z') a[i][j] = 1;else a[i][j] = 0;}if(kind == 2){if(s[i][j] == 'c' || s[i][j] == 'x' || s[i][j] == 'y' || s[i][j] == 'z') a[i][j] = 1;else a[i][j] = 0;}}}for(int i = 1; i <= C; ++i){h[0][i] = 0;}int ans = 0;for(int i = 1; i <= R; ++i){for(int j = 1; j <= C; ++j){if(a[i][j] == 1){h[i][j] = h[i - 1][j] + 1;}else{h[i][j] = 0;}}}for(int i = 1; i <= R; ++i){for(int j = 1; j <= C; ++j){l[j] = r[j] = j;}h[i][0] = -1;for(int j = 1; j <= C; ++j){while(h[i][j] <= h[i][l[j] - 1]){l[j] = l[l[j] - 1];}}h[i][C + 1] = -1;for(int j = C - 1; j >= 1; --j){while(h[i][j] <= h[i][r[j] + 1]){r[j] = r[r[j] + 1];}}for(int j = 1; j <= C; ++j){ans = max(ans, (r[j] - l[j] + 1) * h[i][j]);}}return ans;}int main(){while(scanf("%d %d", &R, &C) != EOF){for(int i = 1; i <= R; ++i){for(int j = 1; j <= C; ++j){s[i][j] = getchar();while(s[i][j] < 'a' || s[i][j] > 'z'){s[i][j] = getchar();}}}int ans = 0;ans = max(ans, solve(0));ans = max(ans, solve(1));ans = max(ans, solve(2));printf("%d\n", ans);}}/*题意:1000*1000的字符矩阵,字符只包含a,b,c,w,x,y,z,w,x,y,z可以做一些变化变成a或b或c,问最大只包含一种字符的矩阵面积。思路:首先有一个观察,我们不用考虑包含w,x,y,z的矩阵情况,将他们转化成a或b或c处理即可。如果直接考虑怎么处理这些变化然后计算矩阵面积比较麻烦,我们不妨分开处理,分别计算只包含a的情况,然后b,c,最后取最大值即可。计算只包含a的情况那么就可以转换成01矩阵中最大1矩阵的问题。可以参考:http://blog.csdn.net/hmc0411/article/details/78540500;http://blog.csdn.net/hmc0411/article/details/78539159;*/
阅读全文
0 0
- HDU-2870 Largest Submatrix (线性dp 最大01矩阵)(2009 Multi-University Training Contest 7 )
- hdu 5791 2016 Multi-University Training Contest 5(dp)
- HDU 2870 Largest Submatrix DP求最大子矩阵
- HDU 2870 Largest Submatrix(dp最大子矩阵和)
- HDU 2870 Largest Submatrix (最大子矩阵)
- Largest Submatrix-最大子矩阵-HDU-2870
- hdu 2870 Largest Submatrix 最大子矩阵
- hdu 2870 Largest Submatrix 最大子矩阵
- hdu 4328 Cut the cake(最大子矩阵)2012 Multi-University Training Contest 3
- hdu 2870 Largest Submatrix(最大子矩阵变式)
- HDU 2870 Largest Submatrix(最大子矩阵面积)
- hdu 6170 dp 2017 Multi-University Training Contest
- HDU/HDOJ 2855 2009 Multi-University Training Contest 5 - Host by NUDT 矩阵二分幂
- (HDU 5816)2016 Multi-University Training Contest 7 Hearthstone (状压DP、搜索)
- hdu 4340 Capturing a country(树形DP) 2012 Multi-University Training Contest 5
- hdu 5726 2016 Multi-University Training Contest 1(二分+dp)
- hdu 5763 Another Meaning(2016 Multi-University Training Contest 4——dp+kmp)
- hdu 5791 Two(2016 Multi-University Training Contest 5——DP)
- Cg Programming/Unity/Curved Glass曲面玻璃
- 使用Notepad++将多行数据合并成一行
- 关于MIUI 9沉浸式状态栏的最新适配
- android中实现对多个EditText的监听
- 深度学习的一些参数说明以及trick
- HDU-2870 Largest Submatrix (线性dp 最大01矩阵)(2009 Multi-University Training Contest 7 )
- Redis 学习笔记(二)之字符串类型命令
- 用LSTM做一个简单预测的Demo
- 加法变乘法
- ansible执行脚本时显示Permission denied
- 关于iOS9之后的loadViewIfNeeded
- httclient
- java 常用集合list与Set、Map区别及适用场景总结
- theano入门学习