ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

动态规划 ---- 最长公共子序列(Longest Common Subsequence, LCS)

2021-05-28 15:02:02  阅读:284  来源: 互联网

标签:LCS her int Eva favorite colors ---- Subsequence stripe


分析:

 

 

 完整代码:

// 最长公共子序列
#include <stdio.h>
#include <algorithm>
using namespace std;

const int N = 100;
char A[N], B[N];
int dp[N][N];

int main()
{
    freopen("in.txt", "r", stdin);
    int n;
    gets(A + 1);        // 从下标1开始读入
    gets(B + 1);
    int lenA = strlen(A + 1);    // 由于读入时下标从1开始,因此读取长度也从1开始
    int lenB = strlen(B + 1);

    // 边界
    for (int i = 0; i <= lenA; i++){
        dp[i][0] = 0;
    }
    for (int j = 0; j <= lenB; j++){
        dp[0][j] = 0;
    }

    // 状态转移方程
    for (int i = 1; i <= lenA; i++){
        for (int j = 1; j <= lenB; j++){
            if (A[i] == B[j]){
                dp[i][j] = dp[i - 1][j - 1] + 1;
            }
            else{
                dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
            }
        }
    }
    // dp[lenA][lenB]是答案
    printf("%d\n", dp[lenA][lenB]);
    fclose(stdin);
    return 0;
}

 

题型实战:

                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 (≤10​4​​) 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 using namespace std;
 4 
 5 const int maxc = 210;        // 颜色的最大种类数
 6 const int maxn = 10010;        // 颜色序列的最大长度
 7 int A[maxc], B[maxn], dp[maxc][maxc];
 8 
 9 int main()
10 {
11     int n, m;
12     scanf("%d%d", &n, &m);
13     for (int i = 1; i <= m; i++){
14         scanf("%d", &A[i]);
15     }
16     int L;
17     scanf("%d", &L);
18     for (int i = 1; i <= L; i++){
19         scanf("%d", &B[i]);
20     }
21     // 边界
22     for (int i = 0; i <= m; i++){
23         dp[i][0] = 0;
24     }
25     for (int j = 0; j <= L; j++){
26         dp[0][j] = 0;
27     }
28 
29     // 状态转移方程
30     for (int i = 1; i <= m; i++){
31         for (int j = 1; j <= L; j++){
32             // 取dp[i - 1][j]、dp[i][j - 1]中的较大值
33             int Max = max(dp[i - 1][j], dp[i][j - 1]);
34             if (A[i] == B[j]){
35                 dp[i][j] = Max + 1;
36             }
37             else{
38                 dp[i][j] = Max;
39             }
40         }
41     }
42 
43     // 输出答案
44     printf("%d\n", dp[m][L]);
45 
46     return 0;
47 }

 

标签:LCS,her,int,Eva,favorite,colors,----,Subsequence,stripe
来源: https://blog.51cto.com/u_14201949/2825926

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有