![](https://img-blog.csdnimg.cn/20190927151053287.png?x-oss-process=image/resize,m_fixed,h_224,w_224)
动态规划(DP)
总结一些常见的动态规划模型供参考
Kylincommander
这个作者很懒,什么都没留下…
展开
-
Leetcode 10. 正则表达式匹配
本文仅仅记录自身代码的一步步演变过程,需要详细解答请前往官网查看一、 纯递归求解class Solution {public: bool Match(string &s,string &p,int start_s,int start_p){ int size_s = s.size(),size_p = p.size(),i = start_s, j =...原创 2020-05-02 11:46:05 · 108 阅读 · 0 评论 -
PAT 甲级 1103 Integer Factorization (30分) 动态规划解法 (附DFS解法参考代码)
一、题目描述The K−P factorization of a positive integer N is to write N as the sum of the P-th power of K positive integers. You are supposed to write a program to find the K−P factorization of N for any p...原创 2020-03-17 11:20:19 · 173 阅读 · 0 评论 -
动态规划模型——背包问题
一、01背包1.问题描述:有n件物品,每件物品重量w[i],价值c[i]。现有一个容量为V的背包,问如何选取物品放入背包,使得背包内物品的总价值最大。其中每种物品都只有一件。二、完全背包1.问题描述:有n件物品,每件物品重量w[i],价值c[i]。现有一个容量为V的背包,问如何选取物品放入背包,使得背包内物品的总价值最大。其中每种物品都无穷件。...原创 2020-03-13 12:14:33 · 534 阅读 · 0 评论 -
动态规划常见思路总结
当题目与序列或者字符串(记为A)有关时,可以考虑把状态设计成两种形式:1.令dp[i]表示以A[i]结尾(或开头)的XXXX。2.令dp[i][j]表示A[i]至A[j]区间的XXXX。XXXX为题目要求一般地,对于一个问题一维的dp不满足状态的无后效性,则需要考虑对状态进行升维,然后对其中的每一维采取下面的某一个表达:1.恰好为i2.前i在每一维的含义设置完毕后,dp数组的含义就可...原创 2020-03-12 19:27:49 · 211 阅读 · 0 评论 -
动态规划模型——最长回文子串
一、我的解法dp[i]表示以input[i]结尾的最长回文串长度。dp[i]可能是由dp[i-1]加上input[i]形成的,也可能是在dp[i-1]的基础上前面后面各加一个元素构成的。为了避免原序列前物元素可补的情况需先判断原序列起始位置。#include<cstdio>#include<iostream>#include<string>#inclu...原创 2020-03-12 16:02:37 · 86 阅读 · 0 评论 -
动态规划模型——最长公共子序列(LCS)
给定两个序列,求一个最长的子序列(子序列可以不连续)//最长公共子序列做法#include<cstdio>#include<vector>using namespace std;int LCS(vector<int>& A,vector<int>& B){//A为模板串,A,B下标从1开始 vector<vect...原创 2020-03-12 12:46:58 · 157 阅读 · 0 评论 -
动态规划模型——最长不下降子序列(LIS)
#include<cstdio>#include<vector>using namespace std;int LIS(vector<int>& input){ vector<int> dp;//dp[i]:以input[i]结尾的最长不下降子序列长度 int maxlength=-1; for(int i=0;i<inp...原创 2020-03-11 19:55:26 · 152 阅读 · 0 评论