Largest Submatrix
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2299 Accepted Submission(s): 1112
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 4
abcw
wxyz
Sample Output
3
HDU1505 1506的加强版
戳这里看这题的简易版
上面两道会了,这题水过
代码
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<math.h>
using namespace std;
const int maxn=1005;
int DP_A[maxn][maxn];
int DP_B[maxn][maxn];
int DP_C[maxn][maxn];
int Left[maxn];
int Right[maxn];
char str[maxn];
int N,M;
void init_DP(int i,int j,char c)
{
if(c=='a')
{
DP_A[i][j]=DP_A[i-1][j]+1;
DP_B[i][j]=0;
DP_C[i][j]=0;
}
else if(c=='b')
{
DP_A[i][j]=0;
DP_B[i][j]=DP_B[i-1][j]+1;
DP_C[i][j]=0;
}
else if(c=='c')
{
DP_A[i][j]=0;
DP_B[i][j]=0;
DP_C[i][j]=DP_C[i-1][j]+1;
}
else if(c=='w')
{
DP_A[i][j]=DP_A[i-1][j]+1;
DP_B[i][j]=DP_B[i-1][j]+1;
DP_C[i][j]=0;
}
else if(c=='x')
{
DP_A[i][j]=0;
DP_B[i][j]=DP_B[i-1][j]+1;
DP_C[i][j]=DP_C[i-1][j]+1;
}
else if(c=='y')
{
DP_A[i][j]=DP_A[i-1][j]+1;
DP_B[i][j]=0;
DP_C[i][j]=DP_C[i-1][j]+1;
}
else if(c=='z')
{
DP_A[i][j]=DP_A[i-1][j]+1;
DP_B[i][j]=DP_B[i-1][j]+1;
DP_C[i][j]=DP_C[i-1][j]+1;
}
}
int Update_result(int line)//计算以第i行为底的结果
{
int result=0;
//以下只是A
Left[1]=1;
for(int i=2; i<=M; i++)
{
int j=i;
while(j>=1&&DP_A[line][j-1]>=DP_A[line][i])
j=Left[j-1];
Left[i]=j;
}
Right[M]=M;
for(int i=M-1; i>=1; i--)
{
int j=i;
while(j<M&&DP_A[line][j+1]>=DP_A[line][i])//注意这里j<M边界处理
j=Right[j+1];
Right[i]=j;
}
for(int i=1; i<=M; i++)
{
if(result<(Right[i]-Left[i]+1)*DP_A[line][i])
result=(Right[i]-Left[i]+1)*DP_A[line][i];
}
//以下还有B
Left[1]=1;
for(int i=2; i<=M; i++)
{
int j=i;
while(j>=1&&DP_B[line][j-1]>=DP_B[line][i])
j=Left[j-1];
Left[i]=j;
}
Right[M]=M;
for(int i=M-1; i>=1; i--)
{
int j=i;
while(j<M&&DP_B[line][j+1]>=DP_B[line][i])
j=Right[j+1];
Right[i]=j;
}
for(int i=1; i<=M; i++)
{
if(result<(Right[i]-Left[i]+1)*DP_B[line][i])
result=(Right[i]-Left[i]+1)*DP_B[line][i];
}
//以下还有C
Left[1]=1;
for(int i=2; i<=M; i++)
{
int j=i;
while(j>=1&&DP_C[line][j-1]>=DP_C[line][i])
j=Left[j-1];
Left[i]=j;
}
Right[M]=M;
for(int i=M-1; i>=1; i--)
{
int j=i;
while(j<M&&DP_C[line][j+1]>=DP_C[line][i])
j=Right[j+1];
Right[i]=j;
}
for(int i=1; i<=M; i++)
{
if(result<(Right[i]-Left[i]+1)*DP_C[line][i])
result=(Right[i]-Left[i]+1)*DP_C[line][i];
}
return result;
}
int main()
{
while(~scanf("%d%d",&N,&M))
{
for(int i=0; i<=N+1; i++)
{
for(int j=0; j<=M+1; j++)
{
DP_A[i][j]=0;
DP_B[i][j]=0;
DP_C[i][j]=0;
}
}
int result=0;
for(int i=1; i<=N; i++)
{
scanf("%s",str+1);
for(int j=1; j<=M; j++)
{
init_DP(i,j,str[j]);
str[j]='\0';//用完就初始化
}
result=max(result,Update_result(i));
}
printf("%d\n",result);
}
return 0;
}