一道\(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. python3 得到a.txt中有的而b.txt中没有的汉字

    已知两个文本文档,求a.txt中有的而b.txt中没有的汉字 #读取list1中的汉字 f1=open('/Users/tanchao/Documents/pythonwork/tensorflow/ ...

  2. python基础学习Day9 函数的初识,实参、形参、

    1.函数 def my_len(): l = [,,,,,,] count = for i in l: count += print(count) my_len() 定义的my_len()方法时,其结 ...

  3. Java http请求工具类

    该工具类可以调用POST请求或者Get请求,参数以Map的方式传入,支持获获取返回值,返回值接收类型为String HttpRequestUtil.java package com.util; imp ...

  4. sass 使用clac的问题

    最后在github的issue中找到了方法,要想在sass的calc中使用变量,必须对这个变量使用sass的插值方法(#{$variable}). 所以把代码改正下面的形式就可以了: width: c ...

  5. 学习笔记001之[Android开发视频教学].01_06_Android当中的常见控件

    文本框,按钮 菜单按钮(需复写两个方法) 后续需完成联系代码.

  6. javascript学习笔记(七):事件详解

    HTML事件处理 <!DOCTYPE html> <html> <head lang="en"> <meta chaset="U ...

  7. V4 V7 V13支持包的区别(转)

    三者均为支持包,可以让低版本系统使用高版本特性,支持最小版本有差异 V4支持1.6以上 V7支持2.1以上 V13支持3.2以上 V7依赖V4 转自:

  8. 【scrapy】其他问题

    今天看<python爬虫开发与项目实践>的17章写代码的时候发现,一个方法的结尾带了红色波浪线: def _process_booklist_item(self,item): ''' 处理 ...

  9. ORA-01555 snapshot too old

    假设有一张6000万行数据的testdb表,预计testdb全表扫描1次需要2个小时,参考过程如下: 1.在1点钟,用户A发出了select * from testdb;此时不管将来testdb怎么变 ...

  10. leetcode 栈和队列类型题

    1,Valid Parentheses bool isVaild1(string& s) { // 直接列举,不易扩展 stack<char> stk; ; i < s.le ...