1. 程式人生 > 其它 >動態規劃 ---- 最長不下降子序列(Longest Increasing Sequence, LIS)

動態規劃 ---- 最長不下降子序列(Longest Increasing Sequence, LIS)

技術標籤:動態規劃演算法資料結構javascriptnumpy

分析:

完整 程式碼:

 1 // 最長不下降子序列
 2 #include <stdio.h>
 3 #include <algorithm>
 4 using namespace std;
 5 
 6 const int N = 100;
 7 int A[N], dp[N];
 8 
 9 int main()
10 {
11     freopen("in.txt", "r", stdin);
12     int n;
13     scanf("%d", &n);
14     for (int i = 1; i <= n; i++){
15         scanf("%d", &A[i]);
16     }
17 
18     int ans = -1;        // 記錄最長的dp[i]
19     for (int i = 1; i <= n; i++){        // 按順序計算出dp[i]的值
20         dp[i] = 1;            // 邊界初始條件(先假設每個元素自成一個子序列)
21         // 如果A[i] >= A[j] 且 A[i]的加入能使dp[i]變長,即dp[j] + 1 > dp[i]
22         for (int j = 1; j < i; j++){
23             if (A[i] >= A[j] && (dp[j] + 1 > dp[i])){
24                 dp[i] = dp[j] + 1;        // 狀態轉移方程,用以更新dp[i]
25             }
26         }
27         ans = max(ans, dp[i]);
28     }
29 
30     printf("%d", ans);
31     fclose(stdin);
32 
33     return 0;
34 }

題型實戰:

             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 pieces and sewing the remaining parts together to form her favorite color stripe.

It is said that a normal human eye can distinguish about less than 200 different colors, so Eva's favorite colors are limited. However the original stripe could be very long, and Eva would like to have the remaining favorite stripe with the maximum length. So she needs your help to find her the best result.

Note that the solution might not be unique, but you only have to tell her the maximum length. For example, given a stripe of colors {2 2 4 1 5 5 6 3 1 1 5 6}. If Eva's favorite colors are given in her favorite order as {2 3 1 5 6}, then she has 4 possible best solutions {2 2 1 1 1 5 6}, {2 2 1 5 5 5 6}, {2 2 1 5 5 6 6}, and {2 2 3 1 1 5 6}.

Input Specification:

Each input file contains one test case. For each case, the first line contains a positive integer N (200) which is the total number of colors involved (and hence the colors are numbered from 1 to N). Then the next line starts with a positive integer M (200) followed by M Eva's favorite color numbers given in her favorite order. Finally the third line starts with a positive integer L (104​​) which is the length of the given stripe, followed by L colors on the stripe. All the numbers in a line a separated by a space.

Output Specification:

For each test case, simply print in a line the maximum length of Eva's favorite stripe.

Sample Input:

6
5 2 3 1 5 6
12 2 2 4 1 5 5 6 3 1 1 5 6

Sample Output:

7

分析:將喜歡的顏色對映到一個非遞減序列,然後將所有輸入的顏色且是喜歡的顏色對映到一個數組中,求這個陣列中最長的非遞減序列長度

程式碼:

 1 #include <stdio.h>
 2 #include <algorithm>
 3 #include <string.h>
 4 using namespace std;
 5 
 6 const int maxc = 210;    // 最大顏色數
 7 const int maxn = 10010;    // 最大的L
 8 
 9 // 
10 int HashTable[maxc];    // 將喜歡的顏色對映為遞增序列,不喜歡的顏色對映為-1
11 int A[maxn], dp[maxn];    // 最長不下降子序列的原陣列A和DP陣列
12 
13 int main()
14 {
15     int n, m, x;
16     scanf("%d%d", &n, &m);
17     memset(HashTable, -1, sizeof(HashTable));        // 將整數HashTable陣列初始化為-1
18     for (int i = 0; i < m; i++){
19         scanf("%d", &x);
20         HashTable[x] = i;
21     }
22 
23     int L, num = 0;        // num存放L個顏色中包含喜歡的顏色的數量
24     scanf("%d", &L);
25     for (int i = 0; i < L; i++){
26         scanf("%d", &x);
27         if (HashTable[x] != -1){
28             A[num++] = HashTable[x];
29         }
30     }
31 
32     // 以下為LIS問題的模板
33     int ans = -1;
34     for (int i = 0; i < num; i++){
35         dp[i] = 1;
36         for (int j = 0; j < i; j++){
37             if (A[j] <= A[i] && dp[j] + 1 > dp[i]){
38                 dp[i] = dp[j] + 1;
39             }
40         }
41         ans = max(ans, dp[i]);
42     }
43 
44     printf("%d\n", ans);
45     return 0;
46 }