HDU 2870 Largest Submatrix(最大完全子矩阵)

Largest Submatrix

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2830    Accepted Submission(s): 1368


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  

这个题就是分别枚举a,b,c;然后看谁的完全子矩阵最大,类似1506,1505;

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int h[1005][1005];
char mat[1005][1005];
int r[1005];
int l[1005];
int n,m,ans;
void fun()
{
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=m;j++)
        {
            l[j]=r[j]=j;
        }
        h[i][0]=h[i][m+1]=-1;
        for(int j=1;j<=m;j++)
        {
            while(h[i][j]<=h[i][l[j]-1])
                l[j]=l[l[j]-1];
        }
        for(int j=m;j>=1;j--)
        {
            while(h[i][j]<=h[i][r[j]+1])
                r[j]=r[r[j]+1];
        }
        for(int j=1;j<=m;j++)
            ans=max(ans,h[i][j]*(r[j]-l[j]+1));
     } 
} 
int main()
{
    char ch;
    while(~scanf("%d%d",&n,&m))
    {
        ans=0;
        getchar();
        for(int i=1;i<=n;i++)
            scanf("%s",mat[i]+1);
        for(int i=1;i<=n;i++)
        {//枚举a 
            for(int j=1;j<=m;j++)
            {
                ch=mat[i][j];
                if(ch=='a'||ch=='w'||ch=='y'||ch=='z')
                    h[i][j]=h[i-1][j]+1;
                else h[i][j]=0;
            }
        }
        
        fun();
        for(int i=1;i<=n;i++)
        {//枚举b 
            for(int j=1;j<=m;j++)
            {
                ch=mat[i][j];
                if(ch=='b'||ch=='w'||ch=='x'||ch=='z')
                    h[i][j]=h[i-1][j]+1;
                else h[i][j]=0;
            }
        }
        fun();
        
        for(int i=1;i<=n;i++)
        {//枚举c 
            for(int j=1;j<=m;j++)
            {
                ch=mat[i][j];
                if(ch=='c'||ch=='x'||ch=='y'||ch=='z')
                    h[i][j]=h[i-1][j]+1;
                else h[i][j]=0;
            }
        }
        fun();
        printf("%d\n",ans);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/bbhhtt/article/details/80375093