题意:让你求的是由同一字母组成的最大子矩阵,w可以变成a或者b,x可以变成b或者c,y可以变成a或者c,z可以变成a或者b或者c。
分析:这是hdu 1506、hdu 1505的加强版,具体的分析看我的博客:http://www.cnblogs.com/jiangjing/p/3221423.html
代码实现:
#include<stdio.h> #include<string.h> #include<stdlib.h> #include<iostream> using namespace std; char str[8]={'a','b','c','w','x','y','z'}; char map[1005][1005]; int bian[7][3]={{1,0,0},{0,1,0},{0,0,1},{1,1,0},{0,1,1},{1,0,1},{1,1,1}}; int shuliang[1005][1005][5]; int l[1005],r[1005]; int n,m; int solve(int i) { int j,k,t,max=-1; for(k=0;k<3;k++) { l[1]=1;r[m]=m; for(j=2;j<=m;j++) { t=j; while(t>1&&shuliang[i][j][k]<=shuliang[i][t-1][k]) t=l[t-1]; l[j]=t; } for(j=m-1;j>=1;j--) { t=j; while(t<m&&shuliang[i][j][k]<=shuliang[i][t+1][k]) t=r[t+1]; r[j]=t; } for(j=1;j<=m;j++) if(max<(r[j]-l[j]+1)*shuliang[i][j][k]) max=(r[j]-l[j]+1)*shuliang[i][j][k]; } return max; } int main() { int i,j,k,t,res,temp; while(scanf("%d%d",&n,&m)!=EOF) { res=-1; getchar(); for(i=1;i<=n;i++) scanf("%s",map[i]+1); memset(shuliang,0,sizeof(shuliang)); for(i=1;i<=n;i++) { for(j=1;j<=m;j++) { for(k=0;k<7;k++) { if(map[i][j]==str[k]) { for(t=0;t<3;t++) { if(bian[k][t]==1) shuliang[i][j][t]=shuliang[i-1][j][t]+1; else shuliang[i][j][t]=0; } break; } } } temp=solve(i); if(temp>res) res=temp; } printf("%d ",res); } return 0; }