目录
一)什么是动态规划
动态规划的本质:对问题的 状态的定义 与 状态的转移;
它也是通过拆分问题,划分子块,但是这里的子块(状态)后续的状态会用到。
【
然后我们的状态转移,见得最多的就是利用数组进行状态转移;
但是,千千万万,别认为就一定要用到数组进行状态的定义与转移!!!
只要你解决问题的核心:拆分问题!定义状态!进行状态的转移!这就是动态规划~
】
二)实战一下
1)Floyd算法
见代码:
#include<iostream>
#define null NULL
using namespace std;
class Graph { //采用邻接矩阵法,存储图的关系,这里仅表示图顶点的index,所以 class vnode与class enode未给出
int** matrix;
int** A;
int** path;
int length;
public:
Graph() { }
Graph(int num) {
length = num;
matrix = new int* [num]();
path = new int*[num]();
for (int i = 0; i < num; i++) {
matrix[i] = new int[num]();
path[i] = new int[num]();
}
for (int i = 0; i < length; i++) {
for (int j = 0; j < length; j++) {
if (i != j) {
matrix[i][j] = 99;
path[i][j] = -1;
}
else {
matrix[i][j] = 0;
path[i][j] = i;
}
}
}
A = matrix;
}
void add_edge(int onepoint, int twopoint, int weight) {//weight有 权值 之意
matrix[onepoint][twopoint] = weight;
path[onepoint][twopoint] = onepoint;
//matrix[twopoint][onepoint] = weight;
}
void Floyd(int start,int end) {
for (int k = 0; k < length; k++) {
for (int i = 0; i < length; i++) {
for (int j = 0; j < length; j++) {
if (A[i][j] > A[i][k] + A[k][j]) {
A[i][j] = A[i][k] + A[k][j];
path[i][j] = path[k][j];
}
}
}
}
for (int i = 0; i < length; i++) {
for (int j = 0; j < length; j++) {
cout << path[i][j] << " ";
}
putchar(10);
}
int i = start, j = end;
while (j!=start) {
cout << path[i][j] << " ";
j = path[i][j];
}
}
};
int main() {
Graph* obj = new Graph(6);
obj->add_edge(0, 1, 1);
obj->add_edge(0, 4, 25);
obj->add_edge(0, 2, 5);
obj->add_edge(1, 3, 2);
obj->add_edge(2, 3, 3);
obj->add_edge(2, 4, 10);
obj->add_edge(2, 5, 1);
obj->add_edge(3, 5, 2);
obj->add_edge(4, 5, 5);
obj->add_edge(5, 4, 1);
obj->Floyd(0,4);
return 0;
}
2)最长递增子序列
以a=[1,5,3,6,7]为例:
在a[1]这里最长为2;而a[2]这里最长为2;你可以很明显看到a[2]的状态有a[0]决定的。递推式,大概就有思路了吧:
#include<iostream>
#include<vector>
using namespace std;
class Solution {
public:
int lengthOfLIS(vector<int>& nums) {
int *length=new int[nums.size()]();
length[0] = 1;
int max_length = length[0];
for (int i = 1; i < nums.size(); i++) {
for (int j = i - 1; j >= 0; j--) {
if (nums.at(i) > nums.at(j)) {
length[i] = length[i] > (length[j] + 1) ? length[i] : (length[j] + 1);
}
else if (nums.at(i) == nums.at(j)) {
length[i] = length[i]>length[j]?length[i]:length[j];
break;
}
if (j == 0 && length[i] == 0) {
length[i] = 1;
}
}
max_length = max_length > length[i] ? max_length : length[i];
}
return max_length;
}
};
int main() {
vector<int>nums = { 2,7,0,2,3,6,2,3,4 };
Solution* obj = new Solution();
cout << obj->lengthOfLIS(nums) << endl;
return 0;
}
3)括号生成(leetcode 22题)
这个就没有数组进行状态转移了,但是状态转移仍在:
你后面生成的括号,要取决于已经加入的括号,如:如果已经添加了“(”,那之后你就还可以添加“(”或者“)”;这里还需要用到栈,你每添加一个右括号,要看栈顶是否是左括号……(基本就这么分析,就大差不差了。)
#include<iostream>
#include<stack>
#include<vector>
#include<string>
#define null NULL
using namespace std;
class Solution {
stack<char>left;//左括号栈,用来确定已经输入的左括号的数量;你也可以自己进行计数不用栈
stack<char>right;//右括号栈
stack<char>str_stack;//符号栈,用来进行左右括号匹配
vector<string>destination;
public:
void left_push(string str,int n) {
str_stack.push('(');
left.push('(');
str += "(";
recrusion(str, n);
left.pop();
str_stack.pop();
}
void right_push(string str, int n) {
if (!str_stack.empty()&&str_stack.top() == '(') {
str_stack.pop();
}
else {
str_stack.push(')');
}
right.push(')');
str += ")";
recrusion(str, n);
right.pop();
str_stack.push('(');
}
void recrusion(string str,int n) {
if (str.size() == 2 * static_cast<unsigned long long>(n)) {
destination.push_back(str);
}
else {
if (str_stack.empty()) {
left_push(str, n);
}
else{
if (left.size() != n) {
left_push(str, n);
}
if (!str_stack.empty()&&right.size() != n) {
right_push(str, n);
}
}
}
}
vector<string> generateParenthesis(int n) {
recrusion("",n);
return destination;
}
};
int main() {
Solution* obj = new Solution();
obj->generateParenthesis(3);
return 0;
}