一道\(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. Spark之机器学习(Python版)(一)——聚类

    https://www.cnblogs.com/charlotte77/p/5437611.html

  2. hive 排序 分组计数后排序 几种不同函数的效果

    [转至:http://blackproof.iteye.com/blog/2164260] 总结: 三个分析函数都是按照col1分组内从1开始排序 (假设4个数,第2和第3个数据相同)    row_ ...

  3. Java NIO Path

    Java NIO Path Creating a Path Instance Creating an Absolute Path Creating a Relative Path Path.norma ...

  4. Snipaste截图

    Snipaste 是一个简单但强大的贴图工具,同时也可以执行截屏.标注等功能. 引自: https://blog.csdn.net/qq_36279445/article/details/702109 ...

  5. django搭建的站点,通过localhost能访问,但是通过ip不能访问

    问题:使用ip访问不了django站点,只能用127.0.0.1访问     解决方法:启动服务时ip使用0.0.0.0   使用gunicorn启动 gunicorn -w4 -b0.0.0.0:8 ...

  6. Vue-axios快速上手(转)

    引入方式: 1 2 3 4 5 $ npm install axios $ cnpm install axios //taobao源 $ bower install axios 或者使用cdn: &l ...

  7. CentOS 下搭建Hudson

    1.下载Hudson安装包 wget http://ftp.jaist.ac.jp/pub/eclipse/hudson/war/hudson-3.3.3.war 2.执行 java -jar hud ...

  8. AltiumDesigner 网络标号放置技巧

    我们在使用AD画图的过程中,灵活的使用一些小技巧可以使我们的工作事半功倍,今天给大家介绍一种在画原理图过程中的小技巧. 我们在画原理图过程中经常会放置网络标号,尤其是很多芯片管脚上,例如现在我需要在这 ...

  9. 配置eclipse+SDK+ADT开发环境

    第一步:配置jdk和eclipse环境(已完成): 第二步:ADT配置:依次点击菜单栏:help -> Install new software -> Add -> Local... ...

  10. ubuntu14.04 安装系统/搜狗/QT/qq/wps/CAJviewer

    1.安装ubuntu系统     http://jingyan.baidu.com/album/4dc40848491fc5c8d946f1b1.html?picindex=1 官方网站:    ht ...