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;
}

最新文章

  1. android 启动模式介绍
  2. 关于PCA的几何表示——MATLAB实现
  3. JS判断是否来自手机移动端的访问,并跳转
  4. 结合setTimeout和clearTimeout,实现“返回顶部”的功能
  5. Qt中的串口编程之三
  6. int main(int argc,char *argv[])参数的应用
  7. [js高手之路]深入浅出webpack教程系列9-打包图片(file-loader)用法
  8. Cache 和 Buffer 都是缓存,主要区别是什么?
  9. springboot swagger-ui结合
  10. oracle sql developer 出现 : 适配器无法建立连接问题解决方案 The Network Adapter could not establish the connection
  11. Redis基础认识及常用命令使用(一)--技术流ken
  12. JS获得元素相对位置坐标getBoundingClientRect()
  13. python的基础初始第二天
  14. selenium Grid2 分布式自动化测试环境搭建
  15. R基本图形示例及代码(持续收集)
  16. servlet、servlet容器和web应用程序的关系
  17. Redis为什么使用单进程单线程方式
  18. 下载各个版本java (Java Development Kit)
  19. Google Maps API v2密钥申请以及实现地图定位导航
  20. BZOJ4057 [Cerc2012]Kingdoms

热门文章

  1. Apache新的URL路由重写规则
  2. SpringMVC中静态资源的处理
  3. Python Django 协程报错,进程池、线程池与异步调用、回调机制
  4. 创建包含CRUD操作的Web API接口3:实现Post方法
  5. SQL Server 索引优化——无用索引
  6. spring boot打包,依赖、配置文件分离,拷贝启动脚本
  7. 并发编程之Java锁
  8. Jest did not exit one second after the test run has completed.
  9. JavaScript常用数组操作方法,包含ES6方法
  10. 一分钟读懂低功耗蓝牙(BLE)连接数据包