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

题意:求一最大子矩阵(该矩阵的元素相同)的个数

思路:我们可以把这道题抽象成直方图

用l[]和r[]两个数组分别记录该点比他大的最左下标和最右下标

当在搜索下标为i的单位矩阵时,当i-1的下标的单位矩阵高度高于它时,其实我们是已经判断过下标为i-1的单位矩阵的最左端
下标的,所以这就满足dp的条件,只要把左边各个连续且大于h[i]高度的矩阵的最远下边记录下来即可。

#include <cstdio>
#include <map>
#include <iostream>
#include<cstring>
#include<bits/stdc++.h>
#define ll long long int
#define M 6
using namespace std;
inline ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
inline ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
int moth[]={,,,,,,,,,,,,};
int dir[][]={, ,, ,-, ,,-};
int dirs[][]={, ,, ,-, ,,-, -,- ,-, ,,- ,,};
const int inf=0x3f3f3f3f;
const ll mod=1e9+;
char G[][];
int l[],r[];
int h[];
int m,n;
int main(){
ios::sync_with_stdio(false);
char t[]={'a','b','c'};
char equal[][]={'b','c','x','a','c','y','a','b','w'};
while(cin>>m>>n){
for(int i=;i<=m;i++)
for(int j=;j<=n;j++)
cin>>G[i][j];
int ans=-inf;
for(int ii=m;ii>=;ii--){ //遍历每一行
for(int i=;i<;i++){ //每一行都有'a','b','c'三种情况
char temp=t[i];
memset(h,,sizeof(h));
memset(l,,sizeof(l));
memset(r,,sizeof(r));
for(int k=;k<=n;k++)
for(int j=ii;j>=;j--){
if(G[j][k]==equal[i][]||G[j][k]==equal[i][]||G[j][k]==equal[i][])
break;
h[k]++; //记录该层的高度
}
l[]=; r[n]=n;
for(int k=;k<=n;k++){
int t=k;
while(t>&&h[k]<=h[t-]){
t=l[t-];
}
l[k]=t;
}
for(int k=n-;k>=;k--){
int t=k;
while(t<n&&h[k]<=h[t+]){
t=r[t+];
}
r[k]=t;
}
for(int k=;k<=n;k++)
ans=max(ans,h[k]*(r[k]-l[k]+));
}
}
cout<<ans<<endl;
}
}

hdu 2870 Largest Submatrix(平面直方图的最大面积 变形)的更多相关文章

  1. HDU 2870 Largest Submatrix (单调栈)

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

  2. HDU 2870 Largest Submatrix

    这三道题的关系是这样的,1505是1506的加强版,2870又是1505的加强版 如果按照上面由简到易的顺序来做的话,还是很简单的 这道题的思想就是 枚举+DP 因为某些字符可以变值,所以我们枚举a, ...

  3. Largest Submatrix(动态规划)

    Largest Submatrix Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  4. POJ 3494 Largest Submatrix of All 1’s

    POJ 2796 Feel Good HDU 1506 Largest Rectangle in a Histogram 和这两题一样的方法. #include<cstdio> #incl ...

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

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

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

  7. codeforces 407D Largest Submatrix 3

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

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

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

随机推荐

  1. 使用Pyspark进行特征工程时的那些坑

    以脚本spark_clean_online_action.py.数据集new_sxf_time_count_1781115582.csv为例: 集群节点包括212.216.217.218.需要注意的是 ...

  2. Momenta电话面试笔记

  3. 调用不同目录类的protected构造器

    一.问题 二.分析 调用不同目录类的protected构造器,IDE报错. 二.解决办法: 后面添加一个{}就可以了

  4. Android 安全退出应用程序的方法总结

    正常关闭应用程序: 当应用不再使用时,通常需要关闭应用,可以使用以下三种方法关闭android应用: 第一种方法:首先获取当前进程的id,然后杀死该进程. android.os.Process.kil ...

  5. 网易云课堂-----Linux内核分析-----期末主观题

    姚歌 + 原创作品转载请注明出处 + <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 下面是对8个课题的 ...

  6. oracle加注释

    COMMENT ON table GC_G_DOC84 IS '行政处罚撤销决定书'; COMMENT ON column GC_G_DOC84.CASEID IS '案件记录ID';

  7. Junit测试用例

    一.题目简介 返回一个给定整数参数的绝对值. 二.源码的github链接 https://github.com/liyan941016/test/blob/master/FileTest.java h ...

  8. java学习--第50天讲到jquery

    4月4日jquery讲完了. jquery组合选择器 逗号隔开 层级选择器 父元素    子元素,直接子元素和间接子元素,空格隔开. 直接后代选择器: 父元素>子元素    选择的直接子元素 下 ...

  9. The MathType Dll cannot be found 问题解决办法

    被这个问题困扰了许久,找到了解决办法,没想到最后居然是因为mathtype安装路径里的文件位置有问题(至少我是这么认为的).是这样的,在安装完mathtype6.9b后,发现打开word2013是正常 ...

  10. Using svn in CLI with Batch

    del %~n0.txt@echo offsetlocal EnableDelayedExpansionfor /f "delims=" %%i in ('DIR /A:D /B' ...