一道\(DP\)

原题链接

发现只有\(a,b,c\)三种情况,所以直接初始化成三个\(01\)方阵,找最大子矩阵即可。

我是先初始化垂直上的高度,然后对每一行处理出每个点向左向右的最大延伸,并不断计算矩阵大小来更新答案。

因为不想开函数传数组,所以全写在主函数复制粘贴了三遍。。代码显得比较冗长。

#include<cstdio>
#include<cstring>
using namespace std;
const int N = 1010;
int A[N][N], B[N][N], C[N][N], l[N], r[N];
char re_l()
{
char c = getchar();
for (; c != 'a'&&c != 'b'&&c != 'c'&&c != 'x'&&c != 'y'&&c != 'z'&&c != 'w'; c = getchar());
return c;
}
inline int maxn(int x, int y)
{
return x > y ? x : y;
}
int main()
{
int i, j, ma, n, m;
char c;
while (scanf("%d%d", &n, &m)==2)
{
ma = 1;
memset(A, 0, sizeof(A));
memset(B, 0, sizeof(B));
memset(C, 0, sizeof(C));
for (i = 1; i <= n; i++)
for (j = 1; j <= m; j++)
{
c = re_l();
if (c == 'a' || c == 'w' || c == 'y' || c == 'z')
A[i][j] = A[i - 1][j] + 1;
if (c == 'b' || c == 'w' || c == 'x' || c == 'z')
B[i][j] = B[i - 1][j] + 1;
if (c == 'c' || c == 'x' || c == 'y' || c == 'z')
C[i][j] = C[i - 1][j] + 1;
}
for (i = 1; i <= n; i++)
{
for (j = 1; j <= m; j++)
l[j] = r[j] = j;
A[i][0] = A[i][m + 1] = -1;
for (j = 1; j <= m; j++)
while (A[i][j] <= A[i][l[j] - 1])
l[j] = l[l[j] - 1];
for (j = m; j; j--)
while (A[i][j] <= A[i][r[j] + 1])
r[j] = r[r[j] + 1];
for (j = 1; j <= m; j++)
ma = maxn(ma, (r[j] - l[j] + 1)*A[i][j]);
}
for (i = 1; i <= n; i++)
{
for (j = 1; j <= m; j++)
l[j] = r[j] = j;
B[i][0] = B[i][m + 1] = -1;
for (j = 1; j <= m; j++)
while (B[i][j] <= B[i][l[j] - 1])
l[j] = l[l[j] - 1];
for (j = m; j; j--)
while (B[i][j] <= B[i][r[j] + 1])
r[j] = r[r[j] + 1];
for (j = 1; j <= m; j++)
ma = maxn(ma, (r[j] - l[j] + 1)*B[i][j]);
}
for (i = 1; i <= n; i++)
{
for (j = 1; j <= m; j++)
l[j] = r[j] = j;
C[i][0] = C[i][m + 1] = -1;
for (j = 1; j <= m; j++)
while (C[i][j] <= C[i][l[j] - 1])
l[j] = l[l[j] - 1];
for (j = m; j; j--)
while (C[i][j] <= C[i][r[j] + 1])
r[j] = r[r[j] + 1];
for (j = 1; j <= m; j++)
ma = maxn(ma, (r[j] - l[j] + 1)*C[i][j]);
}
printf("%d\n", ma);
}
return 0;
}

HDOJ2870 Largest Submatrix的更多相关文章

  1. Largest Submatrix(动态规划)

    Largest Submatrix Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

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

    Largest Submatrix of All 1’s Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 8551   Ac ...

  3. hdu 2870 Largest Submatrix(平面直方图的最大面积 变形)

    Problem Description Now here is a matrix with letter 'a','b','c','w','x','y','z' and you can change ...

  4. 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 m ...

  5. codeforces 407D Largest Submatrix 3

    codeforces 407D Largest Submatrix 3 题意 找出最大子矩阵,须满足矩阵内的元素互不相等. 题解 官方做法 http://codeforces.com/blog/ent ...

  6. 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 m ...

  7. POJ 3494 Largest Submatrix of All 1’s 单调队列||单调栈

    POJ 3494 Largest Submatrix of All 1’s Description Given a m-by-n (0,1)-matrix, of all its submatrice ...

  8. POJ - 3494 Largest Submatrix of All 1’s 单调栈求最大子矩阵

    Largest Submatrix of All 1’s Given a m-by-n (0,1)-matrix, of all its submatrices of all 1’s which is ...

  9. HDU 2870 Largest Submatrix (单调栈)

    http://acm.hdu.edu.cn/showproblem.php? pid=2870 Largest Submatrix Time Limit: 2000/1000 MS (Java/Oth ...

随机推荐

  1. Django 基础教程中的Django表单

    在 urls.py 中对应写上这个函数,教程中给的Django 1.7x以下的,我的时2.0.7,应该为 from django.contrib import admin from django.ur ...

  2. Feign 注意事项

    一.FeignClient注解 FeignClient注解被@Target(ElementType.TYPE)修饰,表示FeignClient注解的作用目标在接口上 1 2 3 4 5 @FeignC ...

  3. iostat磁盘监控工具

    安装iostat磁盘监控工具 1.安装 yum install sysstat 2.运行 iostat -k -d -x 1 10 -k:以kb为单位统计 -d:显示磁盘状态 -x:显示详细信息 1: ...

  4. 加载 AssetBundle 的四种方法

    [加载 AssetBundle 的四种方法] 1.AssetBundle.LoadFromMemoryAsync(byte[] binary, uint crc = 0); 返回AssetBundle ...

  5. 添加setuptools脚本

    #!/usr/bin/env python """Setuptools bootstrapping installer. Maintained at https://gi ...

  6. ss源码学习--从协议建立到完成一次代理请求

    上一次介绍了ss源码中各个事件处理函数完成的工作,这次具体分析一下协议的建立以及请求数据的传输过程. 因为ss的local和server共用一个类以及一系列的事件处理函数,所以看起来稍显复杂.下面来将 ...

  7. Cisco & H3C 交换机 DHCP 中继

    个人理解:其实在核心交换上配置dhcp 中继也就是短短的几条命令,主要是注意细节,具体配置如下,希望能帮到大家: DHCP服务器IP:192.168.1.100 CISCO: 第一步:开启交换机的dh ...

  8. AI图谱

  9. Java_1简介

    1.Java版本 JavaSE  基础标准版 J2ME     小型版 JavaEE   企业版(主要针对Javaweb程序进行开发) 2.Java特点 开源跨平台 跨平台的原因:Java必须先只能装 ...

  10. Head First Servlets & JSP 学习笔记 第二章 —— Web应用体系结构

    Servlet没有main()方法,所以Servlet受其他人控制,这个其他人就是容器!而Tomcat就是一种容器. 容器向Servlet提供Http请求和Http响应:容器来调用Servlet的do ...