hdu2870之DP
来源:互联网 发布:mac上怎么做u盘启动盘 编辑:程序博客网 时间:2024/06/01 22:36
Largest Submatrix
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1071 Accepted Submission(s): 525
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 4abcwwxyz
Sample Output
3
分析:对于第i行第j列,判断从上到下连接的高度,然后判断这个点能延伸的最左端和最右端,延伸的条件是左端/右端字母相等且高度大于等于这个点
#include<iostream>#include<cstdio>#include<cstdlib>#include<cstring>#include<string>#include<queue>#include<algorithm>#include<map>#include<iomanip>#define INF 99999999using namespace std;const int MAX=1000+10;int Left[MAX],Right[MAX];//记录比当前行在位置i可连接字母高于等于hight[i]的最左端和最右端int hight[2][MAX];//hight[i]记录每一列从上到下到当前行的高(即连在一起相同字母的个数)int n,m;char s[MAX][MAX],Map[26][3];//Map映射a,b,c,w,x,y,z的转换结果 int MaxMatrix(){int sum=0;for(int k=0;k<3;++k){//转换成哪种字母 for(int i=0;i<n;++i){for(int j=0;j<m;++j){//求hight if(i == 0)hight[0][j]=1;else hight[i%2][j]=(Map[s[i][j]-'a'][k] == Map[s[i-1][j]-'a'][k]) ? hight[(i-1)%2][j]+1 : 1;Left[j]=Right[j]=j;}for(int j=1;j<m;++j){//求Left while(Left[j]-1>=0 && hight[i%2][Left[j]-1]>=hight[i%2][j] && Map[s[i][Left[j]-1]-'a'][k] == Map[s[i][Left[j]]-'a'][k])Left[j]=Left[Left[j]-1];}for(int j=m-2;j>=0;--j){//求Rightwhile(Right[j]+1<m && hight[i%2][Right[j]+1]>=hight[i%2][j] && Map[s[i][Right[j]+1]-'a'][k] == Map[s[i][Right[j]]-'a'][k])Right[j]=Right[Right[j]+1];}for(int j=0;j<m;++j){sum=max(sum,(Right[j]-Left[j]+1)*hight[i%2][j]);}}}return sum;}int main(){for(int i=0;i<3;++i)for(int j=0;j<3;++j)Map[i][j]=i+'a';Map['w'-'a'][0]='a',Map['w'-'a'][1]='b',Map['w'-'a'][2]='w';Map['x'-'a'][0]='x',Map['x'-'a'][1]='b',Map['x'-'a'][2]='c';Map['y'-'a'][0]='a',Map['y'-'a'][1]='y',Map['y'-'a'][2]='c';Map['z'-'a'][0]='a',Map['z'-'a'][1]='b',Map['z'-'a'][2]='c';while(~scanf("%d%d",&n,&m)){for(int i=0;i<n;++i)scanf("%s",s+i);printf("%d\n",MaxMatrix());}return 0;}
- hdu2870之DP
- 【DP】HDU2870 Largest Submatrix
- hdu2870 Largest Submatrix--DP
- hdu2870 Largest Submatrix 【dp】
- hdu2870暴力或者dp优化
- hdu2870 Largest Submatrix(DP)
- hdu2870
- hdu2870 单调队列
- HDU2870 Largest Submatrix
- hdu2870 Largest Submatrix 1506 1505
- hdu2830可变换位置的hdu2870变型
- 扫描线 hdu 1506 hdu1505 hdu2870
- HDU2870(和HDU1505 1506相同)
- DP专辑之线性DP
- DP专题之概率DP
- hdu2870子串长度子串最小数
- HDU2870----DP_最大子矩阵系列(同HDU1505,1506)
- DP之Brackets Sequence
- U3D需要用到的数学基础知识
- 分类汇总统计的SQL语句 .
- Tiny6410_buttons.c 按键驱动程序
- hdu 1505 hdu 1506 hdu 2870 综合解答
- 对一组字符串排序,使子串总在父串之后
- hdu2870之DP
- VMware_VCP5.0 虚拟化认证原厂培训中文教材 PPT
- HDU 4565 So Easy!
- Codeforces Round #188 (Div. 1) / 317A Perfect Pair(数学&优化)
- poj-1200 Crazy Search
- Wing IDE 4.1使用笔记一修正一下框框字体显示不了中文
- HDOJ 1789
- struts2-常用功能
- Java String.Format() 方法及参数说明