Largest Submatrix
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2682 Accepted Submission(s): 1316
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.
Sample Input
2 4abcwwxyz
Sample Output
3
关于找最大子矩阵:
点击打开链接
遍历找最大子矩阵即可
代码:
#include<bits/stdc++.h>
char t[1002][1002],p[1002][1002];
int n,m,d[1002][1002];
using namespace std;
int bfs(char s)
{
memset(d,0,sizeof(d));
int i,j;
for(i=0; i<n; i++)
for(j=0; j<m; j++)
if(p[i][j]==s)
{
if(j==0)d[i][j]=1;
else d[i][j]=d[i][j-1]+1;
}
int ma=0;
for(i=0; i<n; i++)
{
for(j=0; j<m; j++)
{
if(d[i][j]!=0)
{
int L=0;
for(int k=i-1; k>=0; k--)
{
if(d[k][j]>=d[i][j])L++;
else break;
}
for(int k=i+1;k<n;k++)
{
if(d[k][j]>=d[i][j])L++;
else break;
}
ma=max(ma,(L+1)*d[i][j]);
}
}
}
return ma;
}
void cmpt()
{
for(int i=0; i<n; i++)
for(int j=0; j<m; j++)
p[i][j]=t[i][j];
}
void ttc(char s1,char s2,char s3,char s4)
{
cmpt();
for(int i=0; i<n; i++)
for(int j=0; j<m; j++)
if(p[i][j]==s2||p[i][j]==s3||p[i][j]==s4)
p[i][j]=s1;
}
int main()
{
while(~scanf("%d%d",&n,&m))
{
int i,j;
for(i=0; i<n; i++)
scanf("%s",&t[i]);
cmpt();
int ma=bfs('w');
cmpt();
ma=max(ma,bfs('x'));
cmpt();
ma=max(ma,bfs('y'));
cmpt();
ma=max(ma,bfs('z'));
ttc('a','w','y','z');
ma=max(ma,bfs('a'));
ttc('b','w','x','z');
ma=max(ma,bfs('b'));
ttc('c','x','y','z');
ma=max(ma,bfs('c'));
printf("%d\n",ma);
}
return 0;
}