[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\n",ans);
        }
        
        return 0;
    
}

猜你喜欢

转载自www.cnblogs.com/evenbao/p/9356493.html