HDU 2870 Largest Submatrix(DP)

题意  求最大相同字符子矩阵  其中一些字符可以转换

其实就是HDU1505 1506的加强版  但是分了a,b,c三种情况  看哪次得到的面积最大

对于某一个情况  可以把该字符和可以转换为该字符的位置赋值0 其它位置赋值1 这样就转化成了求最大全0矩阵的问题了

对于转换后矩阵中的每个点 看他向上有多少个连续0 把这个值存在h数组中 再用l数组和r数组记录h连续大于等于该位置的最左边位置和最右位置 这样包含(i,j)点的最大矩阵面积就是(r[i][j]-l[i][j]+1)*h[i][j] 面积最大的点就是最大全0矩阵了

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N = 1005;
char s[N][N];
int mat[N][N], l[N][N], r[N][N], h[N][N], n, m, ans;

void makeMat (char a, char b, char c)
{
    memset (mat, 0, sizeof (mat));
    memset (l, 0, sizeof (l));
    memset (r, 0, sizeof (r));
    memset (h, 0, sizeof (h));
    for (int i = 1; i <= n; ++i)
    {
        h[i][0] = h[i][m + 1] = -1;
        for (int j = 1; j <= m; ++j)
        {
            l[i][j] = r[i][j] = j;
            if (s[i][j] == a || s[i][j] == b || s[i][j] == c)  mat[i][j] = 1;
            if (mat[i][j] == 0) h[i][j] = h[i - 1][j] + 1;
        }
    }
}

int solve (char a, char b, char c)
{
    makeMat (a, b, c);
    int aans = 0;
    for (int i = 1; i <= n; ++i)
    {
        for (int j = m; j >= 1; --j)
            while (h[i][r[i][j] + 1] >= h[i][j])
                r[i][j] = r[i][r[i][j] + 1];

        for (int j = 1; j <= m; ++j)
        {
            while (h[i][l[i][j] - 1] >= h[i][j])
                l[i][j] = l[i][l[i][j] - 1];
            aans = max (aans, (r[i][j] - l[i][j] + 1) * h[i][j]);
        }
    }
    return aans;
}

int main()
{
    while (~scanf ("%d%d", &n, &m))
    {
        for (int i = 1; i <= n; ++i)
            scanf ("%s", s[i] + 1);
        ans = max (solve ('x', 'b', 'c'), solve ('a', 'y', 'c'));
        ans = max (ans, solve ('a', 'b', 'w'));
        printf ("%d\n", ans);
    }
    return 0;
}

Largest Submatrix

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

HDU 2870 Largest Submatrix(DP)

时间: 2024-08-07 22:51:10

HDU 2870 Largest Submatrix(DP)的相关文章

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

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 '

hdu 2870 Largest Submatrix(hdu 1505的加强版)

1.ans=max(全为a的最大矩阵,全为b的最大矩阵,全为c的最大矩阵) 2.代码: #include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int INF=1<<30; char mat[1005][1005]; int a[1005][1005]; int b[1005][1005]; int c[1005][1005]; int La[1005

HDU 1506 &amp;&amp; HDU1505 &amp;&amp; HDU 2870 (DP).

~~~~ 这三道DP题是逐层递进的,大家可以从前往后做. 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1506 http://acm.hdu.edu.cn/showproblem.php?pid=1505 http://acm.hdu.edu.cn/showproblem.php?pid=2870 ~~~~ 1506: 分别找每一块板子的可以的最左边界和最右边界,注意这时不能用两个for暴力(显然会TLE),所以要用DP的思想边搜边保存. 另外注

Largest Submatrix(动态规划)

Largest Submatrix Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2018    Accepted Submission(s): 967 Problem Description Now here is a matrix with letter 'a','b','c','w','x','y','z' and you ca

codeforces 407D Largest Submatrix 3

codeforces 407D Largest Submatrix 3 题意 找出最大子矩阵,须满足矩阵内的元素互不相等. 题解 官方做法 http://codeforces.com/blog/entry/11333 \(O(n^6)\) 枚举子矩阵,暴力check. \(O(n^4)\) 枚举上下边界,双指针. \(O(n^3log_2n)\) 假设当前上边界 \(up\), 下边界 \(down\),\(R_i\) 表示当 \(i\) 为左边界时,右边界最大是 \(R_i\). 当 \(do

HDU 4960 (水dp)

Another OCD Patient Problem Description Xiaoji is an OCD (obsessive-compulsive disorder) patient. This morning, his children played with plasticene. They broke the plasticene into N pieces, and put them in a line. Each piece has a volume Vi. Since Xi

HDU 1087 &amp;&amp; POJ 2533(DP,最长上升子序列).

~~~~ 两道题的意思差不多,HDU上是求最长上升子序列的和,而POJ上就的是其长度. 貌似还有用二分写的nlogn的算法,不过这俩题n^2就可以过嘛.. ~~~~ 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1087 http://poj.org/problem?id=2533 ~~~~ HDU1087: #include<cstdio> #include<cstring> #include<algorithm> #

hdu 2296 aC自动机+dp(得到价值最大的字符串)

Ring Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3180    Accepted Submission(s): 1033 Problem Description For the hope of a forever love, Steven is planning to send a ring to Jane with a rom