zoukankan      html  css  js  c++  java
  • 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;
    }
    View Code

     只能说模板真好用

    在一个谎言的国度,沉默就是英雄
  • 相关阅读:
    627. Swap Salary
    176. Second Highest Salary
    596. Classes More Than 5 Students
    183. Customers Who Never Order
    181. Employees Earning More Than Their Managers
    182. Duplicate Emails
    175. Combine Two Tables
    620. Not Boring Movies
    595. Big Countries
    HDU 6034 Balala Power! (贪心+坑题)
  • 原文地址:https://www.cnblogs.com/EdsonLin/p/5389105.html
Copyright © 2011-2022 走看看