【DP+预处理|最长连续子序列】HDU-2870 Largest Submatrix

69 篇文章 0 订阅

Largest Submatrix

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 '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
 
————————————————————————————————————————————————————————
题意:可以将n×m的矩阵中的w、x、y、z按照规则替换成a、b、c找到最大的一个子矩阵,使其全部由a或b或c构成。
思路:这道题和HDU-1505是一样的,只不过有三种子矩阵而已:
代码如下:
/**
 * ID: j.sure.1
 * PROG:
 * LANG: C++
 */
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <ctime>
#include <cmath>
#include <stack>
#include <queue>
#include <vector>
#include <map>
#include <set>
#include <string>
#include <climits>
#include <iostream>
#define Mem(f, x) memset(f, x, sizeof(f))
#define Pb push_back
#define LL long long
using namespace std;
const int INF = 0x3f3f3f3f;
const double eps = 1e-8;
/****************************************/
const int N = 1111;
int n, m, mat[N][N];
int u[3][N][N];
int l[3][N], r[3][N];
//w -> a, b
//x -> b, c
//y -> a, c
//z -> a, b, c

int main()
{
#ifdef J_Sure
    freopen("000.in", "r", stdin);
    //freopen("999.out", "w", stdout);
#endif
    while(~scanf("%d%d", &n, &m)) {
        char str[N];
        for(int i = 1; i <= n; i++) {
            str[0] = '*';
            scanf("%s", &str[1]);
            for(int j = 1; j <= m; j++) {
                if(str[j] == 'a' || str[j] == 'y' || str[j] == 'z' || str[j] == 'w') {
                    u[0][i][j] = u[0][i-1][j] + 1;
                }
                else u[0][i][j] = 0;
                if(str[j] == 'b' || str[j] == 'w' || str[j] == 'x' || str[j] == 'z') {
                    u[1][i][j] = u[1][i-1][j] + 1;
                }
                else u[1][i][j] = 0;
                if(str[j] == 'c' || str[j] == 'x' || str[j] == 'y' || str[j] == 'z') {
                    u[2][i][j] = u[2][i-1][j] + 1;
                }
                else u[2][i][j] = 0;
            }
        }
        int best[3] = {}, ans = 0;
        for(int k = 0; k < 3; k++) {
            for(int i = 1; i <= n; i++) {
                u[k][i][0] = u[k][i][m+1] = -1;
                for(int j = 1; j <= m; j++) {
                    l[k][j] = r[k][j] = j;
                    while(u[k][i][j] <= u[k][i][l[k][j]-1]) {
                        l[k][j] = l[k][l[k][j]-1];
                    }
                }
                for(int j = m; j >= 1; j--) {
                    while(u[k][i][j] <= u[k][i][r[k][j]+1]) {
                        r[k][j] = r[k][r[k][j]+1];
                    }
                }
                for(int j = 1; j <= m; j++) {
                    int cur = (r[k][j] - l[k][j] + 1)*u[k][i][j];
                    best[k] = max(best[k], cur);
                }
            }
            ans = max(ans, best[k]);
        }
        printf("%d\n", ans);
    }
    return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值