题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2870
题目描述:
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
题目大意:
给你一个m行n列的字符矩阵,其中w可以被a和b代替,x可以被b和c代替,y可以被a和c代替,z可以被a、b、c代替,叫你在这个矩阵中找出一个面积最大的由相同字母组成的矩阵的面积
题目分析:
由于只有三种字母,a、b和c,那么我们可以把这三种情况都算出来求最大值,依次把w、x、y、z当中能变成a的都换成a,来求得全a矩阵的最大面积,同理,求出b和c,三者的较大者就是我们要求的最大矩阵面积
AC代码:
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int a[1005][1005],b[1005][1005],c[1005][1005];
int l[1005],r[1005];
int m,n;
int Max,tmp;
char ch;
int main()
{
while(scanf("%d%d",&m,&n)!=EOF)
{
memset(a,0,sizeof(a));
memset(b,0,sizeof(b));
memset(c,0,sizeof(c));
Max=-0xfffffff;
for(int i=1;i<=m;i++)
{
getchar();
for(int j=1;j<=n;j++)
{
scanf("%c",&ch);
if(ch=='a'||ch=='w'||ch=='y'||ch=='z')
a[i][j]++;
if(ch=='b'||ch=='w'||ch=='x'||ch=='z')
b[i][j]++;
if(ch=='c'||ch=='x'||ch=='y'||ch=='z')
c[i][j]++;
if(a[i][j])
a[i][j]+=a[i-1][j];
if(b[i][j])
b[i][j]+=b[i-1][j];
if(c[i][j])
c[i][j]+=c[i-1][j];
}
memset(l,0,sizeof(l));
memset(r,0,sizeof(r));
l[1]=1;
r[n]=n;
for(int j=2;j<=n;j++)
{
tmp=j;
if(a[i][j]==0)
continue;
while(tmp>1&&a[i][j]<=a[i][tmp-1])
tmp=l[tmp-1];
l[j]=tmp;
}
for(int j=n-1;j>=1;j--)
{
tmp=j;
if(a[i][j]==0)
continue;
while(tmp<n&&a[i][j]<=a[i][tmp+1])
tmp=r[tmp+1];
r[j]=tmp;
}
for(int j=1;j<=n;j++)
{
Max=max(Max,a[i][j]*(r[j]-l[j]+1));
}
memset(l,0,sizeof(l));
memset(r,0,sizeof(r));
l[1]=1;
r[n]=n;
for(int j=2;j<=n;j++)
{
tmp=j;
if(b[i][j]==0)
continue;
while(tmp>1&&b[i][j]<=b[i][tmp-1])
tmp=l[tmp-1];
l[j]=tmp;
}
for(int j=n-1;j>=1;j--)
{
tmp=j;
if(b[i][j]==0)
continue;
while(tmp<n&&b[i][j]<=b[i][tmp+1])
tmp=r[tmp+1];
r[j]=tmp;
}
for(int j=1;j<=n;j++)
{
Max=max(Max,b[i][j]*(r[j]-l[j]+1));
}
memset(l,0,sizeof(l));
memset(r,0,sizeof(r));
l[1]=1;
r[n]=n;
for(int j=2;j<=n;j++)
{
tmp=j;
if(c[i][j]==0)
continue;
while(tmp>1&&c[i][j]<=c[i][tmp-1])
tmp=l[tmp-1];
l[j]=tmp;
}
for(int j=n-1;j>=1;j--)
{
tmp=j;
if(c[i][j]==0)
continue;
while(tmp<n&&c[i][j]<=c[i][tmp+1])
tmp=r[tmp+1];
r[j]=tmp;
}
for(int j=1;j<=n;j++)
{
Max=max(Max,c[i][j]*(r[j]-l[j]+1));
}
}
cout<<Max<<endl;
}
return 0;
}
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2870
题目描述:
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
题目大意:
给你一个m行n列的字符矩阵,其中w可以被a和b代替,x可以被b和c代替,y可以被a和c代替,z可以被a、b、c代替,叫你在这个矩阵中找出一个面积最大的由相同字母组成的矩阵的面积
题目分析:
由于只有三种字母,a、b和c,那么我们可以把这三种情况都算出来求最大值,依次把w、x、y、z当中能变成a的都换成a,来求得全a矩阵的最大面积,同理,求出b和c,三者的较大者就是我们要求的最大矩阵面积
AC代码: