http://acm.hdu.edu.cn/showproblem.php?

pid=2870

Largest Submatrix
Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1569    Accepted Submission(s): 748
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
 
Source
 

题意:

矩阵中有7种字母:abcwxyz,w可替换为a或b,x可替换为b或c,y可替换为a或c,z可替换为a、b或c。求一个子矩阵,该矩阵中全部元素都同样,问该矩阵最大是多少?

分析:

首先将w,x,y,z换为a,b,c;得到3个01矩阵。然后就是求元素所有是1的最大子矩阵,能够转化为面积做。

维护出第i行各元素的“高度”,然后用单调栈向左/右找到比该点低的位置并记录,最后计算面积。

/*
*
* Author : fcbruce <fcbruce8964@gmail.com>
*
* Time : Sat 25 Oct 2014 07:10:43 PM CST
*
*/
#include <cstdio>
#include <iostream>
#include <sstream>
#include <cstdlib>
#include <algorithm>
#include <ctime>
#include <cctype>
#include <cmath>
#include <string>
#include <cstring>
#include <stack>
#include <queue>
#include <list>
#include <vector>
#include <map>
#include <set>
#define sqr(x) ((x)*(x))
#define LL long long
#define itn int
#define INF 0x3f3f3f3f
#define PI 3.1415926535897932384626
#define eps 1e-10 #ifdef _WIN32
#define lld "%I64d"
#else
#define lld "%lld"
#endif #define maxm
#define maxn 1007 using namespace std; int trans[maxn][maxn][3];
char matrix[maxn][maxn];
int n,m;
int ql[maxn],qr[maxn],fl,rl,fr,rr;
int h[maxn];
int l[maxn],r[maxn]; inline void init(int x,int y,char ch)
{
switch (ch)
{
case 'a':
trans[x][y][0]=1;
break;
case 'b':
trans[x][y][1]=1;
break;
case 'c':
trans[x][y][2]=1;
break;
case 'w':
trans[x][y][0]=trans[x][y][1]=1;
break;
case 'x':
trans[x][y][1]=trans[x][y][2]=1;
break;
case 'y':
trans[x][y][0]=trans[x][y][2]=1;
break;
case 'z':
trans[x][y][0]=trans[x][y][1]=trans[x][y][2]=1;
break;
}
} int solve(int k)
{
int MAX=0;
memset(h,0,sizeof h);
for (int i=0;i<n;i++)
{
for (int j=0;j<m;j++)
{
if (trans[i][j][k]==1) h[j]++;
else h[j]=0;
} fl=fr=0;rl=rr=-1;
for (int j=0;j<m;j++)
{
while (fl<=rl && h[ql[rl]]>=h[j]) rl--;
if (fl<=rl) l[j]=ql[rl]+1;
else l[j]=0;
ql[++rl]=j; while (fr<=rr && h[qr[rr]]>=h[m-j-1]) rr--;
if (fr<=rr) r[m-j-1]=qr[rr];
else r[m-j-1]=m;
qr[++rr]=m-j-1;
} for (int j=0;j<m;j++)
MAX=max(MAX,h[j]*(r[j]-l[j]));
} return MAX;
} int main()
{
#ifdef FCBRUCE
freopen("/home/fcbruce/code/t","r",stdin);
#endif // FCBRUCE while (scanf("%d%d",&n,&m)==2)
{
for (int i=0;i<n;i++)
scanf("%s",matrix[i]); memset(trans,0,sizeof trans);
for (int i=0;i<n;i++)
for (int j=0;j<m;j++)
init(i,j,matrix[i][j]); int MAX=0;
for (int i=0;i<3;i++)
MAX=max(MAX,solve(i)); printf("%d\n",MAX);
} return 0;
}

HDU 2870 Largest Submatrix (单调栈)的更多相关文章

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

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

  2. hdu2870 Largest Submatrix 单调栈

    描述 就不需要描述了... 题目传送门 题解 被lyd的书的标签给骗了(居然写了单调队列优化dp??)  看了半天没看出来哪里是单调队列dp,表示强烈谴责QAQ w x y z  可以各自 变成a , ...

  3. HDU 2870 Largest Submatrix

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

  4. hdu 5033 buiding(单调栈)

    hdu 5033 buiding(单调栈) 某年某月某天,马特去了一个小镇.这个小镇如此狭窄,以至于他可以把小镇当作一个枢纽.在镇上有一些摩天大楼,其中一栋位于xi,高度为hi.所有的摩天大楼位于不同 ...

  5. hdu - 5033 - Building(单调栈)

    题意:N 幢楼排成一列(1<=N<=10^5),各楼有横坐标 xi(1<=xi<=10^7) 以及高度 hi(1<=hi<=10^7),在各楼之间的Q个位置(1&l ...

  6. poj 2559 Largest Rectangle(单调栈)

    Largest Rectangle in a Histogram Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 26549 ...

  7. HDU 5033 Building (维护单调栈)

    题目链接 题意:n个建筑物,Q条询问,问所在的位置,看到天空的角度是多少,每条询问的位置左右必定是有建筑物的. 思路 : 维护一个单调栈,将所有的建筑物和所有的人都放到一起开始算就行,每加入一个人,就 ...

  8. HDU 5033 Building --离线+单调栈

    题意:给一些建筑物,x表示横坐标,h表示高度,然后查询某些坐标x,问从该点看向天空的最大张角是多大. 解法:离线操作,读入所有数据,然后按x升序排序,对每一个查询的x,先从左到右,依次添加x坐标小于x ...

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

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

随机推荐

  1. <在此处打开命令窗口>替换为PowerShell打开模式

    Windows7中Shift+右键"在此处打开命令窗口"默认是采用cmd的方式打开. 把cmd替换为PowerShell的方式打开. 1. Ctrl + R 输入regedit进入 ...

  2. leetcode_894. All Possible Full Binary Trees

    https://leetcode.com/problems/all-possible-full-binary-trees/ 给定节点个数,求所有可能二叉树,该二叉树所有节点要么有0个子节点要么有两个子 ...

  3. 启动myeclipse弹窗Please allow Subclipse team to receive anonymous usage statistics for this Eclipse intance

    Please allow Subclipse team to receive anonymous usage statistics for this Eclipse intance(翻译:请允许Sub ...

  4. scrollfix.js插件:滚动固定在某个位置

    插件文件在/文件 scrollfix.js 用法: var fix = $(".fix"), fixtop = $(".fix-top"), fixStartT ...

  5. 关于Map的遍历

    想起之前有人问过我这个,那就顺手写一下Map的遍历 Map<Integer, String> map = new HashMap<Integer, String>(); map ...

  6. MySQL与MyBatis中的查询记录

    1.时间段查询 MySQL:select * from table where ctime >= CURDATE() and ctime <DATE_SUB(CURDATE(),INTER ...

  7. 1 SQL 数据库和SQL

    1 数据库和SQL 1-1 数据库是什么 将大量数据保存起来,通过计算机加工而成的可以进行高效访问的数据集合称为数据库(Database, DB). 法则 用来管理数据库的计算机系统称为数据库管理系统 ...

  8. python3.x Day6 paramiko

    python3 paramiko模块,用来进行远程操作linux服务器,利用的就是ssh #利用用户名,密码,进行连接 import paramiko #创建一个SSH对象 ssh=paramiko. ...

  9. Mysql:零散记录

    limit用法 查询第4行记录 select * from tablename limit 3,1; limit 3,1:截取第3行加1行的数据 查询第6-15行 select * from tabl ...

  10. A - 栈

    Description   You are given a string consisting of parentheses () and []. A string of this type is s ...