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. elasticsearch 管理常用命令集合

    elasticsearch rest api遵循的格式为: curl -X<REST Verb> <Node>:<Port>/<Index>/<T ...

  2. idea右下角显示使用内存情况

    效果 设置

  3. 悬架的灵魂——K&C特性

    静止便是死亡,只有运动才能敲开永生的大门     —  泰戈尔 KC特性是车辆操控稳定性的直接影响者!可以分为 K ( Kinematic) 特性和 C( Compliance) 特性: K 特性即悬 ...

  4. python 之 Django框架(Django框架简介、视图装饰器、request对象、Response对象)

    12.33 Django框架简介: MVC,全名是Model View Controller,是软件工程中的一种软件架构模式,把软件系统分为三个基本部分:模型(Model).视图(View)和控制器( ...

  5. day41——数值类型、完整性约束

    day41 数值类型 整数类型 有符号的设置 mysql> create table t1(id tinyint); # 默认有符号,即数字前有正负号 无符号的设置 mysql> crea ...

  6. PAT(B) 1045 快速排序(C)

    题目链接:1045 快速排序 (25 point(s)) 参考博客:1045 快速排序 (25 point(s))繁星蓝雨 题目描述 著名的快速排序算法里有一个经典的划分过程:我们通常采用某种方法取一 ...

  7. 3:基于乐观锁(两种)控制并发: version、external锁

    ES是基于乐观锁进行并发控制的. 如果有并发的业务场景,可以直接使用ES内置乐观锁机制. 使用的时候,java程序需要先Get指定的记录,获取到版本号,然后Put的时候,带着该版本号,请求更新. ES ...

  8. 可拖拽dialog

    指令的封装转自https://blog.csdn.net/sinat_21902709/article/details/86545444 可拖拽dialog应用于很多弹出框,所以需要作用于全局 在插件 ...

  9. bsd pkg install gcc gmake cmake gdb cgdb

    bsd pkg install gcc gmake cmake gdb cgdb 安装pkg帮助文档并查看文档# pkg help install# man pkg-install # pkg sea ...

  10. dubbo源码阅读之自适应扩展

    自适应扩展机制 刚开始看代码,其实并不能很好地理解dubbo的自适应扩展机制的作用,我们不妨先把代码的主要逻辑过一遍,梳理一下,在了解了代码细节之后,回过头再来思考自适应扩展的作用,dubbo为什么要 ...