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. td太多内容显示...

    table style="table-layout:fixed;"td style="text-overflow: ellipsis;white-space: nowra ...

  2. Python:staticmethod vs classmethod

    Being educated under Java background, static method and class method are the same thing. But not so ...

  3. rsyslog 日志服务器端配置

    $EscapeControlCharactersOnReceive off #关闭rsyslog默认转译ASCII<32的所有怪异字符,包括换行符等 $template tocFormat,&q ...

  4. OpenStreetMap(OSM) for developers

    This article from: http://wiki.openstreetmap.org/wiki/Develop OpenStreetMap isn't just open data - i ...

  5. Ajax学习教程在线阅读

      1.什么是AJAX ?(1) 2.什么是AJAX ?(2) 3.什么是AJAX ?(3) 4.什么是AJAX ?(4) 5.Ajax基础教程(1)-Ajax简介 1.1 Web应用简史 6.Aja ...

  6. 「花田对」CSDN程序员专场——谁来拯救技术宅!_豆瓣

    「花田对」CSDN程序员专场--谁来拯救技术宅!_豆瓣 「花田对」CSDN程序员专场--谁来拯救技术宅!

  7. poj1007 qsort快排

    这道题比较简单,但通过这个题我学会了使用c++内置的qsort函数用法,收获还是很大的! 首先简要介绍一下qsort函数. 1.它是快速排序,所以就是不稳定的.(不稳定意思就是张三.李四成绩都是90, ...

  8. 黑马程序员 Java基础<十八>---> 网路编程

    --------------- ASP.Net+Android+IO开发S..Net培训.期待与您交流! --------------- 第一  概述 一.概述: 1.网络模型:OSI参考模型和TCP ...

  9. 初步STL集装箱List

    List 特点: 1.它实质上是一个双向链表 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvd2FuZ3hpYW9idXB0/font/5a6L5L2T/f ...

  10. JavaScript 工作必知(九)function 说起 闭包问题

    大纲 Function Caller 返回函数调用者 Callee 调用自身 作用域 闭包 function 函数格式 function getPrototyNames(o,/*optional*/ ...