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. 安装ngix

    第一步:解压源码包 第二步:./configure -->这个时候会提示缺少PCRE 这个时候要安装yum -y install pcre-devel 第三步:./configure --> ...

  2. JNDI(转载)

    转自:http://javacrazyer.iteye.com/blog/759485 原理:         在DataSource中事先建立多个数据库连接,保存在数据库连接池中.当程序访问数据库时 ...

  3. python map, reduce,filter 使用

    参考python built-on function: http://docs.python.org/2.7/library/functions.html?highlight=map%20reduce ...

  4. node.js 入门教程(beginnder guide

    非常好的教程: node入门: JavaScript与Node.js JavaScript与你 简短申明 服务器端JavaScript “Hello World” 一个完整的基于Node.js的web ...

  5. 传智播客C/C++学员荣膺微软&Cocos 2d-x黑客松最佳创新奖

     6月30日,历时32小时的微软开放技术Cocos 2d-x 编程黑客松在北京望京微软大厦成功落下帷幕,这是微软开放技术首次联合Cocos 2d-x 在中国举办黑客松.此次活动共有包括传智播客C/ ...

  6. oracle check if the display variable is set

  7. linux学习(二)-目录的操作命令

    Linux命令大全:http://www.jb51.net/linux/ 目录分绝对路径和相对路径 : 绝对路径,在路径前会加  / 相对路径就是相对于当前的路径,直接 路径名即可. 查看目录: cd ...

  8. 一个利用扩展方法的实例:AttachDataExtensions

    扩展方法是C# 3.0(老赵对VB不熟)中最简单,也是最常用的语言特性之一.这是老赵自以为的一个简单却不失经典的实例: [AttributeUsage(AttributeTargets.All, Al ...

  9. Linux下装Eclipse C/C++,以及环境配置

    由于前些日子朋友搞个智能家居开发,用C语言写的.叫我装个CentOS(Linux中的一种)来进行开发,所以这几天都在摸索怎么装,当然,朋友也有给予一丁点帮助(可恶的色长.你叫我装东西,也不帮帮我),由 ...

  10. vs2010:【“System.Data.OracleClient.OracleConnection”已过时】警告

    在oracle 安装目录下 找到 Oracle.DataAccess.dll添加引用,然后 using Oracle.DataAccess.Client;其他的都不用动,即可.连接字符串中 如有 用的 ...