![](https://img-blog.csdnimg.cn/20201014180756923.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
动态规划
小魚兒.
天下皆白,唯我独黑!
展开
-
PAT A1068 Find More Coins (30 分)(背包问题)
1068 Find More Coins (30 分)Eva loves to collect coins from all over the universe, including some other planets like Mars. One day she visited a universal shopping mall which could accept all kinds o...原创 2019-11-14 15:25:00 · 124 阅读 · 0 评论 -
11.7 背包问题(完全背包)基础算法
#include <cstdio>//完全背包问题——物品件数无穷型 for(int i=1;i<=n;++i){ for(int v=w[i];v<=V;++v) { //有两种选择,选第i件物品,不选第i件物品 dp[i][v]=max(dp[i-1][v],dp[i][v-w[i]]+c[i]); //与01背包唯一区别是第二个参数是dp[i]...原创 2019-11-14 14:53:01 · 176 阅读 · 0 评论 -
11.7 背包问题(01背包)——测试用例
#include <cstdio>#include <algorithm>using namespace std;const int maxn =100; //物品最大件数 const int maxv=1000;//V的上限 int w[maxn],c[maxn],dp[maxv];int main(){ int n,V; scanf("%d%d",&...原创 2019-11-14 14:41:37 · 5802 阅读 · 0 评论 -
11.7 背包问题(01背包)基础算法
#include <cstdio>//01背包问题for(int i=1;i<=n;++i){ for(int v=w[i];v<=V;++v) { //有两种选择,选第i件物品,不选第i件物品 dp[i][v]=max(dp[i-1][v],dp[i-1][v-w[i]]+c[i]); }}//利用滚动数组进行空间优化for(int i=...原创 2019-11-14 14:29:54 · 135 阅读 · 0 评论 -
PAT A 1040 Longest Symmetric String (25 分)
1040 Longest Symmetric String (25 分)Given a string, you are supposed to output the length of the longest symmetric sub-string. For example, given Is PAT&TAP symmetric?, the longest symmetric sub...原创 2019-11-13 15:41:51 · 90 阅读 · 0 评论 -
11.5 最长回文字串(动态规划)
#include <cstdio>#include <cstring>const int maxn=1010;char s[maxn];int dp[maxn][maxn];int main(){ gets(s); int len =strlen(s),ans=1; memset(dp,0,sizeof(dp)); //边界 for(int i=0;...原创 2019-11-13 15:11:10 · 91 阅读 · 0 评论 -
11.4 最长公共子序列(LCS)——动态规划
#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int N=100;char a[N],b[N];int dp[N][N];int main(){ gets(a+1);//从下标1开始读入 gets(b+1); int len...原创 2019-11-13 14:37:02 · 68 阅读 · 0 评论 -
PAT A 1045 Favorite Color Stripe (30 分)(变体形式的最长不下降子序列)
1045 Favorite Color Stripe (30 分)Eva is trying to make her own color stripe out of a given one. She would like to keep only her favorite colors in her favorite order by cutting off those unwanted pi...原创 2019-11-12 15:30:43 · 151 阅读 · 0 评论 -
11.3 最长不下降子序列LIS(动态规划)
#include <cstdio>#include <algorithm>using namespace std;const int N=100;int A[N],dp[N];int main(){ int n; scanf("%d",&n); for(int i=1;i<=n;++i) { scanf("%d",&A[i])...原创 2019-11-12 14:59:54 · 102 阅读 · 0 评论 -
PAT A 1007 Maximum Subsequence Sum (25 分)(最大连续子列和——动态规划)
1007 Maximum Subsequence Sum (25 分)Given a sequence of K integers { N1, N2, ..., NK }. A continuous subsequence is defined to be { Ni, Ni+1, ..., Nj } where 1≤i≤j≤K. The Maximum Su...原创 2019-11-12 14:18:23 · 115 阅读 · 0 评论 -
11.2 Code Up 问题 A: 最大连续子序列(动态规划——含起始与结束点的确定)
问题 A: 最大连续子序列题目描述给定K个整数的序列{N1,N2,...,NK},其任意连续子序列可表示为{Ni,Ni+1,...,Nj},其中1<=i<=j<=K。最大连续子序列是所有连续子序列中元素和最大的一个,例如给定序列{-2,11,-4,13,-5,-2},其最大连续子序列为{11,-4,13},最大...原创 2019-11-10 15:33:08 · 251 阅读 · 0 评论 -
11.2 最大连续子序列和——动态规划
如何求最大连续子序列和?1、如果只有一个数,则便是其本身。2、有多个数的数组A,如-2,11,-3,13;则最大子序列和为 11-3+13=20.我们知道此序列第二个序列前的最大值为11,前三个的最大和为11-3=8,若利用dp数组存储第i个位置的最大子序列和,则dp[3]=dp[2]+A[3];由此可得状态转移方程:dp[i]=max(A[i],dp[i-1]+A[i]);参考代...原创 2019-11-10 14:39:29 · 182 阅读 · 1 评论 -
11.1 code up 问题 A: Fibonacci(动态规划递归写法)
问题 A: Fibonacci题目描述The Fibonacci Numbers{0,1,1,2,3,5,8,13,21,34,55...} are defined by the recurrence:F0=0 F1=1 Fn=Fn-1+Fn-2,n>=2Write a program to calculate the Fibonacci Numbers.输入Eac...原创 2019-11-09 15:28:28 · 101 阅读 · 0 评论 -
11.1.3 动态规划递推写法(数塔问题)
//数塔问题——从塔顶向下的路径最大值#include <cstdio>#include <algorithm>using namespace std;const int maxn=1000;int f[maxn][maxn],dp[maxn][maxn];int main(){ int n; scanf("%d",&n); for(int i...原创 2019-11-09 15:12:30 · 103 阅读 · 0 评论 -
11.1 动态规划初步(递归写法)
#include <cstdio>using namespace std;//11 动态规划 //1、坡博纳妾数列//1.1递归写法int F(int n){ if(n==0||n==1) return 1; else return F(n-1)+F(n-2);}// 1.2利用dp数组存储已经计算过的结果——记忆化搜索 int dp[maxn]...原创 2019-11-09 14:54:57 · 201 阅读 · 0 评论