hdu 2870

//枚举a,b,c转化为1505的问题,一次ac 
#include<iostream>
using namespace std;

char mat[1005][1005];
char comp[1005][1005];

int m,n,i,j;
//根据哪个字符寻找
int up[1005][1005];
int height[1005][1005];
int ll[1005];
int rr[1005];

void changeTox(char tmp){
    if(tmp=='a'){
        for(i=1;i<=m;i++)
            for(j=1;j<=n;j++){
                up[i][j]=i;
                if(mat[i][j]=='y'||mat[i][j]=='z'||mat[i][j]=='w'){
                    comp[i][j]='a';
                }else{
                    comp[i][j]=mat[i][j];
                }                  
            }
    }else if(tmp=='b'){
          for(i=1;i<=m;i++)
            for(j=1;j<=n;j++){
                up[i][j]=i;
                if(mat[i][j]=='x'||mat[i][j]=='z'||mat[i][j]=='w'){
                    comp[i][j]='b';
                }else{
                    comp[i][j]=mat[i][j];
                }                  
            }
    }else if(tmp=='c'){
         for(i=1;i<=m;i++)
            for(j=1;j<=n;j++){
                up[i][j]=i;
                if(mat[i][j]=='y'||mat[i][j]=='z'||mat[i][j]=='x'){
                    comp[i][j]='c';
                }else{
                    comp[i][j]=mat[i][j];
                }                  
            }
    }
}

void back(){
    for(i=1;i<=m;i++)
        for(j=1;j<=n;j++)
            comp[i][j]=mat[i][j];     
}



int findMax(char ch){
    changeTox(ch);
    for(i=1;i<=m;i++)
        for(j=1;j<=n;j++){
            if(comp[i][j]==ch){
                while(comp[up[i][j]-1][j]==comp[i][j])
                    up[i][j]=up[up[i][j]-1][j];
                height[i][j]=i-up[i][j]+1;
            }else{
                height[i][j]=0;
            }
        }
    //计算出高度之后转化为hdu1506
    for(i=1;i<=n;i++){
        ll[i]=i;
        rr[i]=i;
    }
    for(i=1;i<=m;i++)
        height[i][0]=height[i][n+1]=-1;
    
    int max=-1;
    for(i=1;i<=m;i++){
        for(j=1;j<=n;j++){
            if(comp[i][j]==ch){
                while(height[i][j]<=height[i][ll[j]-1])
                    ll[j]=ll[ll[j]-1];
            }
        }
        
        for(j=n;j>=1;j--){
           if(comp[i][j]==ch){
               while(height[i][j]<=height[i][rr[j]+1])
                   rr[j]=rr[rr[j]+1];
           }
        }
        
        for(j=1;j<=n;j++){
            if(comp[i][j]==ch){
                if((rr[j]-ll[j]+1)*height[i][j]>max)
                    max=(rr[j]-ll[j]+1)*height[i][j];
            }
        }
        for(j=1;j<=n;j++){
            ll[j]=j;
            rr[j]=j;
        }
        for(j=1;j<=m;j++)
            height[j][0]=height[j][n+1]=-1;
    }
    back();
    return max;  
}



int main(){
    while(scanf("%d%d",&m,&n)!=EOF){
        for(i=1;i<=m;i++)
           scanf("%s",mat[i]+1);
        int res=-1,temp;
        if(res<(temp=findMax('a')))
            res=temp;

        if(res<(temp=findMax('b')))
            res=temp;

        if(res<(temp=findMax('c')))
            res=temp;

        cout<<res<<endl;
    }    
    return 0;
}

你可能感兴趣的:(hdu 2870)