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
题意:求一最大子矩阵(该矩阵的元素相同)的个数
思路:我们可以把这道题抽象成直方图
用l[]和r[]两个数组分别记录该点比他大的最左下标和最右下标
当在搜索下标为i的单位矩阵时,当i-1的下标的单位矩阵高度高于它时,其实我们是已经判断过下标为i-1的单位矩阵的最左端
下标的,所以这就满足dp的条件,只要把左边各个连续且大于h[i]高度的矩阵的最远下边记录下来即可。
#include <cstdio>
#include <map>
#include <iostream>
#include<cstring>
#include<bits/stdc++.h>
#define ll long long int
#define M 6
using namespace std;
inline ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
inline ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
int moth[]={,,,,,,,,,,,,};
int dir[][]={, ,, ,-, ,,-};
int dirs[][]={, ,, ,-, ,,-, -,- ,-, ,,- ,,};
const int inf=0x3f3f3f3f;
const ll mod=1e9+;
char G[][];
int l[],r[];
int h[];
int m,n;
int main(){
ios::sync_with_stdio(false);
char t[]={'a','b','c'};
char equal[][]={'b','c','x','a','c','y','a','b','w'};
while(cin>>m>>n){
for(int i=;i<=m;i++)
for(int j=;j<=n;j++)
cin>>G[i][j];
int ans=-inf;
for(int ii=m;ii>=;ii--){ //遍历每一行
for(int i=;i<;i++){ //每一行都有'a','b','c'三种情况
char temp=t[i];
memset(h,,sizeof(h));
memset(l,,sizeof(l));
memset(r,,sizeof(r));
for(int k=;k<=n;k++)
for(int j=ii;j>=;j--){
if(G[j][k]==equal[i][]||G[j][k]==equal[i][]||G[j][k]==equal[i][])
break;
h[k]++; //记录该层的高度
}
l[]=; r[n]=n;
for(int k=;k<=n;k++){
int t=k;
while(t>&&h[k]<=h[t-]){
t=l[t-];
}
l[k]=t;
}
for(int k=n-;k>=;k--){
int t=k;
while(t<n&&h[k]<=h[t+]){
t=r[t+];
}
r[k]=t;
}
for(int k=;k<=n;k++)
ans=max(ans,h[k]*(r[k]-l[k]+));
}
}
cout<<ans<<endl;
}
}