0

# hdu2870 Largest Submatrix--DP

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

`#include<iostream>  #include<string.h>#include<algorithm>  using namespace std;char ch[1005][1005];int a[1005][1005];int b[1005][1005];int c[1005][1005];int l[1005];int r[1005];int n, m;int Solve(int x[1005][1005]){  int finalAns = -1;  for (int i = 1; i <= n; i++)  {    l[1] = 1;    r[m] = m;    for (int j = 2; j <= m; j++)    {      l[j] = j;      while (l[j] != 1 && x[i][j] <= x[i][l[j] - 1])        l[j] = l[l[j] - 1];    }    for (int j = m - 1; j >= 1; j--)    {      r[j] = j;      while (r[j] != m&&x[i][j] <= x[i][r[j] + 1])        r[j] = r[r[j] + 1];    }    for (int j = 1; j <= m; j++)      if (x[i][j] * (r[j] - l[j] + 1) > finalAns)        finalAns = x[i][j] * (r[j] - l[j] + 1);  }  return finalAns;}int main(){  char ch;  while (~scanf("%d%d", &n, &m))  {    for (int i = 1; i <= n; i++)    {      getchar();      for (int j = 1; j <= m; j++)      {        scanf("%c", &ch);        //a        if (ch == 'a' || ch == 'w' || ch == 'y' || ch == 'z')          a[i][j] = a[i - 1][j] + 1;        else          a[i][j] = 0;        //b        if (ch == 'b' || ch == 'w' || ch == 'x' || ch == 'z')          b[i][j] = b[i - 1][j] + 1;        else          b[i][j] = 0;        //c        if (ch == 'c' || ch == 'x' || ch == 'y' || ch == 'z')          c[i][j] = c[i - 1][j] + 1;        else          c[i][j] = 0;      }    }    int a_ans = Solve(a);    int b_ans = Solve(b);    int c_ans = Solve(c);    printf("%d\n", max(max(a_ans, b_ans), c_ans));  }  return 0;}`

0 条评论