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. Maven打包报错:[WARNING] The POM for xxx is missing, no dependency inform

    maven install 或 package 时 ,执行警告报错: [WARNING] The POM for com.xx-base:jar:1.0 is missing, no dependen ...

  2. 新手配置LNMP环境教程

    回顾一下这几天自己配置LNMP环境踩得坑,希望帮助更多人 前期准备:VMtool.Linux.Nginx.Mysql.PHP.cmake 版本如下:Centos6.nginx1.6.0.mysql5. ...

  3. Postman 使用方法详细介绍

    1,下载安装: https://www.getpostman.com/apps 2,打开Postman,如图所示: 3,创建一个接口项目 4,新增接口文件 5,下面以登录接口login为例,介绍如何做 ...

  4. 第5/7Beta冲刺

    1.团队成员 成员姓名 成员学号 秦裕航 201731062432(组长) 刘东 201731062227 张旭 201731062129 王伟 201731062214 2.SCRU部分 2.1各成 ...

  5. 8、1 周末总结+Mongdb

    都说加一个Id 注解就行了,其实还要加一条数据测试表是否生成 这里我们探讨  数据库是谁,表是谁 数据库在mongo启动的时候就指定了,这个无需我们关心 表根据实体类自动生成, 1.pom.xml & ...

  6. Qt deletelater函数分析(1)

               生活的全部意义在于无穷地探索尚未知道的东西,在于不断地增加更多的知识.--左拉 该函数是QObject类的函数:                             ---- ...

  7. QT 5.x 网络资源集锦

    github上的好书:太好了: http://qmlbook.github.io/en/ch01/index.html 论坛: 基于QT的音乐创作软件:(是不是可以跟谷歌的深度学习艺术项目结合) ht ...

  8. matlab调试时子函数断点不起作用

    matlab调试代码时总是遇到这样一个奇怪的问题,就是当我在主程序(.m脚本)中调用子函数并在子函数中设置断点,然后开始调试运行主程序... 发现主程序直接运行到结束而并没有在调用子函数的时候在所设置 ...

  9. linux命令行模式与图形界面切换

    1.实时切换 1.1 命令行->图形 执行startx命令 1.2 图形->命令行 Ctrl+Alt+F1--F6 2.启动默认 2.1 启动进入命令行 修改/etc/inittab文件 ...

  10. [转帖]央行推出数字货币DCEP:基于区块链技术、将取代现钞

    央行推出数字货币DCEP:基于区块链技术.将取代现钞 天天快报的内容. 密码财经 2019-10-29 18:15 关注   前不久的10月23日,Facebook的首席执行官扎克伯格在美国国会听证会 ...