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;}
0 0