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. 解析Tensorflow官方English-Franch翻译器demo

    今天我们来解析下Tensorflow的Seq2Seq的demo.继上篇博客的PTM模型之后,Tensorflow官方也开放了名为translate的demo,这个demo对比之前的PTM要大了很多(首 ...

  2. 最全面 Nginx 入门教程 + 常用配置解析

    转自 http://blog.csdn.net/shootyou/article/details/6093562 Nginx介绍和安装 一个简单的配置文件 模块介绍 常用场景配置 进阶内容 参考资料 ...

  3. OpenLayers访问WTMS服务及添加Googlemap

    1.访问WMS服务 首先需要发布WMS服务,才能进行地图WMS服务访问.这里不说怎么发布WMS服务,直接看怎么调用,代码如下: 代码 Code highlighting produced by Act ...

  4. C4.5较ID3的改进

    1.ID3选择最大化Information Gain的属性进行划分   C4.5选择最大化Gain Ratio的属性进行划分 规避问题:ID3偏好将数据分为很多份的属性 解决:将划分后数据集的个数考虑 ...

  5. java reflection总结

    一.java反射常用方法 获取Class的几种方式: Class class1 = String.class;// 该方法最为安全可靠,程序性能更高.         Class class2 = s ...

  6. JQuery UI 精品UI推荐

    1.JQuery MiniUi  http://www.miniui.com/

  7. javascript第六课类型转换

    1.parseint(参数): 转换为整数,即使参数中的字符串包含字母数字混合,此方法也会自动一个一个判断和转换   parseInt(参数,进制);将参数通过几进制的方式转为数字 2.parsefl ...

  8. SharePoint 2013设置“以其他用户身份登录”

    登录web服务器,打开位于“C:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\TEMPLATE\CONTR ...

  9. c++中,size_typt, size_t, ptrdiff_t 简介

    size_type 类型 从逻辑上来讲,size() 成员函数似乎应该返回整形数值,或如 2.2 节“建议”中所述的无符号整数.但事实上,size 操作返回的是 string::size_type 类 ...

  10. VARIANT类型

    VARIANT的结构可以参考头文件VC98\Include\OAIDL.H中关于结构体tagVARIANT的定义.struct  tagVARIANT    {    union         {  ...