POJ 1050 To the Max(最长子串DP)
来源:互联网 发布:计算机基础与c语言 编辑:程序博客网 时间:2024/06/06 04:00
刚看到这题目确实蒙了!之前没学过动态规划,于是在网上搜了搜,理解了一点,一下是解题思路:
题目大意是:读入一个nj阶数组,数组元素有正有负。比如:
0 -2 -7 0
9 2 -6 2
-4 1 -4 1
-1 8 0 -2
然后从里面截取出一个矩形,使得矩形内的元素之和最大,我们可以看到,截取出
9 2
-4 1
-1 8
这个矩形,和为15。输出结果为15!
思路:假设只有 一列数,求这一列数的最大子串和。例子:2 3 -1 -6 7 8,公式:sum[i]=max(0,sum[i-1])+a[i] ;若有多列,我们可以把它压缩成一列,在压缩过程中,只要确定从哪一行开始压缩,就把各列相加,逐渐往右走 ,dp[i]代表到第i个子串的长度,temp[i]表示当确定哪一行后,第i列的数之和,代码如下
#include <iostream>#include <stdio.h>#include <cstring>using namespace std;int main(){ int nums[101][105],dp[105]={0}; int n,i,j,k,max=0; cin>>n; for(i=1;i<=n;i++) { for(j=1;j<=n;j++) cin>>nums[i][j]; } for(i=1;i<=n;i++)//表示从第几行开始 { int temp[105]={0}; for(j=i;j<=n;j++)//表示从第几列结束 { for(k=1;k<=n;k++)//表示第几列 { temp[k]+=nums[j][k]; if(temp[k]+dp[k-1]>0) dp[k]=temp[k]+dp[k-1]; else dp[k]=0; if(dp[k]>max) max=dp[k]; } } } cout<<max<<endl; return 0;}
- POJ 1050 To the Max(最长子串DP)
- poj - 1050 - To the Max(dp)
- POJ 1050 to the max(dp)
- poj 1050 To the Max (dp)
- 【POJ 1050】To the Max(dp)
- POJ 1050 To the Max(DP)
- Poj 1050 To the Max (最大子矩阵 DP)
- [POJ 1050] To the Max DP+最大子矩阵和
- poj 1050 To the Max dp 最大子矩阵
- POJ 1050 To the Max && POJ 2479Maximum sum(DP最大连续子段和)
- poj to the max(最大子矩阵和) 1050(dp大法)
- hdu 1081 To The Max ****poj 1050(最大子矩阵和)DP
- POJ 1050 To the Max(DP,最大子矩阵和)
- poj To the Max 1050 (二维最大子矩阵 DP)
- POJ 1050 To the Max DP
- POJ 1050 To the Max 经典DP
- POJ 1050 To the Max DP
- poj 1050To the Max dp+暴力
- Zend Studio7中的debug
- C#三种WCF网络客户端的实现方式
- Ganglia 和 Nagios,第 2 部分: 使用 Nagios 监视企业集群
- Image uImage zImage U-Boot的区别
- svn 回滚
- POJ 1050 To the Max(最长子串DP)
- lhgdialog 关闭窗口
- poi 读取excel 的乱码
- Win7 eclipse Hadoop Failed to set permissions of path
- flex 时间类型转换
- Android的Audio 系统
- mime type 与 dataflavor
- * 定义验证各种格式类型的正则表达式对象
- mysql导入和导出数据库