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. Python MySQLdb 学习总结(转)

    转自http://www.cnblogs.com/coser/archive/2012/01/12/2320741.html 感谢@糖拌咸鱼 任何应用都离不开数据,所以在学习python的时候,当然也 ...

  2. LeetCode 856. 括号的分数(Score of Parentheses)

    856. 括号的分数 856. Score of Parentheses 题目描述 给定一个平衡括号字符串 S,按下述规则计算该字符串的分数: () 得 1 分. AB 得 A + B 分,其中 A ...

  3. 04 Mybatis 框架的环境搭建及入门案例

    1.搭建 Mybatis 开发环境 mybatis的环境搭建 第一步:创建maven工程并导入坐标 第二步:创建实体类和dao的接口 第三步:创建Mybatis的主配置文件 SqlMapConifg. ...

  4. Feign输出Info级别日志

    背景 spring cloud netfix组件中,feign相关的日志默认是不会输出的,需要自定义配置才能输出,并且Feign只对Debug基本的日志做出响应, 实际业务需要输出Info级别的日志, ...

  5. 2019/7/18ACM集训

    2019-07-18 09:15:34 这个是练习刷的题 Vus the Cossack and Numbers Vus the Cossack has nn real numbers aiai. I ...

  6. AX导出excel设置格式

    今天在AX2009里面写一个导出EXCEL,没有模版,这是第一次碰到,之后写完之后发现导出的数据格式不对. 到处取经之后得到一下结果: 定义一个 Com   range; SysExcelCells  ...

  7. mysq 连表更新

    update table1 a left join table2 b on a.id = b.user_id left join table3 c on a.id = c.user_idset a.p ...

  8. 使用 SetParent 制作父子窗口的时候,如何设置子窗口的窗口样式以避免抢走父窗口的焦点

    原文:使用 SetParent 制作父子窗口的时候,如何设置子窗口的窗口样式以避免抢走父窗口的焦点 制作传统 Win32 程序以及 Windows Forms 程序的时候,一个用户看起来独立的窗口本就 ...

  9. 并发编程之Disruptor并发框架

    一.什么是Disruptor Martin Fowler在自己网站上写了一篇LMAX架构的文章,在文章中他介绍了LMAX是一种新型零售金融交易平台,它能够以很低的延迟产生大量交易.这个系统是建立在JV ...

  10. MQTT协议中的topic

    1.MQTT协议中的topic 定阅与发布必须要有主题,只有当定阅了某个主题后,才能收到相应主题的payload,才能进行通信. 2. 主题层级分隔符--"/" 主题层级分隔符使得 ...