• [HDU 2870] Largest Submatrix


    [题目链接]

              http://acm.hdu.edu.cn/showproblem.php?pid=2870

    [算法]

            dp + 单调栈

    [代码]

            

    #include<bits/stdc++.h>
    using namespace std;
    #define MAXN 1010
    
    int i,j,n,m,ans;
    char mp[MAXN][MAXN];
    int fa[MAXN][MAXN],fb[MAXN][MAXN],fc[MAXN][MAXN];
    
    inline int getans(int *a)
    {
            int i,top = 0,tmp;
            int res = 0;
            int stk[MAXN],w[MAXN];
            stk[0] = -1;
            a[m + 1] = -1;
            for (i = 1; i <= m + 1; i++)
            {
                    if (a[i] >= stk[top]) 
                    {
                            stk[++top] = a[i];
                            w[top] = 1;
                            continue;
                    }
                    tmp = 0;
                    while (a[i] < stk[top])
                    {
                            tmp += w[top];
                            res = max(res,stk[top] * tmp);
                            top--;
                    }
                    stk[++top] = a[i];
                    w[top] = tmp + 1;
            }        
            return res;
    }
    
    int main() 
    {
            
            while (scanf("%d%d",&n,&m) != EOF)
            { 
                    for (i = 1; i <= n; i++) scanf("%s",mp[i] + 1);
                    for (i = 1; i <= n; i++)
                    {
                            for (j = 1; j <= m; j++)
                            {
                                    if (mp[i][j] == 'a' || mp[i][j] == 'w' || mp[i][j] == 'y' || mp[i][j] == 'z')
                                            fa[i][j] = fa[i - 1][j] + 1;
                                    else fa[i][j] = 0;    
                            }    
                    }
                    for (i = 1; i <= n; i++)
                    {
                            for (j = 1; j <= m; j++)
                            {
                                    if (mp[i][j] == 'b' || mp[i][j] == 'w' || mp[i][j] == 'x' || mp[i][j] == 'z')
                                            fb[i][j] = fb[i - 1][j] + 1;
                                    else fb[i][j] = 0;
                            }
                    }
                    for (i = 1; i <= n; i++)
                    {
                            for (j = 1; j <= m; j++)
                            {
                                    if (mp[i][j] == 'c' || mp[i][j] == 'x' || mp[i][j] == 'y' || mp[i][j] == 'z')
                                            fc[i][j] = fc[i - 1][j] + 1;
                                    else fc[i][j] = 0;
                            }
                    }
                    ans = 0;
                    for (i = 1; i <= n; i++) ans = max(ans,max(max(getans(fa[i]),getans(fb[i])),getans(fc[i])));
                    printf("%d
    ",ans);
            }
            
            return 0;
        
    }
  • 相关阅读:
    你读了该博客中哪些超链接?有何感想
    最理想的师生关系是健身教练和学员的关系,在这种师生关系中你期望获得来自老师的哪些帮助?
    1500802028 王莉娟
    解码方法
    N皇后问题
    两个链表的交叉
    全排列
    交叉字符串
    翻转链表
    爬楼梯
  • 原文地址:https://www.cnblogs.com/evenbao/p/9356493.html
Copyright © 2020-2023  润新知