HDU 2870 (最大0 1 矩阵面积)
2014-07-31 10:10
411 查看
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2870
这个题是 1505,1506的变形,建议先去看看1506,那是基础,解释都在代码中,感觉关键还是掌握下面我强调的难点处(对我还说是难点)
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
Sample Output
//感觉还是很神奇的方法,我也是看别人好久才懂得,
这个题是 1505,1506的变形,建议先去看看1506,那是基础,解释都在代码中,感觉关键还是掌握下面我强调的难点处(对我还说是难点)
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
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; #define N 1005 int h ,le ,ri ; char a ; int main() { int n,m,i,j; while(scanf("%d%d",&n,&m)!=EOF) { getchar(); char c; int ans=0,t; for(i=1;i<=n;i++) { for(j=1;j<=m;j++) scanf("%c",&a[i][j]); getchar(); } for(t=1;t<=3;t++) //分三种情况,a,(b,c),当可以把转化a的字符全部转化成a(不是真的变成a), //计算高度,求得最大矩形面积(方法详解在上篇 1505) { //然后比较这三种情况的最大值 memset(h,0,sizeof(h)); if(t==1) { for(i=1;i<=n;i++) for(j=1;j<=m;j++) if(a[i][j]=='a'||a[i][j]=='w'||a[i][j]=='y'||a[i][j]=='z') h[i][j]=h[i-1][j]+1; else h[i][j]=0; } else if(t==2) { for(i=1;i<=n;i++) for(j=1;j<=m;j++) if(a[i][j]=='b'||a[i][j]=='w'||a[i][j]=='x'||a[i][j]=='z') h[i][j]=h[i-1][j]+1; else h[i][j]=0; } else { for(i=1;i<=n;i++) for(j=1;j<=m;j++) if(a[i][j]=='c'||a[i][j]=='x'||a[i][j]=='y'||a[i][j]=='z') h[i][j]=h[i-1][j]+1; else h[i][j]=0; } for(i=1;i<=n;i++) //以上高度确定,下面是算面积 { for(j=1;j<=m;j++) { le[j]=j; while(le[j]>1&&h[i][le[j]-1]>=h[i][j]) //此处是难点,不懂看 上篇1505 le[j]=le[le[j]-1]; } for(j=m;j>=1;j--) { ri[j]=j; while(ri[j]<m&&h[i][ri[j]+1]>=h[i][j]) //此处是难点,不懂看 上篇1505 ri[j]=ri[ri[j]+1]; } for(j==1;j<=m;j++) ans=max(ans,(ri[j]-le[j]+1)*h[i][j]); } } printf("%d\n",ans); } return 0; }
//感觉还是很神奇的方法,我也是看别人好久才懂得,
相关文章推荐
- HDU 1505 1506 2830 2870求最大矩阵面积 DP
- HDU 2870 Largest Submatrix(最大子矩阵面积)
- 【DP求最大子矩阵面积】hdu 1506
- 暑期dp46道(39)--HDOJ 2870 最大子矩阵面积...
- Largest Submatrix-最大子矩阵-HDU-2870
- HDU 2870 Largest Submatrix (求最大子矩阵变形,可变值)枚举每种情况
- hdu 1506 最大矩阵面积 __int64 AC long long TLE
- 暑期dp46道(9)——HDU 1505City Game 求最大子矩阵面积
- HDU 2870 Largest Submatrix(最大完全子矩阵之不可移动列)
- HDU2870_Largest Submatrix【最大完全子矩阵】
- 【最大子矩阵】hdu 2870
- HDU 1505 City Game【矩阵的最大面积】
- HDU-2870 Largest Submatrix (线性dp 最大01矩阵)(2009 Multi-University Training Contest 7 )
- HDU 1506 Largest Rectangle in a Histogram【矩阵最大面积】
- HDU 1505 City Game (单调栈+最大子矩阵面积)
- HDU 2870【DP_求最大矩阵】
- HDU 2870 Largest Submatrix (最大子矩形面积)
- HDU1505、2870,2830(最大的10矩阵面积问题)
- 动态规划-面积最大的全1子矩阵
- 面积最大的全1子矩阵