HDU2870_Largest Submatrix【最大完全子矩阵】

Largest Submatrix

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

Total Submission(s): 1589    Accepted Submission(s): 761

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

2009 Multi-University Training Contest 7 - Host by FZU

题目大意:有个字母矩阵,包含字母"a、b、c、w、x、y、z",其中,w能变为"a、b",

x能变为"b、c",y能变为"a、c",z能变为"a、b、c"。问能构成的最大字母完全一样的子

矩阵面积为多大?

思路:和HDU1505、HDU1506一样的思路,其中a[i][j]表示转换为字母a后以第i行为底,

第j列上方连续空闲位置的高度。b[i][j]表示字母b……,c[i][j]表示字母c……

遍历计算出该点向左右两边延伸的左右边界,从而计算出面积,最终比较计算出最大面积。

参考博文:HDU1505http://blog.csdn.net/lianai911/article/details/40302489

HDU1506http://blog.csdn.net/lianai911/article/details/40208265

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;

int a[1100][1100],b[1100][1100],c[1100][1100],l[1100],r[1100];

int main()
{
    int M,N;
    char ch;
    while(~scanf("%d%d",&M,&N))
    {
        for(int i = 1; i <= M; i++)
        {
            getchar();
            for(int j = 1; j <= N; j++)
            {
                scanf("%c",&ch);
                if(ch=='a' || ch=='w' || ch=='y' || ch=='z')
                    a[i][j] = a[i-1][j] + 1;
                else
                    a[i][j] = 0;
                if(ch=='b' || ch=='w' || ch=='x' || ch=='z')
                    b[i][j] = b[i-1][j] + 1;
                else
                    b[i][j] = 0;
                if(ch=='c' || ch=='x' || ch=='y' || ch=='z')
                    c[i][j] = c[i-1][j] + 1;
                else
                    c[i][j] = 0;
            }
        }

        int area = -0xffffff0;
        for(int i = 1; i <= M; i++)
        {
//            ------------------求a最大面积---------------
            l[0] = 1,r[N+1] = N;
            a[i][0] = a[i][N+1] = -1;
            for(int j = 1; j <= N; j++)
            {
                l[j] = j;
                while(a[i][l[j]-1] >= a[i][j])
                    l[j] = l[l[j]-1];
            }
            for(int j = N; j >= 1; j--)
            {
                r[j] = j;
                while(a[i][r[j]+1] >= a[i][j])
                    r[j] = r[r[j]+1];
            }
            for(int j = 1; j <= N; j++)
            {
                if(a[i][j] * (r[j]-l[j]+1) > area)
                    area = a[i][j] * (r[j]-l[j]+1);
            }
//            ----------------求b最大面积----------------
            l[0] = 1,r[N+1] = N;
            b[i][0] = b[i][N+1] = -1;
            for(int j = 1; j <= N; j++)
            {
                l[j] = j;
                while(b[i][l[j]-1] >= b[i][j])
                    l[j] = l[l[j]-1];
            }
            for(int j = N; j >= 1; j--)
            {
                r[j] = j;
                while(b[i][r[j]+1] >= b[i][j])
                    r[j] = r[r[j]+1];
            }
            for(int j = 1; j <= N; j++)
            {
                if(b[i][j] * (r[j]-l[j]+1) > area)
                    area = b[i][j] * (r[j]-l[j]+1);
            }
//            -------------求c最大面积---------------
            l[0] = 1,r[N+1] = N;
            c[i][0] = c[i][N+1] = -1;
            for(int j = 1; j <= N; j++)
            {
                l[j] = j;
                while(c[i][l[j]-1] >= c[i][j])
                    l[j] = l[l[j]-1];
            }
            for(int j = N; j >= 1; j--)
            {
                r[j] = j;
                while(c[i][r[j]+1] >= c[i][j])
                    r[j] = r[r[j]+1];
            }
            for(int j = 1; j <= N; j++)
            {
                if(c[i][j] * (r[j]-l[j]+1) > area)
                    area = c[i][j] * (r[j]-l[j]+1);
            }
        }
        printf("%d\n",area);
    }
    return 0;
}
时间: 2024-10-18 13:03:17

HDU2870_Largest Submatrix【最大完全子矩阵】的相关文章

[CareerCup] 18.12 Largest Sum Submatrix 和最大的子矩阵

18.12 Given an NxN matrix of positive and negative integers, write code to find the submatrix with the largest possible sum. 这道求和最大的子矩阵,跟LeetCode上的Maximum Size Subarray Sum Equals k和Maximum Subarray很类似.这道题不建议使用brute force的方法,因为实在是不高效,我们需要借鉴上面LeetCode

[LintCode] Submatrix Sum 子矩阵之和

Given an integer matrix, find a submatrix where the sum of numbers is zero. Your code should return the coordinate of the left-up and right-down number. Have you met this question in a real interview? Yes Example Given matrix [ [1 ,5 ,7], [3 ,7 ,-8],

Submatrix Sum

Given an integer matrix, find a submatrix where the sum of numbers is zero. Your code should return the coordinate of the left-up and right-down number. Subarray Sum的follow up.求二维矩阵上和为0的submatrix. 这题暴力解法枚举左上两坐标,右下两坐标,求中间和.复杂度为O(n^6).太高. 比较好的办法是枚举开始行和

子矩阵(暴搜(全排列)+DP)

子矩阵(暴搜(全排列)+DP) 一.题目 子矩阵 时间限制: 1 Sec  内存限制: 128 MB 提交: 1  解决: 1 [提交][状态][讨论版] 题目描述 给出如下定义: 1. 子矩阵:从一个矩阵当中选取某些行和某些列交叉位置所组成的新矩阵(保持行与列的相对顺序)被称为原矩阵的一个子矩阵. 例如,下面左图中选取第2.4行和第2.4.5列交叉位置的元素得到一个2*3的子矩阵如右图所示. 9 3 3 3 9 9 4 8 7 4 1 7 4 6 6 6 8 5 6 9 7 4 5 6 1 的

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

题目: Largest Submatrix of All 1’s Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 5540   Accepted: 2085 Case Time Limit: 2000MS Description Given a m-by-n (0,1)-matrix, of all its submatrices of all 1’s which is the largest? By largest w

hdu 2870 Largest Submatrix

Largest Submatrix http://acm.hdu.edu.cn/showproblem.php?pid=2870 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Problem Description Now here is a matrix with letter 'a','b','c','w','x','y','z' and you can change '

POJ3494Largest Submatrix of All 1’s[单调栈]

Largest Submatrix of All 1’s Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 5883   Accepted: 2217 Case Time Limit: 2000MS Description Given a m-by-n (0,1)-matrix, of all its submatrices of all 1’s which is the largest? By largest we me

Max Sub-matrix

Max Sub-matrix 教练找的题目,目前样列过了 题意:找子矩阵的最大周长 思路:先离散每列,再枚举列(n*n),在当前枚举的两列之间求每行的和(n*n*n),但是开两个数组,一个包含两列上的元素  一个不包含,这样可以处理出前i行当前这两列上的元素和.  当前两列中每行元素和知道   两列上前i项和知道就可以找出最大值 1 #include <iostream> 2 #include <cstdio> 3 #include <algorithm> 4 #inc

HDU 2870 Largest Submatrix (单调栈)

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 wit