hdu1506 Largest SubMatrix

一次ac,难的呀,和之前做的01最大矩阵差不多的,再加一维记录多‘a‘,‘b‘,‘c‘的每种情况的高度

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <string>
#include <stack>
#include <queue>

const int inf = (1<<31)-1;
const int MAXN = 1e3+10;
using namespace std;

char s[MAXN];
int a[MAXN][MAXN][3];
int L[MAXN][3];
int R[MAXN][3];

int main()
{
    int n,m;
    while(scanf("%d%d",&n,&m)==2){
        memset(a,0,sizeof(a));
        for(int i=1;i<=n;i++){
            scanf("%s",s);
            for(int j=0;j<m;j++){
                if(s[j]==‘a‘||s[j]==‘y‘||s[j]==‘w‘||s[j]==‘z‘)a[i][j+1][0] = a[i-1][j+1][0]+1;
                if(s[j]==‘b‘||s[j]==‘x‘||s[j]==‘w‘||s[j]==‘z‘)a[i][j+1][1] = a[i-1][j+1][1]+1;
                if(s[j]==‘c‘||s[j]==‘x‘||s[j]==‘y‘||s[j]==‘z‘)a[i][j+1][2] = a[i-1][j+1][2]+1;
            }
        }
        int ans,mmax = -inf;
        for(int i=1;i<=n;i++){

            a[i][0][0] = a[i][0][1] = a[i][0][2] = -1;
            a[i][m+1][0] = a[i][m+1][1] = a[i][m+1][2] = -1;

            for(int j=1;j<=m;j++){
                //L[j][0]=L[j][1] = L[j][2] = j;
                for(int k=0;k<3;k++){
                    L[j][k] = j;
                    while(a[i][j][k]<=a[i][L[j][k]-1][k])
                        L[j][k] = L[L[j][k]-1][k];
                }
            }
            for(int j=m;j>=1;j--){
                for(int k=0;k<3;k++){
                    R[j][k] = j;
                    while(a[i][j][k]<=a[i][R[j][k]+1][k])
                        R[j][k] = R[R[j][k]+1][k];
                }
            }
            for(int j=1;j<=m;j++){
                for(int k=0;k<3;k++){
                    ans = (R[j][k]-L[j][k]+1)*a[i][j][k];
                    mmax = max(ans,mmax);
                }
            }
        }
        cout<<mmax<<endl;
    }
    //cout << "Hello world!" << endl;
    return 0;
}

只能说模板真好用

时间: 2024-10-12 08:27:29

hdu1506 Largest SubMatrix的相关文章

Largest Submatrix(动态规划)

Largest Submatrix Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2018    Accepted Submission(s): 967 Problem Description Now here is a matrix with letter 'a','b','c','w','x','y','z' and you ca

POJ 3494 Largest Submatrix of All 1’s(单调栈)

题目: Largest Submatrix of All 1’s Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 5540   Accepted: 2085 Case Time Limit: 2000MS Description Given a m-by-n (0,1)-matrix, of all its submatrices of all 1’s which is the largest? By largest w

hdu 2870 Largest Submatrix

Largest Submatrix http://acm.hdu.edu.cn/showproblem.php?pid=2870 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Problem Description Now here is a matrix with letter 'a','b','c','w','x','y','z' and you can change '

HDU 2870 Largest Submatrix (单调栈)

http://acm.hdu.edu.cn/showproblem.php?pid=2870 Largest Submatrix Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1569    Accepted Submission(s): 748 Problem Description Now here is a matrix wit

POJ 3494 Largest Submatrix of All 1&#39;s 栈的运用 好题

Language: Default Largest Submatrix of All 1’s Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 5185   Accepted: 1950 Case Time Limit: 2000MS Description Given a m-by-n (0,1)-matrix, of all its submatrices of all 1’s which is the largest

Largest Submatrix of All 1’s

Given a m-by-n (0,1)-matrix, of all its submatrices of all 1’s which is the largest? By largest we mean that the submatrix has the most elements. Input The input contains multiple test cases. Each test case begins with m and n (1 ≤ m, n ≤ 2000) on li

HDU 2870 Largest Submatrix(DP)

题意  求最大相同字符子矩阵  其中一些字符可以转换 其实就是HDU1505 1506的加强版  但是分了a,b,c三种情况  看哪次得到的面积最大 对于某一个情况  可以把该字符和可以转换为该字符的位置赋值0 其它位置赋值1 这样就转化成了求最大全0矩阵的问题了 对于转换后矩阵中的每个点 看他向上有多少个连续0 把这个值存在h数组中 再用l数组和r数组记录h连续大于等于该位置的最左边位置和最右位置 这样包含(i,j)点的最大矩阵面积就是(r[i][j]-l[i][j]+1)*h[i][j] 面

codeforces 407D Largest Submatrix 3

codeforces 407D Largest Submatrix 3 题意 找出最大子矩阵,须满足矩阵内的元素互不相等. 题解 官方做法 http://codeforces.com/blog/entry/11333 \(O(n^6)\) 枚举子矩阵,暴力check. \(O(n^4)\) 枚举上下边界,双指针. \(O(n^3log_2n)\) 假设当前上边界 \(up\), 下边界 \(down\),\(R_i\) 表示当 \(i\) 为左边界时,右边界最大是 \(R_i\). 当 \(do

2906: Largest Submatrix of All 1’s

描述 Given a m-by-n (0,1)-matrix, of all its submatrices of all 1’s which is the largest? By largest we mean that the submatrix has the most elements. 输入 The input contains multiple test cases. Each test case begins with m and n (1 ≤ m, n ≤ 2000) on li