一道\(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. ASP.NET 散碎知识

    1.按钮点击打开一个新的Web窗体,可在按钮点击事件里面写:Response.Redirect("窗体的名字.aspx"); 2.复合控件: CheckBoxList - 复选框组 ...

  2. Missing parentheses in call to 'print'

    这个消息的意思是你正在试图用python3.x来运行一个只用于python2.x版本的python脚本. print"Hello world" 上面的语法在python3中是错误的 ...

  3. soap 简单调用其他系统的函数

    <?xml version ='1.0' encoding ='UTF-8' ?> <definitions name='自定义名称' targetNamespace='目标命名空间 ...

  4. 与元素类型 "item" 相关联的 "name" 属性值不能包含 '<' 字符。

    Android Studio 打包时,报错: 与元素类型 "item" 相关联的 "name" 属性值不能包含 '<' 字符. 这个问题自己百度也没有发现 ...

  5. Gimbal Lock

    [Gimbal Lock] 万向锁源于欧拉角的是有序处理的.U3D中的序列为: y->x->z.当旋转y时,local坐标系与世界坐标系重合,所以y等于永远按惯性坐标旋转.当x旋转+/-9 ...

  6. json数组转java对象

    <dependency> <groupId>net.sf.json-lib</groupId> <artifactId>ison-lib</art ...

  7. js中函数的 this、arguments 、caller,call(),apply(),bind()

    在函数内部有两个特殊的对象,arguments 和 this,还有一个函数对象的属性caller. arguments对象 arguments是一个类似数组的对象,包含着传入函数的所有参数. func ...

  8. vue-router导航守卫,限制页面访问权限

    在项目开发过程中,经常会需要登录.注册.忘记密码等,也有很多页面是需要登录后才能访问,有些页面是无需登录就可以访问的,那么vue是怎么来限制这些访问权限问题的呢? vue-router导航守卫的bef ...

  9. PAT1131(dfs)

    In the big cities, the subway systems always look so complex to the visitors. To give you some sense ...

  10. 取得<asp:TextBox中的值:

     取得<asp:TextBox中的值:  var a= document.getElementById("<%= (ID名).ClientID %>").valu ...