您的位置:首页 > 其它

HDU 2870 最大子矩阵,联系1505, 1506

2012-02-23 22:03 363 查看
题目意思:w x y z 可以转化,求最大子矩阵。
思路:枚举后转换为 hdu 1505 hdu 1506 的情况。
可以转化为a的有:w,y, z
可以转化为b的有:w,x, z
可以转换为c的有:x, y, z
所以
a, w, y, z一组
b, w, x, z一组
c, x, y, z一组
分别dp

#include <cstdlib>
#include <iostream>
using namespace std;
const int maxn=1005;
int h[maxn][maxn];
int l[maxn],r[maxn];
char str[maxn][maxn];
int n,m,i,j;
int ans;

int max(int a,int b)
{
if(a>b)
return a;
else
return b;
}
void DP()
{
for(i=1;i<=n;++i)
{
for(j=1;j<=m;++j)
l[j]=r[j]=j;
h[i][0]=h[i][m+1]=-1;
for(j=1;j<=m;++j)
while(h[i][j]<=h[i][l[j]-1])
l[j]=l[l[j]-1];
for(j=m;j>=1;j--)
while(h[i][j]<=h[i][r[j]+1])
r[j]=r[r[j]+1];
for(j=1;j<=m;++j)
ans=max(ans,h[i][j]*(r[j]-l[j]+1));
}
}
int main()
{
memset(h[0],0,sizeof(h[0]));
while(scanf("%d%d",&n,&m)!=EOF)
{
ans=0;
for(i=1;i<=n;++i)
scanf("%s",str[i]+1);
for(i=1;i<=n;++i)
for(j=1;j<=m;++j)
if(str[i][j]=='a'||str[i][j]=='w'||str[i][j]=='y'||str[i][j]=='z')
h[i][j]=h[i-1][j]+1;
else
h[i][j]=0;
DP();
for(i=1;i<=n;++i)
for(j=1;j<=m;++j)
if(str[i][j]=='b'||str[i][j]=='w'||str[i][j]=='x'||str[i][j]=='z')
h[i][j]=h[i-1][j]+1;
else
h[i][j]=0;
DP();
for(i=1;i<=n;++i)
for(j=1;j<=m;++j)
if(str[i][j]=='c'||str[i][j]=='x'||str[i][j]=='y'||str[i][j]=='z')
h[i][j]=h[i-1][j]+1;
else
h[i][j]=0;
DP();
printf("%d\n",ans);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: