Largest Submatrix
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2301 Accepted Submission(s): 1113
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
题意:给你一个矩阵,这个矩阵由a b c w x y z组成,w可以变为a b,x可以变为b c, y可以变为a c,z可以变为a b c,问你经过变换后可以得到的最大的矩阵面积是多少
思路:hdu1505和hdu1506的加强版,把矩阵分别分为a b c用1505的方法做,然后三者比较得出最大值。我写完了a的,bc就完全贴了a的代码。写起来还是很快的。
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
using namespace std;
char r[1050][1050];
int a[1050][1050];
int rright[1050][1050];
int lleft[1050][1050];
int main(){
int n,m;
while(scanf("%d%d",&n,&m) == 2){
for(int i = 1; i <= n; i++)
scanf("%s",&r[i][1]); //!!输入
int maxx=0;
//!!对a的处理
for(int j = 1; j <= m; j++) //lie
for(int i = 1; i <= n; i++) //hang
if(r[i][j]!='a' && r[i][j]!='w' && r[i][j]!='y' && r[i][j]!='z')
a[i][j] = 0;
else
a[i][j] = a[i-1][j]+1;
for(int i = 1; i <= n; i++){//hang
lleft[i][1] = 1;
rright[i][m] = m;
for(int j = 2; j <= m; j++){
int k = j;
while(k>1 && a[i][j]<=a[i][k-1])
k = lleft[i][k-1];
lleft[i][j] = k;
}
for(int j = m-1; j >= 1; j--){
int k = j;
while(k<m && a[i][j]<=a[i][k+1])
k = rright[i][k+1];
rright[i][j] = k;
}
}
for(int i = 1; i <= n; i++)
for(int j = 1; j <= m; j++)
maxx = max(maxx,a[i][j]*(rright[i][j]-lleft[i][j]+1));
//!!对b的处理
for(int j = 1; j <= m; j++) //lie
for(int i = 1; i <= n; i++) //hang
if(r[i][j]!='b' && r[i][j]!='w' && r[i][j]!='x' && r[i][j]!='z')
a[i][j] = 0;
else
a[i][j] = a[i-1][j]+1;
for(int i = 1; i <= n; i++){//hang
lleft[i][1] = 1;
rright[i][m] = m;
for(int j = 2; j <= m; j++){
int k = j;
while(k>1 && a[i][j]<=a[i][k-1])
k = lleft[i][k-1];
lleft[i][j] = k;
}
for(int j = m-1; j >= 1; j--){
int k = j;
while(k<m && a[i][j]<=a[i][k+1])
k = rright[i][k+1];
rright[i][j] = k;
}
}
for(int i = 1; i <= n; i++)
for(int j = 1; j <= m; j++)
maxx = max(maxx,a[i][j]*(rright[i][j]-lleft[i][j]+1));
//!!对c的处理
for(int j = 1; j <= m; j++) //lie
for(int i = 1; i <= n; i++) //hang
if(r[i][j]!='c' && r[i][j]!='x' && r[i][j]!='y' && r[i][j]!='z')
a[i][j] = 0;
else
a[i][j] = a[i-1][j]+1;
for(int i = 1; i <= n; i++){//hang
lleft[i][1] = 1;
rright[i][m] = m;
for(int j = 2; j <= m; j++){
int k = j;
while(k>1 && a[i][j]<=a[i][k-1])
k = lleft[i][k-1];
lleft[i][j] = k;
}
for(int j = m-1; j >= 1; j--){
int k = j;
while(k<m && a[i][j]<=a[i][k+1])
k = rright[i][k+1];
rright[i][j] = k;
}
}
for(int i = 1; i <= n; i++)
for(int j = 1; j <= m; j++)
maxx = max(maxx,a[i][j]*(rright[i][j]-lleft[i][j]+1));
cout << maxx <<endl;
}
}