HDU 2870 largest submatrix (monotonous stack)

Source: Internet
Author: User

Http://acm.hdu.edu.cn/showproblem.php? PID = 1, 2870

Largest SubmatrixTime Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1569    Accepted Submission(s): 748


Problem descriptionnow 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?
Inputthe 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.
Outputfor each test case, output one line containing the number of elements of the largest submatrix of all same letters.
Sample Input
2 4abcwwxyz
 
Sample output
3
 
Source2009 multi-university training contest 7-host by fzu
Question:

There are 7 letters in the matrix: abcwxyz, W can be replaced with A or B, X can be replaced with B or C, y can be replaced with A or C, Z can be replaced with A, B, or C. Evaluate a child matrix. All elements in the matrix are the same. What is the maximum value of the Matrix?

Analysis:

First, replace w, x, y, z with A, B, C; to obtain three 01 matrices, and then calculate the largest child matrix where all elements are 1, which can be converted to area.

Maintain the "height" of each element in line I, use the monotonous stack to find the position lower than this point on the left/right, and record it. Finally, calculate the area.


/* * * Author : fcbruce <[email protected]> * * 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 1007using 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 (monotonous stack)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.