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

Posted hi3254014978

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了动态规划 ---- 最长公共子序列(Longest Common Subsequence, LCS)相关的知识,希望对你有一定的参考价值。

技术图片

分析:

技术图片

 

 技术图片

 完整代码:

// 最长公共子序列
#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
", 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
", dp[m][L]);
45 
46     return 0;
47 }

 

以上是关于动态规划 ---- 最长公共子序列(Longest Common Subsequence, LCS)的主要内容,如果未能解决你的问题,请参考以下文章

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

leetcode 1143. Longest Commom Subsequence 最长公共子序列(中等)

动态规划 最长公共子序列 过程图解

LeetCode 1143 最长公共子序列

动态规划——最长公共子序列与最长公共子串 (含Python实现代码)

最长公共子序列(LCS)动态规划解题笔记