zoukankan      html  css  js  c++  java
  • 动态规划 ---- 最长不下降子序列(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
    ", ans);
    45     return 0;
    46 }
  • 相关阅读:
    未能从程序集 C:Program Files (x86)MSBuild14.0inMicrosoft.Data.Entity.Build.Tasks.dll 加载任务“EntityClean”
    asp.net mvc 4 项目升级到 asp.net mvc5
    SQL Server查看所有表大小,所占空间
    0x80072f8a未指定的错误
    vs2012 aps.net4.0/4.5尚未在web服务器上注册
    vsphere 出现“在主机的当前连接状况下不允许执行该操作”
    sql server 发布时提示'dbo.sysmergepublications'无效的解决办法
    sql server更改机器名后更改数据库机器名
    Ajax向后台传入File类型参数
    上传下载Azure Blob里的Excel文件。
  • 原文地址:https://www.cnblogs.com/hi3254014978/p/12248180.html
Copyright © 2011-2022 走看看