题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=2870
题目意思:
给你一个字符矩阵
里面除了a,b,c之外,还有别的字符可以转换成a,b,c
然后求转换后的最大相同字母组成的子矩阵
暴力分别枚举成a,b,c
然后用1505的方法就可以A掉
我求的时候,傻逼一样的把高给乘掉了
不会1505的可以参见:http://blog.youkuaiyun.com/dr5459/article/details/9116599
代码:
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn = 1010;
char a[maxn][maxn];
char b[maxn][maxn];
int h[maxn][maxn];
int l[maxn];
int r[maxn];
int main()
{
int m,n;
while(~scanf("%d%d",&m,&n))
{
getchar();
for(int i=1;i<=m;i++)
{
for(int j=1;j<=n;j++)
scanf("%c",&a[i][j]);
getchar();
}
int ans = -1;
//先枚举a
for(int i=1;i<=m;i++)
{
for(int j=1;j<=n;j++)
{
if(a[i][j]=='a'||a[i][j]=='b'||a[i][j]=='c'||a[i][j]=='x')
b[i][j] = a[i][j];
else
b[i][j] = 'a';
}
}
memset(h,0,sizeof(h));
for(int i=1;i<=m;i++)
{
for(int j=1;j<=n;j++)
{
if(b[i][j]=='a')
h[i][j] = h[i-1][j]+1;
}
}
for(int i=1;i<=m;i++)
{
h[i][0]=-1;
h[i][n+1]=-1;
l[1]=1;
r[n]=n;
for(int j=2;j<=n;j++)
{
l[j]=j;
while(h[i][l[j]-1] >= h[i][j])
l[j] = l[l[j]-1];
}
for(int j=n-1;j>=1;j--)
{
r[j]=j;
while(h[i][r[j]+1] >= h[i][j])
{
r[j] = r[r[j]+1];
}
}
int tmp;
for(int j=1;j<=n;j++)
{
tmp=(r[j]-l[j]+1)*h[i][j];
if(ans<tmp)
ans=tmp;
}
}
//再枚举b
for(int i=1;i<=m;i++)
{
for(int j=1;j<=n;j++)
{
if(a[i][j]=='a'||a[i][j]=='b'||a[i][j]=='c'||a[i][j]=='y')
b[i][j] = a[i][j];
else
b[i][j] = 'b';
}
}
memset(h,0,sizeof(h));
for(int i=1;i<=m;i++)
{
for(int j=1;j<=n;j++)
{
if(b[i][j]=='b')
h[i][j] = h[i-1][j]+1;
}
}
for(int i=1;i<=m;i++)
{
h[i][0]=-1;
h[i][n+1]=-1;
l[1]=1;
r[n]=n;
for(int j=2;j<=n;j++)
{
l[j]=j;
while(h[i][l[j]-1] >= h[i][j])
l[j] = l[l[j]-1];
}
for(int j=n-1;j>=1;j--)
{
r[j]=j;
while(h[i][r[j]+1] >= h[i][j])
{
r[j] = r[r[j]+1];
}
}
int tmp;
for(int j=1;j<=n;j++)
{
tmp=(r[j]-l[j]+1)*h[i][j];
if(ans<tmp)
ans=tmp;
}
}
for(int i=1;i<=m;i++)
{
for(int j=1;j<=n;j++)
{
if(a[i][j]=='a'||a[i][j]=='b'||a[i][j]=='c'||a[i][j]=='w')
b[i][j] = a[i][j];
else
b[i][j] = 'c';
}
}
memset(h,0,sizeof(h));
for(int i=1;i<=m;i++)
{
for(int j=1;j<=n;j++)
{
if(b[i][j]=='c')
h[i][j] = h[i-1][j]+1;
}
}
for(int i=1;i<=m;i++)
{
h[i][0]=-1;
h[i][n+1]=-1;
l[1]=1;
r[n]=n;
for(int j=2;j<=n;j++)
{
l[j]=j;
while(h[i][l[j]-1] >= h[i][j])
l[j] = l[l[j]-1];
}
for(int j=n-1;j>=1;j--)
{
r[j]=j;
while(h[i][r[j]+1] >= h[i][j])
{
r[j] = r[r[j]+1];
}
}
int tmp;
for(int j=1;j<=n;j++)
{
tmp=(r[j]-l[j]+1)*h[i][j];
if(ans<tmp)
ans=tmp;
}
}
printf("%d\n",ans);
}
return 0;
}