hdoj 2870 Largest Submatrix 【单调栈】
来源:互联网 发布:数据恢复软件哪个好 编辑:程序博客网 时间:2024/05/18 03:32
题目链接: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代码:
#include <iostream>#include <cstdio>#include <cmath>#include <algorithm>#include <cstring>#define CLR(a, b) memset(a, (b), sizeof(a))#define fi first#define se secondusing 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;}
- hdoj 2870 Largest Submatrix 【单调栈】
- HDU 2870 Largest Submatrix (单调栈)
- hdu - 2870 - Largest Submatrix(dp / 单调栈)
- SPOJ MINSUN Largest Submatrix [二分+单调栈]
- [SPOJ MINSUB Largest Submatrix]单调栈
- poj3494 Largest Submatrix of All 1’s 单调栈+dp
- POJ3494---Largest Submatrix of All 1’s(单调栈)
- 单调栈 Largest Submatrix of All 1’s poj3494
- POJ3494 Largest Submatrix of All 1’s[单调栈]
- Largest Submatrix of All 1’s(POJ 3494) 单调栈
- SPOJ-MINSUB - Largest Submatrix(单调栈,二分,好题)
- Largest Submatrix of All 1’s----单调栈
- poj 3494 Largest Submatrix of All 1’s(单调栈)
- 单调栈--poj3494 Largest Submatrix of All 1’s
- HDOJ题目2870 Largest Submatrix(动态规划)
- HDU 2870 Largest Submatrix
- hdu 2870 Largest Submatrix
- hdu 2870 Largest Submatrix
- nm命令中符号类型详解
- Android Density(密度)
- Caffe安装笔记三:后记
- Android 数据库ORM开源框架之greenDAO
- android学习日志,点击几次后执行某操作的代码
- hdoj 2870 Largest Submatrix 【单调栈】
- IntelliJ Idea 常用快捷键列表
- dstat的使用
- 第八周-杀伤距离有限
- Java EE 基础
- AS使用技巧
- web.xml中Servlet 配置的四个节点
- Ubuntu16.04安装Atom
- (OK) dnf——install docker on Fedora23