[关闭]
@rg070836rg 2015-11-19T22:00:07.000000Z 字数 2700 阅读 1450

算法概论实验六

算法概论实验


  1. 实验六
  2. 实验目的与要求:掌握动态规划方法的基本思想与设计策略。
  3. 1.最长公共子序列问题
  4. 【问题描述】
  5. 给定两个字符串XY,设计一个动态规划算法,求出这两个字符串的最长公共子序列,并输出该子序列。
  6. 若仅要求求出两个字符串的最长公共子序列的长度值,为节省存储空间,采用“滚动数组”方式实现动态规划算法。
  7. 20-1背包问题
  8. 【问题描述】
  9. 给定n种物品和一背包。物品i的重量是wi,其价值为vi,背包的容量为W(假定物品重量与背包容量值均为整数),应如何选择装入背包中的物品,使得装入背包中物品的总价值最大?设计一个动态规划算法,求解背包问题。

The LCS Question

01.jpg-385.6kB

  1. // ConsoleApplication2.cpp : 定义控制台应用程序的入口点。
  2. // VS2013 CPP CODE
  3. //
  4. #include "stdafx.h"
  5. #include<iostream>
  6. using namespace std;
  7. void PrintLcsPath(int ** b, char * x, int m, int n)
  8. {
  9. if (m == 0 | n == 0)
  10. return;
  11. if (b[m][n] == 1)
  12. {
  13. PrintLcsPath(b, x, m - 1, n - 1);
  14. cout << x[m - 1];
  15. }
  16. else if (b[m][n] == 2)
  17. PrintLcsPath(b, x, m, n - 1);
  18. else
  19. PrintLcsPath(b, x, m - 1, n);
  20. }
  21. void print(int ** a, int m, int n)
  22. {
  23. for (int i = 0; i < m + 1; i++)
  24. {
  25. for (int j = 0; j < n + 1; j++)
  26. cout << a[i][j] << " ";
  27. cout << endl;
  28. }
  29. }
  30. int LcsLength(char *x, char *y, int m, int n)
  31. {
  32. //创建一个 m+1 * n+1 用于存储LCS
  33. int **a = new int *[m + 1];
  34. for (int i = 0; i < m + 1; i++)
  35. a[i] = new int[n + 1];
  36. //创建一个 m+1 * n+1 用于存储状态
  37. //来自于对角线 1 来自于左侧2 来自于上方3
  38. int **b = new int *[m + 1];
  39. for (int i = 0; i < m + 1; i++)
  40. b[i] = new int[n + 1];
  41. //base case
  42. for (int i = 0; i < m + 1; i++)
  43. a[i][0] = 0;
  44. for (int i = 0; i < n + 1; i++)
  45. a[0][i] = 0;
  46. //for
  47. for (int i =1; i < m + 1;i++)
  48. {
  49. for (int j =1; j < n + 1;j++)
  50. {
  51. if (x[i-1]==y[j-1])
  52. {
  53. a[i][j] = a[i - 1][j - 1] + 1;
  54. b[i][j] = 1;
  55. }
  56. else
  57. {
  58. if (a[i-1][j] <= a[i][j-1])
  59. {
  60. a[i][j] = a[i][j - 1];
  61. b[i][j] = 2;
  62. }
  63. else
  64. {
  65. a[i][j] = a[i - 1][j];
  66. b[i][j] = 3;
  67. }
  68. }
  69. }
  70. }
  71. /*print(a, m, n);
  72. cout << endl;
  73. print(b, m, n);*/
  74. cout << "LCS为:";
  75. PrintLcsPath(b, x, m, n);
  76. cout << endl;
  77. return a[m][n];
  78. }
  79. int main()
  80. {
  81. char x[] = "12312312qwe12312";
  82. char y[] = "abqweqw123e123123qwcbdab";
  83. int m = strlen(x);
  84. int n = strlen(y);
  85. cout << "LCS的长度为:" << LcsLength(x, y, m, n) << endl;
  86. }

0-1背包

02.jpg-273.7kB

  1. // N给定n种物品和一背包。物品i的重量是wi,其价值为vi,背包的容量为W(假定物品重量与背包容量值均为整数),应如何选择装入背包中的物品,使得装入背包中物品的总价值最大?设计一个动态规划算法,求解背包问题。
  2. //
  3. #include "stdafx.h"
  4. #include <iostream>
  5. using namespace std;
  6. #define W 50
  7. void print(int ** a, int m, int n)
  8. {
  9. for (int i = 0; i < m + 1; i++)
  10. {
  11. for (int j = 0; j < n + 1; j++)
  12. cout << a[i][j] << " ";
  13. cout << endl;
  14. }
  15. }
  16. void Trackback(int *weight, int n, int w,bool *p,int **a)
  17. {
  18. if (n==0 || w==0)
  19. return;
  20. if (a[w][n]==a[w][n-1])//若和左边的一致,说明没有选最后一个
  21. {
  22. p[n - 1] = false;
  23. Trackback(weight, n - 1, w, p, a);
  24. }
  25. else
  26. {
  27. p[n - 1] = true;
  28. Trackback(weight, n - 1, w-weight[n-1], p, a);
  29. }
  30. }
  31. int getMaxValue(int w, int n, int *price, int * weight )
  32. {
  33. //创建一个 w+1 * n+1 的二维表
  34. int **a = new int *[w + 1];
  35. for (int i = 0; i < w + 1;i++)
  36. {
  37. a[i] = new int[n + 1];
  38. }
  39. //创建一个数组 记录货物是否取的状态
  40. bool *p = new bool[w];
  41. memset(p, false, sizeof(p));
  42. //base case
  43. for (int i = 0; i < w + 1; i++)
  44. a[i][0] = 0;
  45. for (int i = 0; i < n + 1; i++)
  46. a[0][i] = 0;
  47. //for
  48. for (int i = 1; i < w + 1;i++)
  49. {
  50. for (int j = 1; j < n + 1;j++)
  51. {
  52. if (i<weight[j-1])//填写a[i][j],若当前背包重量小于物品,则不装
  53. {
  54. a[i][j] = a[i][j - 1];
  55. }
  56. else
  57. {
  58. if (a[i][j-1] <= a[i-weight[j-1]][j-1]+price[j-1])
  59. {
  60. a[i][j] = a[i - weight[j - 1]][j - 1] + price[j - 1] ;
  61. }
  62. else
  63. a[i][j] = a[i][j - 1];
  64. }
  65. }
  66. }
  67. //print(a,w,n);
  68. Trackback(weight, n, w, p, a);
  69. cout << "从左到右是否取件为:";
  70. for (int i = 0; i < n; i++)
  71. cout << p[i] << " ";
  72. cout << endl;
  73. return a[w][n];
  74. }
  75. int main()
  76. {
  77. //int price[] = { 1, 2, 3, 4, 7 };
  78. //int weight[] = { 2, 4, 5, 6, 210 };
  79. int price[] = { 60, 100, 120 };
  80. int weight[] = { 10, 20, 30 };
  81. cout << "背包问题的解是:"<<getMaxValue(W, 5, price, weight) << endl;
  82. return 0;
  83. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注