hdu2870 Largest Submatrix 最大完全子矩阵 模板题

2019-04-13 22:18发布

Largest Submatrix

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2750    Accepted Submission(s): 1344


Problem DescriptionNow 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? 
InputThe 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. 
OutputFor each test case, output one line containing the number of elements of the largest submatrix of all same letters. 
Sample Input2 4abcwwxyz 
Sample Output3 
Source2009 Multi-University Training Contest 7 - Host by FZU 
Recommend/* 能转成a的全转成a,然后不能不是a的位置是0,是a的位置是1。 然后再把 全是 b和c 的求出来 求最大值就好了。 是1505题的多种情况版,建议先做1505题。 */ #include using namespace std; const int maxn = 1005; int h[maxn][maxn]; int l[maxn],r[maxn]; char s[maxn][maxn]; int n,m,ans=0; void getArea() { for(int i = 1;i<=n;i++) { for(int j = 1;j<=m;j++) l[j]=r[j]=j; h[i][0]=h[i][m+1]=-1; for(int j = 1;j<=m;j++) while(h[i][j]<=h[i][l[j]-1]) l[j]=l[l[j]-1]; for(int j = m;j>=1;j--) while(h[i][j]<=h[i][r[j]+1]) r[j]=r[r[j]+1]; for(int j = 1;j<=m;j++) ans = max(ans,h[i][j]*(r[j]-l[j]+1)); } } int main() { while(scanf("%d%d",&n,&m)!=EOF) { ans=0; for(int i = 1;i<=n;i++) scanf("%s",s[i]+1); //注意整个地址 for(int i = 1;i<=n;i++) for(int j =1;j<=m;j++) if(s[i][j]=='a'||s[i][j]=='w'||s[i][j]=='y'||s[i][j]=='z') h[i][j]=h[i-1][j]+1; else h[i][j]=0; getArea(); for(int i = 1;i<=n;i++) for(int j = 1;j<=m;j++) if(s[i][j]=='b'||s[i][j]=='w'||s[i][j]=='x'||s[i][j]=='z') h[i][j]=h[i-1][j]+1; else h[i][j]=0; getArea(); for(int i = 1;i<=n;i++) for(int j = 1;j<=m;j++) if(s[i][j]=='c'||s[i][j]=='x'||s[i][j]=='y'||s[i][j]=='z') h[i][j]=h[i-1][j]+1; else h[i][j]=0; getArea(); printf("%d ",ans); } }