Largest Submatrix
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2828 Accepted Submission(s): 1367
Problem 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.
hdu 2870
Sample Input
2 4
abcw
wxyz
Sample Output
3
题意:给你N行M列,由字母 'a','b','c','w','x','y','z'组成的矩阵, 'w' 可以转换成 'a' or 'b', c; 'x'可以转换成'b' or 'c'; 'y' 可以转换成 'a' or 'c'; 'z' 可以转换成 'a', 'b' or 'c'。
问你由‘a’组成或者由‘b’组成或者由‘c’组成的矩形的最大面积是多少;
思路:枚举a,b,c; 预处理全部转换成当前枚举的元素,对每个元素的求其深度,求最大子矩阵的面积同hdu 1505。
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
char p[1005][1005];
int a[1005][1005];
int r[1005],l[1005];
int n,m;
int maxn;
void fun(int a[][1005])
{
for(int i=1;i<=n;i++)
{
l[1]=1;r[m]=m;
a[i][0]=a[i][m+1]=-1;
for(int j=2;j<=m;j++)
{
l[j]=j;
while(a[i][j]<=a[i][l[j]-1])
l[j]=l[l[j]-1];
}
for(int j=m-1;j>=1;j--)
{
r[j]=j;
while(a[i][j]<=a[i][r[j]+1])
r[j]=r[r[j]+1];
}
for(int j=1;j<=m;j++)
maxn=max(maxn,a[i][j]*(r[j]-l[j]+1));
}
}
int main()
{
while(~scanf("%d%d",&n,&m))
{
memset(a,0,sizeof a);
for(int i=1;i<=n;i++)
{
scanf("%s",p[i]+1);
}
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
if(p[i][j]=='z'||p[i][j]=='a'||p[i][j]=='y'||p[i][j]=='w')
a[i][j]=a[i-1][j]+1;
else
a[i][j]=0;
}
}
maxn=0;
fun(a);
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
if(p[i][j]=='z'||p[i][j]=='b'||p[i][j]=='z'||p[i][j]=='w')
a[i][j]=a[i-1][j]+1;
else
a[i][j]=0;
}
}
fun(a);
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
if(p[i][j]=='z'||p[i][j]=='c'||p[i][j]=='y'||p[i][j]=='x')
a[i][j]=a[i-1][j]+1;
else
a[i][j]=0;
}
}
fun(a);
printf("%d\n",maxn);
}
return 0;
}