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;
}

最新文章

  1. linux分区机制(MBR GPT)简介
  2. ES6深入学习记录(一)class方法相关
  3. Hadoop: MapReduce2多个job串行处理
  4. vim中添加molokai.vim 配色安装
  5. 加密,解密,Hash
  6. java中DriverManager跟DataSource获取getConnection有什么不同?
  7. ubuntu下搭建nagios
  8. chromedriver 与 chrome关联关系
  9. My97DatePicker使用的问题
  10. .NET程序员生活开始
  11. 通过js实现在页面中添加音乐
  12. c++设计模式-----抽象工厂模式
  13. 【Troubleshooting Case】Unable to delete Exchange database?
  14. java-bootstrap
  15. solr7.2安装实例,中文分词器
  16. Group(), Groups(),&amp; Groupdict() 用法
  17. 16. pt-mysql-summary
  18. ES6中的proxy
  19. 【转】每天一个linux命令(48):watch命令
  20. DXDBGrid使用方法

热门文章

  1. Winform中ListView鼠标移动使用toolTip显示信息
  2. iOS 对overflow:scroll使用
  3. ubuntu下pycharm无法使用pip安装python包的修复方案
  4. 【经验分享】IMX6开发板编译问题及解决方法
  5. C# 处理年月日提取时间
  6. BZOJ4318: OSU! (概率DP)
  7. 第2节 mapreduce深入学习:2、3
  8. NOIP2016玩具迷题
  9. lnmp -memcached使用
  10. buf.entries()详解