CBMS-2018-12
题目来源:[[做题/文字版题库/CBMS/2018#Question 12|2018#Question 12]] 日期:2024-07-27 题目主题: CS-算法-最长公共子序列
解题思路
我们首先定义 和 ,并描述它们的递归关系。然后我们计算给定字符串的矩阵 和 。接着,我们编写伪代码以从矩阵 中获取一个最长的公共子序列。最后,我们编写伪代码以在给定矩阵 和 的情况下计算包含 的第 个位置的公共子序列的最大长度。
Solution
Part 1: Recurrence Relations
Recurrence for
The value of can be computed as follows:
- If , then .
- If , then .
This can be summarized as:
Recurrence for
The value of can be computed as follows:
- If , then .
- If , then .
This can be summarized as:
Part 2: Compute Matrices and for and
Matrix
Let’s fill the matrix for and :
x\y | A | C | A | C | G |
---|---|---|---|---|---|
A | 1 | 1 | 1 | 1 | 1 |
C | 1 | 2 | 2 | 2 | 2 |
T | 1 | 2 | 2 | 2 | 2 |
G | 1 | 2 | 2 | 2 | 3 |
G | 1 | 2 | 2 | 2 | 3 |
Matrix
Let’s fill the matrix for and :
x\y | G | C | A | C | A |
---|---|---|---|---|---|
G | 1 | 1 | 1 | 1 | 1 |
G | 1 | 1 | 1 | 1 | 1 |
T | 1 | 1 | 1 | 1 | 1 |
C | 1 | 2 | 2 | 2 | 2 |
A | 1 | 2 | 3 | 3 | 3 |
Part 3: Pseudocode for Obtaining One of the Longest Common Subsequences
To extract one of the longest common subsequences from the matrix , we use the following algorithm. This algorithm traces back from the bottom-right corner of the matrix to the top-left corner, reconstructing the longest common subsequence by following the path of optimal choices recorded in .
Explanation
- Start from the bottom-right corner of the matrix , i.e., .
- Compare characters of and :
- If , include in the result and move diagonally to .
- If , move in the direction that gives the larger value (either up or left).
- Continue this process until reaching the top-left corner of the matrix.
- The result will be one of the longest common subsequences.
Pseudocode
Part 4: Pseudocode for Computing Maximal Length of Common Subsequences Containing the -th Position of
Given matrices and , we can compute the maximal length of common subsequences that include the -th position of . This is done by evaluating the length of subsequences that start from the beginning and end at the -th position, combined with subsequences that start at the -th position and extend to the end.
Explanation
- For each position in :
- Combine the length of the prefix up to () with the length of the suffix from onwards () as the length of the common subsequence.
- If , since is included in both the prefix and suffix, subtract 1 from the total length.
- The maximum value obtained through this process gives the desired length.
Pseudocode
知识点
难点思路
对于递归关系的理解和矩阵填充的具体实现可能会比较复杂,需要仔细考虑每一步的递推关系。
解题技巧和信息
- 动态规划表格填充方法:先初始化,然后按照递推关系逐步填充。
- 递归关系需要对字符串字符的匹配情况进行详细考虑,以确保递推关系的正确性。
重点词汇
- common subsequence 公共子序列
- recurrence relation 递推关系
- prefix 前缀
- suffix 后缀
- dynamic programming 动态规划
参考资料
- Introduction to Algorithms, 3rd Edition, Cormen et al., Chapter 15.