Largest Submatrix

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

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

题解:

上题 的加强版。

三种情况。

全部变为a,全部为b,全部为c,分别求最大。

代码:

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
using namespace std;
const int INF=0x3f3f3f3f;
#define mem(x,y) memset(x,y,sizeof(x))
#define SI(x) scanf("%d",&x)
#define PI(x) printf("%d",x)
#define SD(x,y) scanf("%lf%lf",&x,&y)
#define P_ printf(" ")
const int MAXN=;
typedef long long LL;
int dp[][MAXN][MAXN],s[MAXN],l[MAXN],r[MAXN];
char mp[MAXN][MAXN];
bool isa(char ch){
if(ch=='a'||ch=='w'||ch=='y'||ch=='z')
return true;
else return false;
}
bool isb(char ch){
if(ch=='b'||ch=='w'||ch=='x'||ch=='z')
return true;
else return false;
}
bool isc(char ch){
if(ch=='c'||ch=='x'||ch=='y'||ch=='z')
return true;
else return false;
} int main(){
int N,M;
while(~scanf("%d%d",&N,&M)){
for(int i=;i<=N;i++)
scanf("%s",mp[i]+);
mem(dp,);
int ans=;
for(int i=;i<=N;i++){
for(int j=;mp[i][j];j++){
if(isa(mp[i][j]))dp[][i][j]=dp[][i-][j]+;
s[j]=dp[][i][j];l[j]=j;r[j]=j;
}
s[]=s[M+]=-;
for(int j=;j<=M;j++){
while(s[l[j]-]>=s[j])
l[j]=l[l[j]-];
}
for(int j=M;j>=;j--){
while(s[r[j]+]>=s[j])
r[j]=r[r[j]+];
}
for(int j=;j<=M;j++){
ans=max((r[j]-l[j]+)*s[j],ans);
}
//
for(int j=;mp[i][j];j++){
if(isb(mp[i][j]))dp[][i][j]=dp[][i-][j]+;
s[j]=dp[][i][j];l[j]=j;r[j]=j;
}
s[]=s[M+]=-;
for(int j=;j<=M;j++){
while(s[l[j]-]>=s[j])
l[j]=l[l[j]-];
}
for(int j=M;j>=;j--){
while(s[r[j]+]>=s[j])
r[j]=r[r[j]+];
}
for(int j=;j<=M;j++){
ans=max((r[j]-l[j]+)*s[j],ans);
}
//
for(int j=;mp[i][j];j++){
if(isc(mp[i][j]))dp[][i][j]=dp[][i-][j]+;
s[j]=dp[][i][j];l[j]=j;r[j]=j;
}
s[]=s[M+]=-;
for(int j=;j<=M;j++){
while(s[l[j]-]>=s[j])
l[j]=l[l[j]-];
}
for(int j=M;j>=;j--){
while(s[r[j]+]>=s[j])
r[j]=r[r[j]+];
}
for(int j=;j<=M;j++){
ans=max((r[j]-l[j]+)*s[j],ans);
}
}
printf("%d\n",ans);
}
return ;
}

Largest Submatrix(动态规划)的更多相关文章

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

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

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

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

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

  4. codeforces 407D Largest Submatrix 3

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

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

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

  8. HDU 2870 Largest Submatrix (单调栈)

    http://acm.hdu.edu.cn/showproblem.php? pid=2870 Largest Submatrix Time Limit: 2000/1000 MS (Java/Oth ...

  9. MINSUB - Largest Submatrix

    MINSUB - Largest Submatrix no tags  You are given an matrix M (consisting of nonnegative integers) a ...

随机推荐

  1. MySql的一些问题

    问题1:卸载重装mysql时,报1045和2003错误. 解决:点击skip,跳过这个错误.进到my.ini,在mysqld下面加一句:skip-grant-tables,保存.重启mysql服务,在 ...

  2. AsyncTask详解

    http://blog.csdn.net/liuhe688/article/details/6532519 在Android中实现异步任务机制有两种方式,Handler和AsyncTask. Hand ...

  3. ab压力测试报错: apr_socket_recv: Connection reset by peer (104)

    使用ab对网站进行压力测试,开始设置并发500,可以正常使用,当设置并发为1000,则报错: apr_socket_recv: Connection reset by peer (104) 改服务端a ...

  4. 在JavaScript的数组中进行数组元素查找和替换(JS的indexOf等)

    <html> <head> <title> Extend JavaScript Array Method </title> <script lan ...

  5. JS 事件绑定的几种方式 小笔记

    第一种 var test=document.getElementById('add'); add.onclick=function(){ alert('1'); } 直接在对象上注册事件 缺点:如果我 ...

  6. 关于css3中before与after用单冒号还是双冒号的疑虑

    在 CSS3 中为了区别伪元素和伪类为伪元素使用了双冒号,因此如果使用了 display 或者 width 等属性时使得显示脱离了原本元素后,建议按照标准双写.

  7. android监听键盘

    android中的带有输入功能的页面布局经常被弹出的键盘遮挡,一种处理方法是监听键盘的弹出,设置布局的padding或隐藏某些占位控件,使得输入框不被键盘遮挡.一种常用的方法是当Activity设置为 ...

  8. DataSetToList 和 DataTableTolist 转换

    DataSetToList 及DataTableTolist经常使用,在此分享一下我的方法. DataSetToList 和 DataTableTolist 转换 DataSetToList 和 Da ...

  9. MySql5.6Window超详细安装教程(msi 格式的安装)

    转自:红黑联盟  http://www.2cto.com/database/201506/409821.html 一.安装包准备 1.下载MySql5.6 http://www.mysql.com/ ...

  10. Node.js(转) -- 临时来说还看不懂!

    转自:http://blog.jobbole.com/53736/ 本文由 伯乐在线 - Lellansin 翻译.未经许可,禁止转载!英文出处:toptal.欢迎加入翻译组. 介绍 JavaScri ...