hdoj 2870 Largest Submatrix 【单调栈】
2016-05-03 14:52
357 查看
题目链接:hdoj 2870 Largest Submatrix
Largest Submatrix
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2108 Accepted Submission(s): 1013
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 4
abcw
wxyz
Sample Output
3
题意:给定一个n*m的字符矩阵,你可以将w -> a 或 b,将x -> b 或 c,将y -> a 或 c,将z -> a 或 b 或 c。问你字符相同的最大矩阵。
枚举行,然后就是维护三个单调栈。
AC代码:
Largest Submatrix
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2108 Accepted Submission(s): 1013
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 4
abcw
wxyz
Sample Output
3
题意:给定一个n*m的字符矩阵,你可以将w -> a 或 b,将x -> b 或 c,将y -> a 或 c,将z -> a 或 b 或 c。问你字符相同的最大矩阵。
枚举行,然后就是维护三个单调栈。
AC代码:
#include <iostream> #include <cstdio> #include <cmath> #include <algorithm> #include <cstring> #define CLR(a, b) memset(a, (b), sizeof(a)) #define fi first #define se second using namespace std; typedef long long LL; typedef pair<int, int> pii; const int MAXN = 1e4 +10; const int INF = 0x3f3f3f3f; char str[1010]; pii Stack[1010]; int da[1010], db[1010], dc[1010]; LL dp[1010]; int n, m; int Solve(int d[]) { int ans = 0, top = 0; int l, r; for(int j = 1; j <= m; j++) { r = 0; while(top && Stack[top-1].fi >= d[j]) { if(r == 0) r = Stack[top-1].se; if(top == 1) { l = Stack[top-1].se; } else { l = Stack[top-1].se - Stack[top-2].se; } ans = max(ans, (r - Stack[top-1].se + l) * Stack[top-1].fi); top--; } Stack[top++] = pii(d[j], j); } r = 0; while(top) { if(r == 0) r = Stack[top-1].se; if(top == 1) { l = Stack[top-1].se; } else { l = Stack[top-1].se - Stack[top-2].se; } ans = max(ans, (r - Stack[top-1].se + l) * Stack[top-1].fi); top--; } return ans; } int main() { while(scanf("%d%d", &n, &m) != EOF) { for(int j = 1; j <= m; j++) da[j] = db[j] = dc[j] = 0; int ans = 0; for(int i = 1; i <= n; i++) { scanf("%s", str+1); for(int j = 1; j <= m; j++) { if(str[j] == 'a') { da[j]++; db[j] = dc[j] = 0; } else if(str[j] == 'b') { db[j]++; da[j] = dc[j] = 0; } else if(str[j] == 'c') { dc[j]++; da[j] = db[j] = 0; } else if(str[j] == 'w') { da[j]++; db[j]++; dc[j] = 0; } else if(str[j] == 'x') { db[j]++; dc[j]++; da[j] = 0; } else if(str[j] == 'y') { da[j]++; dc[j]++; db[j] = 0; } else { da[j]++; db[j]++; dc[j]++; } } ans = max(ans, Solve(da)); ans = max(ans, Solve(db)); ans = max(ans, Solve(dc)); } printf("%d\n", ans); } return 0; }
相关文章推荐
- android学习日志,点击几次后执行某操作的代码
- 最好的8个Java RESTful框架
- Android 数据库ORM开源框架之greenDAO
- Android Density(密度)
- SVN回退到历史版本
- Caffe安装笔记三:后记
- PyQt5教程——对话框(6)
- HDU-2059 龟兔赛跑( DP )
- 【笔记】关于全栈开发、技术发展方向,软件开发模式的思考
- 什么是大数据思维?
- rt.jar ,dt.jar ,tool.jar都是 做什么用的
- c++中斜杠和反斜杠的作用 相对路径和绝对路径
- Android中常见的adb命令
- javascript内存和性能
- 使用Mono打造轻量级的.NET程序运行时
- ajax 跨域
- 五 数据组织模式 2) 分区模式 代码
- 三 概要模式 3) MR计数器计数 。无 reduce 计数
- nil、Nil、NULL和NSNull的理解
- Android Toolbar使用系统原生返回键,并改变其颜色,自定义图片替换系统原生返回键