动态规划

一.钢条切割

 

 1 #include<iostream>
 2 using namespace std;
 3 
 4
 5 
 6 int Cut_Rod(int A[], int length, int S[])
 7 {
 8     int *r = new int[length + 1];
 9     for (int i = 0; i < 11; i++)
10     {
11         r[i] = 0;
12     }
13     r[0] = 0;
14     S[0] = 1;
15     if (length <= 0 || A == NULL || S == NULL)
16     {
17         return r[0];
18     }
19 
20     for (int i = 1; i <= length; i++)
21     {
22         for (int j = 1; j <= i; j++)
23         {
24             if (r[i] < A[j - 1] + r[i - j])
25             {
26                 r[i] = A[j - 1] + r[i - j];
27                 S[i - 1] = j;
28             }
29         }
30     }
31     return r[10];
32 }
33 
34 int main()
35 {
36     int A[] = { 1, 5, 8, 9, 10, 17, 17, 20, 24, 30};
37     int S[10];
38 
39     cout << Cut_Rod(A, 10, S) << endl;
40     for (int i = 0; i < 10; i++)
41     {
42         cout << S[i] << " ";
43     }
44     cout << endl;
45     return 0;
46 }

 

 

二.矩阵链乘法

 

 1 #include<iostream>
 2 using namespace std;
 3 
 4 inline int  min(int a, int b)
 5 {
 6     if (a > b)
 7     {
 8         return b;
 9     }
10     else
11     {
12         return a;
13     }
14 }
15 
16 int Matrix_Chain_Order(int matrix[], int low, int high, int **m, int **s)
17 {
18     for (int i = low; i <= high; i++)
19     {
20         m[i][i] = 0;
21     }
22     for (int len = 2; len <= high - low + 1; len++)
23     {
24         for (int i = low; i <= high - len + 1; i++)
25         {
26             int j = i + len - 1;
27             for (int k = i; k < j; k++)
28             {
29                 if (k == i)
30                 {
31                     m[i][j] = m[i][k] + m[k + 1][j] + matrix[i * 2 - 2] * matrix[k * 2 - 1] * matrix[j * 2 - 1];
32                     s[i][j] = k;
33                 }
34                 else if (m[i][j] > m[i][k] + m[k + 1][j] + matrix[i * 2 - 2] * matrix[k * 2 - 1] * matrix[j * 2 - 1])
35                 {
36                     m[i][j] = m[i][k] + m[k + 1][j] + matrix[i * 2 - 2] * matrix[k * 2 - 1] * matrix[j * 2 - 1];
37                     s[i][j] = k;
38                 }
39             }
40         }
41     }
42     return m[low][high];
43 }
44 
45 void Print_Chain(int **s, int low, int high)
46 {
47     if (low == high)
48     {
49         cout << "A" << low;
50     }
51     else
52     {
53         cout << "(";
54         Print_Chain(s, low, s[low][high]);
55         Print_Chain(s, s[low][high] + 1, high);
56         cout << ")";
57     }
58 }
59 
60 int main()
61 {
62     int A[] = { 30, 35, 35, 15, 15, 5, 5, 10, 10, 20, 20, 25};
63     int **m = new int*[7];
64     for (int i = 0; i < 7; i++)
65     {
66         m[i] = new int[7];
67     }
68 
69     int **s = new int*[7];
70     for (int i = 0; i < 7; i++)
71     {
72         s[i] = new int[7];
73     }
74     cout << Matrix_Chain_Order(A, 1, 6, m, s) << endl;
75     Print_Chain(s, 1, 6);
76     return 0;
77 }

 

 

三.最大子段和

 

 1 #include<iostream>
 2 using namespace std;
 3 
 4 void MaxSubSum(int A[], int length, int sum[], int num[])
 5 {
 6     sum[0] = A[0];
 7     num[0] = 0;
 8     for (int i = 1; i < length; i++)
 9     {
10         if (sum[i - 1] > 0)
11         {
12             sum[i] = A[i] + sum[i - 1];
13             num[i] = num[i - 1];
14         }
15         else
16         {
17             sum[i] = A[i];
18             num[i] = i;
19         }
20     }
21 }
22 
23 int main()
24 {
25     int A[] = { -2, 11, -4, 13, -5, -2 };
26     int sum[6], num[6];
27     MaxSubSum(A, 6, sum, num);
28     int max = 0;
29     for (int i = 0; i < 6; i++)
30     {
31         if (sum[max] < sum[i])
32         {
33             max = i;
34         }
35     }
36     cout << sum[max] << endl;
37     for (int i = num[max]; i <= max; i++)
38     {
39         cout << A[i] << " ";
40     }
41     cout << endl;
42     return 0;
43 }

 

 

四.LCS

 1 #include<iostream>
 2 #include<string>
 3 #include<stack>
 4 using namespace std;
 5 
 6 int LCS(string x, string y, int **b)
 7 {
 8     int i = x.length();
 9     int j = y.length();
10     int *C = new int[i];
11     for (int t = 0; t < i; t++)
12     {
13         C[t] = 0;
14     }
15     for (int m = 0; m < j; m++)
16     {
17         int num = 0;
18         for (int n = 0; n < i; n++)
19         {
20             int preNum = C[n];
21             if (y[m] == x[n])
22             {
23                 C[n] = num + 1;
24                 b[m][n] = 0;
25             }
26             else
27             {
28                 if (n != 0)
29                 {
30                     if (C[n - 1] > C[n])
31                     {
32                         C[n] = C[n - 1];
33                         b[m][n] = -1;
34                     }
35                     else
36                     {
37                         b[m][n] = 1;
38                     }
39                 }
40                 else
41                 {
42                     b[m][n] = 1;
43                 }
44             }
45             num = preNum;
46         }
47     }
48     return C[i - 1];
49 }
50 
51 void PrintLCS(string str, int i, int j, int **b)
52 {
53     stack<char> s;
54     while (j >= 0 && i >= 0)
55     {
56         if (b[j][i] == 0)
57         {
58             s.push(str[i]);
59             i--; j--;
60             continue;
61         }
62         if (b[j][i] == -1)
63         {
64             i--;
65             continue;
66         }
67         if (b[j][i] == 1)
68         {
69             j--;
70             continue;
71         }
72     }
73     while (!s.empty())
74     {
75         cout << s.top() << " ";
76         s.pop();
77     }
78     cout << endl;
79 }
80 
81 int main()
82 {
83     string x = "BDCABA";
84     string y = "ABCBDAB";
85     int **b = new int*[7];
86     for (int i = 0; i < 7; i++)
87     {
88         b[i] = new int[6];
89     }
90     cout << LCS(x, y, b) << endl; //4
91     PrintLCS(x, 5, 6, b); // BCBA
92     return 0;
93 }

 

五.0-1背包

 

  1 #include<iostream>
  2 using namespace std;
  3 
  4 inline int min(int a, int b)
  5 {
  6     if (a > b)
  7     {
  8         return b;
  9     }
 10     else
 11     {
 12         return a;
 13     }
 14 }
 15 
 16 inline int max(int a, int b)
 17 {
 18     if (a > b)
 19     {
 20         return a;
 21     }
 22     else
 23     {
 24         return b;
 25     }
 26 }
 27 
 28 int KnapSack(int w[], int v[], int c, int n, int**m)
 29 {
 30     if (w == NULL || v == NULL || c <= 0 || n <= 0 || m == NULL)
 31     {
 32         return 0;
 33     }
 34     int JMax = min(w[n - 1] - 1, c);
 35     for (int j = 0; j <= JMax; j++)
 36     {
 37         m[n - 1][j] = 0;
 38     }
 39     for (int j = w[n - 1]; j <= c; j++)
 40     {
 41         m[n - 1][j] = v[n - 1];
 42     }
 43     for (int i = n - 2; i >= 1; i--)
 44     {
 45         JMax = min(w[i] - 1, c);
 46         for (int j = 0; j <= JMax; j++)
 47         {
 48             m[i][j] = m[i + 1][j];
 49         }
 50         for (int j = w[i]; j <= c; j++)
 51         {
 52             m[i][j] = max(m[i + 1][j], m[i + 1][j - w[i]] + v[i]);
 53         }
 54     }
 55     if (w[0] > c)
 56     {
 57         m[0][c] = m[1][c];
 58     }
 59     else
 60     {
 61         m[0][c] = max(m[1][c], m[1][c - w[0]] + v[0]);
 62     }
 63     return m[0][c];
 64 }
 65 
 66 void TraceBack(int w[], int c, int n, int **m)
 67 {
 68     int *x = new int[n];
 69     for (int i = 0; i < n - 1; i++)
 70     {
 71         if (m[i][c] == m[i + 1][c])
 72         {
 73             x[i] = 0;
 74         }
 75         else
 76         {
 77             x[i] = 1;
 78             c -= w[i];
 79         }
 80     }
 81     if (m[n - 1][c] != 0)
 82     {
 83         x[n - 1] = 1;
 84     }
 85     else
 86     {
 87         x[n - 1] = 0;
 88     }
 89     for (int i = 0; i < n; i++)
 90     {
 91         if (x[i] == 1)
 92         {
 93             cout << i + 1 << " ";
 94         }
 95     }
 96     cout << endl;
 97 }
 98 
 99 int main()
100 {
101     int w[] = { 4, 5, 6 };
102     int v[] = { 3, 4, 5 };
103     int c = 10;
104     int **m = new int*[3];
105     for (int i = 0; i < 3; i++)
106     {
107         m[i] = new int[11];
108     }
109     cout << KnapSack(w, v, c, 3, m) << endl; // 8
110     TraceBack(w, c, 3, m); // 1 3
111 
112     return 0;
113 }

 

posted @ 2016-03-14 19:00  早杰  阅读(184)  评论(0编辑  收藏  举报