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 ‘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

Recommend

gaojie   |   We have carefully selected several similar
problems for you:  2830 2577 2845 1069 1058

指定字母可以全部变为某指定字母

问相同字母组成的最大子矩阵

hdu 1505 加强版

#include<cstdio>
#include<cstring>
#include<algorithm>
#define N 1001
using namespace std;
int n,m,k,minn,ans;
char s[N+2][N+2],t[N+2][N+2];
int l[N],r[N];
int a[N],b[N];
int q[N],tmp[N],head,tail;
void change(char w,char x,char y,char z)
{
    for(int i=1;i<=n;i++)
     for(int j=1;j<=m;j++)
      if(s[i][j]==x || s[i][j]==y || s[i][j]==z) t[i][j]=w;
      else t[i][j]=s[i][j];
}
void monotonous(int *c,int *d)
{
    int h=1;
    while(h<=m && !c[h]) h++;
    if(h>m) return;
    q[0]=c[h]; tmp[0]=h;
    head=0; tail=1;
    for(int i=h+1;i<=m;i++)
    {
        if(!c[i]) while(head<tail) d[tmp[head++]]=i-1;
        else if(head==tail) q[tail]=c[i],tmp[tail++]=i;
        else
        {
            while(head<tail && c[i]<q[tail-1]) d[tmp[--tail]]=i-1;
            q[tail]=c[i];
            tmp[tail++]=i;
        }
    }
    while(head<tail) d[tmp[head++]]=tmp[tail-1];
}
void solve(char x)
{
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=m;j++)
        {
            if(t[i][j]!=x) a[j]=b[m-j+1]=0;
            else
            {
                if(t[i][j]==t[i-1][j]) a[j]++,b[m-j+1]++;
                 else a[j]=b[m-j+1]=1;
            }
        }
        monotonous(a,r);
        monotonous(b,l);
        for(int j=1;j<=m;j++) tmp[j]=l[j];
        for(int j=1;j<=m;j++) l[m-j+1]=m-tmp[j]+1;
        for(int j=1;j<=m;j++) ans=max(ans,(r[j]-l[j]+1)*a[j]);
    }
}
int main()
{
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        ans=0;
        for(int i=1;i<=n;i++) scanf("%s",s[i]+1);
        change(‘a‘,‘w‘,‘y‘,‘z‘); solve(‘a‘);
        change(‘b‘,‘w‘,‘x‘,‘z‘); solve(‘b‘);
        change(‘c‘,‘x‘,‘y‘,‘z‘); solve(‘c‘);
        printf("%d\n",ans);
    }
}
时间: 2024-10-05 05:00:22

hdu 2870 Largest Submatrix的相关文章

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] 面

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(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

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

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 1505 Largest Rectangle in a Histogram &amp;&amp; HDU 1506 City Game(动态规划)

1506题意:给你连续的直方图(底边边长为1),求连续的矩阵面积. 对每个直方图,分别向左向右进行扩展. #include<cstdio> #include<stdlib.h> #include<string.h> #include<string> #include<map> #include<cmath> #include<iostream> #include <queue> #include <sta

POJ 3494 Largest Submatrix of All 1’s

POJ 2796 Feel Good HDU 1506 Largest Rectangle in a Histogram 和这两题一样的方法. #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> using namespace std; const int maxn=2000+10; int a[maxn]; int L[maxn],R[maxn]; int m,n;

POJ 3494 Largest Submatrix of All 1&#39;s 栈的运用 好题

Language: Default Largest Submatrix of All 1’s Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 5185   Accepted: 1950 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

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的思想边搜边保存. 另外注