Largest Submatrix

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3105    Accepted Submission(s): 1476


Problem Description
Now here is a matrix with letter 'a','b','c','w','x','y','z' and you can change 'w' to 'a' or 'b', change 'x' to 'b' or 'c', change 'y' to 'a' or 'c', and change 'z' to 'a', 'b' or 'c'. After you changed it, what's the largest submatrix with the same letters you can make?
 

Input
The input contains multiple test cases. Each test case begins with m and n (1 ≤ m, n ≤ 1000) on line. Then come the elements of a matrix in row-major order on m lines each with n letters. The input ends once EOF is met.
 

Output
For each test case, output one line containing the number of elements of the largest submatrix of all same letters.
 

Sample Input
2 4
abcw
wxyz
 

Sample Output
3
 

Source
 

Recommend
gaojie   |   We have carefully selected several similar problems for you:  2830 2577 1505 2845 1069 
 

Statistic | Submit | Discuss | Note

有个字母矩阵,包含字母"a、b、c、w、x、y、z",其中,w能变为"a、b",x能变为"b、c",y能变为"a、c",z能变为"a、b、c"。问能构成的最大字母完全一样的子矩阵面积为多大?

对三种字符分别考虑,每次尽量转换成一种字符。

问题就变成了最大01子矩阵。上单调栈即可。

时间复杂度\(O(mn)\)

#include<iostream>
#define rg register
#define il inline
#define co const
template<class T>il T read(){
rg T data=0,w=1;rg char ch=getchar();
while(!isdigit(ch)) {if(ch=='-') w=-1;ch=getchar();}
while(isdigit(ch)) data=data*10+ch-'0',ch=getchar();
return data*w;
}
template<class T>il T read(rg T&x) {return x=read<T>();}
typedef long long ll;
using namespace std; co int N=1002;
int m,n,a[N][N],b[N][N],c[N][N],s[N],w[N];
char buf[N];
void Largest_Submatrix(){
for(int i=1;i<=m;++i){
scanf("%s",buf+1);
for(int j=1;j<=n;++j){
if(buf[j]=='a'||buf[j]=='w'||buf[j]=='y'||buf[j]=='z') a[i][j]=a[i-1][j]+1;
else a[i][j]=0;
if(buf[j]=='b'||buf[j]=='w'||buf[j]=='x'||buf[j]=='z') b[i][j]=b[i-1][j]+1;
else b[i][j]=0;
if(buf[j]=='c'||buf[j]=='x'||buf[j]=='y'||buf[j]=='z') c[i][j]=c[i-1][j]+1;
else c[i][j]=0;
}
}
int ans=0;
for(int i=1;i<=m;++i){
// max of a
int p=0;
for(int j=1;j<=n+1;++j){
if(a[i][j]>s[p]) s[++p]=a[i][j],w[p]=1;
else{
int width=0;
while(s[p]>a[i][j]){
width+=w[p];
ans=max(ans,width*s[p]);
--p;
}
s[++p]=a[i][j],w[p]=width+1;
}
}
// max of b
p=0;
for(int j=1;j<=n+1;++j){
if(b[i][j]>s[p]) s[++p]=b[i][j],w[p]=1;
else{
int width=0;
while(s[p]>b[i][j]){
width+=w[p];
ans=max(ans,width*s[p]);
--p;
}
s[++p]=b[i][j],w[p]=width+1;
}
}
// max of c
p=0;
for(int j=1;j<=n+1;++j){
if(c[i][j]>s[p]) s[++p]=c[i][j],w[p]=1;
else{
int width=0;
while(s[p]>c[i][j]){
width+=w[p];
ans=max(ans,width*s[p]);
--p;
}
s[++p]=c[i][j],w[p]=width+1;
}
}
}
printf("%d\n",ans);
}
int main(){
while(~scanf("%d %d",&m,&n)) Largest_Submatrix();
return 0;
}

HDU2870 Largest Submatrix的更多相关文章

  1. hdu2870 Largest Submatrix 单调栈

    描述 就不需要描述了... 题目传送门 题解 被lyd的书的标签给骗了(居然写了单调队列优化dp??)  看了半天没看出来哪里是单调队列dp,表示强烈谴责QAQ w x y z  可以各自 变成a , ...

  2. Largest Submatrix(动态规划)

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

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

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

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

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

  5. 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 ...

  6. codeforces 407D Largest Submatrix 3

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

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

  8. 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 ...

  9. 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 ...

随机推荐

  1. Tomcat详解|乐字节

    大家好,欢迎来到乐字节小乐的Java技术分享园地.这次给大家分享的是Tomcat   一. 什么是 Tomcat Tomcat 是一个符合 JavaEE WEB 标准的最小的 WEB 容器,所有的 J ...

  2. Java学习关注

    1.不去上课: 内部类的继承: https://blog.csdn.net/ruidianbaihuo/article/details/102092256 2.Matrix海 子 http://www ...

  3. TCP/IP学习笔记16--TCP--特点,数据重发,连接管理,段

    TCP充分实现了数据传输时各种控制功能,可以进行丢包时的重发控制,还可以对次序乱掉的包进行顺序控制,这些在UDP中都是没有的.UDP是一种没有复杂控制,提供面向无连接通信服务的一种协议.TCP是面向有 ...

  4. 13 JSP、MVC开发模式、EL表达式和JSPL标签+软件设计架构---学习笔记

    1.JSP (1)JSP概念:Java Server Pages 即java服务器端页面可以理解为:一个特殊的页面,其中既可以指定定义html标签,又可以定义java代码用于简化书写!!! (2)原理 ...

  5. 安装Delphi7的错误

    delphi7运行不正常的提示unable to rename'c:\program files\Borland\delphi7\Bin\delphi32.$$$'to'c:\program file ...

  6. SpringCloud之熔断器Hystrix及服务监控Dashboard

    目的:     服务雪崩效应 服务熔断服务降级 Hystrix默认超时时间设置 Hystrix服务监控Dashboard 服务雪崩效应 雪崩效应就是一种不稳定的平衡状态也是加密算法的一种特征,它指明文 ...

  7. idea常用插件安装

    1.IDEA Restart IDEA没有重启的选项,这个工具就是来弥补这个功能,可以在File-->Restart 重启,也可以使用快捷键  CTRL + ALT + R 2.Maven He ...

  8. isolate sqflite demo

    main.dart import 'package:flutter/material.dart'; import 'demo_isolates.dart'; import 'package:rxdar ...

  9. 2019年北航OO第三次博客总结

    一.JML语言理论基础及其工具链 1. JML语言理论基础 JML是用于对Java程序进行规格化设计的一种表示语言,是一种行为接口规格语言(Behavior Interface Specificati ...

  10. 原生JS获取HTML DOM元素的8种方法

    JS获取DOM元素的方法(8种) 通过ID获取(getElementById) 通过name属性(getElementsByName) 通过标签名(getElementsByTagName) 通过类名 ...