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
这道题直接把可替换的分别全换为a,b,c然后计算分别由a,b,c最大子矩阵的面积,此处求法可参照http://blog.csdn.net/ffgcc/article/details/78491268,这里我们多次对每一行求解即可.
#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
const int N=1e3+10;
char app[N][N];
int arr[N][N];
int dp[N][N];
int num[N];
int l[N];
int r[N];
int n,m;
int fi()
{
memset(dp,0,sizeof dp);
int mx=0;
for(int i=1;i<=n;i++)
{
for(int j=0;j<m;j++)
{
if(arr[i][j]==1)
dp[i][j]=dp[i-1][j]+1;
else
dp[i][j]=0;
num[j]=dp[i][j];
}
l[0]=0;r[m-1]=m-1;
for(int j=1;j<m;j++)
{
int tt=j;
while(tt>0&&num[j]<=num[tt-1]) tt=l[tt-1];
l[j]=tt;
}
for(int j=m-1;j>=0;j--)
{
int tt=j;
while(tt<m-1&&num[j]<=num[tt+1])tt=r[tt+1];
r[j]=tt;
}
for(int j=0;j<m;j++)
mx=max((r[j]-l[j]+1)*num[j],mx);
}
return mx;
}
int main()
{
ios::sync_with_stdio(false);
while(cin>>n>>m&&n)
{
for(int i=1;i<=n;i++)
cin>>app[i];
memset(arr,0,sizeof arr);
int mx=0;
for(int i=1;i<=n;i++)
{
for(int j=0;j<m;j++)
if(app[i][j]=='w'||app[i][j]=='y'||app[i][j]=='z'||app[i][j]=='a')
arr[i][j]=1;
}
//cout<<fi()<<endl;
mx=max(mx,fi());
memset(arr,0,sizeof arr);
for(int i=1;i<=n;i++)
{
for(int j=0;j<m;j++)
if(app[i][j]=='w'||app[i][j]=='x'||app[i][j]=='z'||app[i][j]=='b')
arr[i][j]=1;
}
mx=max(mx,fi());
//cout<<fi()<<endl;
memset(arr,0,sizeof arr);
for(int i=1;i<=n;i++)
{
for(int j=0;j<m;j++)
if(app[i][j]=='x'||app[i][j]=='y'||app[i][j]=='z'||app[i][j]=='c')
arr[i][j]=1;
}
//cout<<fi()<<endl;
mx=max(mx,fi());
cout<<mx<<endl;
}
return 0;
}