动态规划 ---- 最长不下降子序列(Longest Increasing Sequence, LIS)

分析:

 

 

完整 代码:

 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 }

 

posted @ 2020-02-01 12:53  Lucky小黄人^_^  阅读(397)  评论(0编辑  收藏  举报