跳到主要内容

東京大学 新領域創成科学研究科 メディカル情報生命専攻 2017年8月実施 問題12

Author​

zephyr, 祭音Myyura

Description​

For two strings x=x[1]⋯x[n]\mathbf{x} = x[1] \cdots x[n] and y=y[1]⋯y[m]\mathbf{y} = y[1] \cdots y[m] of lengths nn and mm (n≥0,m≥0n \geq 0, m \geq 0), we define the set of common subsequences as

S={(i1,⋯ ,irj1,⋯ ,jr)∣r:positive integer,1≤i1<⋯<ir≤n1≤j1<⋯<jr≤m,x[ik]=y[jk],k=1,⋯ ,r}S = \left\{ \begin{pmatrix} i_1, \cdots, i_r \\ j_1, \cdots, j_r \end{pmatrix} \mid r: \text{positive integer}, \begin{matrix} 1 \leq i_1 < \cdots < i_r \leq n \\ 1 \leq j_1 < \cdots < j_r \leq m \end{matrix}, x[i_k] = y[j_k], k = 1, \cdots, r \right\}

Here, rr represents the length of common subsequence (i1,⋯ ,irj1,⋯ ,jr)\begin{pmatrix} i_1, \cdots, i_r \\ j_1, \cdots, j_r \end{pmatrix}. Let l(x,y)l(x, y) denote the length of the longest common subsequences. If S=∅S = \emptyset, we define l(x,y)=0l(x, y) = 0.

(1) Let zkpz_k^p and zksz_k^s denote the length-kk prefix and suffix of string zz, respectively. We define α[i,j]=l(xip,yjp)\alpha[i, j] = l(x_i^p, y_j^p) and β[i,j]=l(xn−i+1s,ym−j+1s)\beta[i, j] = l(x_{n-i+1}^s, y_{m-j+1}^s) for 1≤i≤n,1≤j≤m1 \leq i \leq n, 1 \leq j \leq m. Describe a recurrence for computing α[i,j]\alpha[i, j] from α[i−1,j−1]\alpha[i-1, j-1], α[i−1,j]\alpha[i-1, j], and α[i,j−1]\alpha[i, j-1] when 1<i,1<j1 < i, 1 < j. Also, describe a recurrence for computing β[i,j]\beta[i, j] from β[i+1,j]\beta[i+1, j], β[i,j+1]\beta[i, j+1], and β[i+1,j+1]\beta[i+1, j+1] when i<n,j<mi < n, j < m.

(2) Compute matrices α\alpha and β\beta for x=ACTGG\mathbf{x} = \text{ACTGG} and y=ACACG\mathbf{y} = \text{ACACG}.

(3) Suppose matrix α\alpha is given. Write a pseudocode for obtaining one of the longest common subsequences.

(4) Suppose matrices α\alpha and β\beta are given. Write a pseudocode for computing the maximal length of common subsequences that contain the ii-th position of x\mathbf{x} (1≤i≤n1 \leq i \leq n).


对于长度为 nn 和 mm 的两个字符串 x=x[1]⋯x[n]\mathbf{x} = x[1] \cdots x[n] 和 y=y[1]⋯y[m]\mathbf{y} = y[1] \cdots y[m] (n≥0,m≥0n \geq 0, m \geq 0),我们定义公共子序列集为

S={(i1,⋯ ,irj1,⋯ ,jr)∣r:正整数,1≤i1<⋯<ir≤n1≤j1<⋯<jr≤m,x[ik]=y[jk],k=1,⋯ ,r}S = \left\{ \begin{pmatrix} i_1, \cdots, i_r \\ j_1, \cdots, j_r \end{pmatrix} \mid r: \text{正整数}, \begin{matrix} 1 \leq i_1 < \cdots < i_r \leq n \\ 1 \leq j_1 < \cdots < j_r \leq m \end{matrix}, x[i_k] = y[j_k], k = 1, \cdots, r \right\}

这里,rr 表示公共子序列 (i1,⋯ ,irj1,⋯ ,jr)\begin{pmatrix} i_1, \cdots, i_r \\ j_1, \cdots, j_r \end{pmatrix} 的长度。令 l(x,y)l(x, y) 表示最长公共子序列的长度。如果 S=∅S = \emptyset,我们定义 l(x,y)=0l(x, y) = 0。

(1) 令 zkpz_k^p 和 zksz_k^s 分别表示字符串 zz 的长度为 kk 的前缀和后缀。我们定义 α[i,j]=l(xip,yjp)\alpha[i, j] = l(x_i^p, y_j^p) 和 β[i,j]=l(xn−i+1s,ym−j+1s)\beta[i, j] = l(x_{n-i+1}^s, y_{m-j+1}^s) 对于 1≤i≤n,1≤j≤m1 \leq i \leq n, 1 \leq j \leq m。描述从 α[i−1,j−1]\alpha[i-1, j-1],α[i−1,j]\alpha[i-1, j],和 α[i,j−1]\alpha[i, j-1] 计算 α[i,j]\alpha[i, j] 的递推关系,当 1<i,1<j1 < i, 1 < j。同时,描述从 β[i+1,j]\beta[i+1, j],β[i,j+1]\beta[i, j+1],和 β[i+1,j+1]\beta[i+1, j+1] 计算 β[i,j]\beta[i, j] 的递推关系,当 i<n,j<mi < n, j < m。

(2) 计算 x=ACTGG\mathbf{x} = \text{ACTGG} 和 y=ACACG\mathbf{y} = \text{ACACG} 的矩阵 α\alpha 和 β\beta。

(3) 假设给定矩阵 α\alpha。编写伪代码以获取一个最长的公共子序列。

(4) 假设给定矩阵 α\alpha 和 β\beta。编写伪代码以计算包含 x\mathbf{x} 的第 ii 个位置的最大长度的公共子序列 (1≤i≤n1 \leq i \leq n)。

题目描述​

给定长度分别为 n,mn,m(n,m≥0n,m\ge0)的字符串

x=x[1]⋯x[n],y=y[1]⋯y[m].\mathbf x=x[1]\cdots x[n],\qquad \mathbf y=y[1]\cdots y[m].

公共子序列用两列严格递增的下标

(i1,…,irj1,…,jr)\begin{pmatrix}i_1,\ldots,i_r\\j_1,\ldots,j_r\end{pmatrix}

表示,其中 rr 为正整数,

1≤i1<⋯<ir≤n,1≤j1<⋯<jr≤m,1\le i_1<\cdots<i_r\le n,\qquad 1\le j_1<\cdots<j_r\le m,

且对每个 k=1,…,rk=1,\ldots,r 都有 x[ik]=y[jk]x[i_k]=y[j_k]。最长公共子序列长度记为 l(x,y)l(x,y);若不存在公共子序列,则定义 l(x,y)=0l(x,y)=0。

  1. 令 zkp,zksz_k^p,z_k^s 分别为字符串 zz 的长度 kk 前缀和后缀,并定义

    α[i,j]=l(xip,yjp),β[i,j]=l(xn−i+1s,ym−j+1s).\alpha[i,j]=l(x_i^p,y_j^p),\qquad \beta[i,j]=l(x_{n-i+1}^s,y_{m-j+1}^s).

    对 1<i,1<j1<i,1<j,给出由 α[i−1,j−1]\alpha[i-1,j-1]、α[i−1,j]\alpha[i-1,j]、α[i,j−1]\alpha[i,j-1] 计算 α[i,j]\alpha[i,j] 的递推;对 i<n,j<mi<n,j<m,给出由 β[i+1,j]\beta[i+1,j]、β[i,j+1]\beta[i,j+1]、β[i+1,j+1]\beta[i+1,j+1] 计算 β[i,j]\beta[i,j] 的递推。

  2. 对 x=ACTGG\mathbf x=\mathrm{ACTGG}、y=ACACG\mathbf y=\mathrm{ACACG},计算完整矩阵 α,β\alpha,\beta。

  3. 已知 α\alpha,编写取得一个最长公共子序列的伪代码。

  4. 已知 α,β\alpha,\beta,编写伪代码,对指定 1≤i≤n1\le i\le n 求所有包含 x\mathbf x 第 ii 个位置的公共子序列中的最大长度。

Kai​

(1)​

Recurrence for α[i,j]\alpha[i, j]​

The value of α[i,j]=l(xip,yjp)\alpha[i, j] = l(x_i^p, y_j^p) can be computed as follows:

  • If x[i]=y[j]x[i] = y[j], then α[i,j]=α[i−1,j−1]+1\alpha[i, j] = \alpha[i-1, j-1] + 1.
  • If x[i]≠y[j]x[i] \neq y[j], then α[i,j]=max⁡(α[i−1,j],α[i,j−1])\alpha[i, j] = \max(\alpha[i-1, j], \alpha[i, j-1]).

This can be summarized as:

α[i,j]={α[i−1,j−1]+1if x[i]=y[j]max⁡(α[i−1,j],α[i,j−1])if x[i]≠y[j]\alpha[i, j] = \begin{cases} \alpha[i-1, j-1] + 1 & \text{if } x[i] = y[j] \\ \max(\alpha[i-1, j], \alpha[i, j-1]) & \text{if } x[i] \neq y[j] \end{cases}

Use the boundary values α[0,j]=α[i,0]=0\alpha[0,j]=\alpha[i,0]=0.

Recurrence for β[i,j]\beta[i, j]​

The value of β[i,j]=l(xn−i+1s,ym−j+1s)\beta[i, j] = l(x_{n-i+1}^s, y_{m-j+1}^s) can be computed as follows:

  • If x[i]=y[j]x[i] = y[j], then β[i,j]=β[i+1,j+1]+1\beta[i, j] = \beta[i+1, j+1] + 1.
  • If x[i]≠y[j]x[i] \neq y[j], then β[i,j]=max⁡(β[i+1,j],β[i,j+1])\beta[i, j] = \max(\beta[i+1, j], \beta[i, j+1]).

This can be summarized as:

β[i,j]={β[i+1,j+1]+1if x[i]=y[j]max⁡(β[i+1,j],β[i,j+1])if x[i]≠y[j]\beta[i, j] = \begin{cases} \beta[i+1, j+1] + 1 & \text{if } x[i] = y[j] \\ \max(\beta[i+1, j], \beta[i, j+1]) & \text{if } x[i] \neq y[j] \end{cases}

Use the boundary values β[n+1,j]=β[i,m+1]=0\beta[n+1,j]=\beta[i,m+1]=0.

(2)​

Matrix α\alpha​

Let's fill the matrix α\alpha for x=ACTGG\mathbf{x} = \text{ACTGG} and y=ACACG\mathbf{y} = \text{ACACG}:

x\yACACG
A11111
C12222
T12222
G12223
G12223

Matrix β\beta​

Let's fill the matrix β\beta for x=ACTGG\mathbf{x} = \text{ACTGG} and y=ACACG\mathbf{y} = \text{ACACG}. The row and column indices are displayed in descending order:

x\yGCACA
G11111
G11111
T11111
C12222
A12333

(3)​

To extract one of the longest common subsequences from the matrix α\alpha, 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 α\alpha.

Explanation​

  • Start from the bottom-right corner of the matrix α\alpha, i.e., α[n,m]\alpha[n, m].
  • Compare characters of x\mathbf{x} and y\mathbf{y}:
    • If x[i]=y[j]x[i] = y[j], include x[i]x[i] in the result and move diagonally to α[i−1,j−1]\alpha[i-1, j-1].
    • If x[i]≠y[j]x[i] \neq y[j], move in the direction that gives the larger value (either up or left).
  • Continue until one of the remaining prefixes is empty.
  • The resulting list of matched index pairs represents one of the longest common subsequences; their characters can be read from xx.

Pseudocode​

function getLCS(x, y, alpha)
i = length(x)
j = length(y)
pairs = []
while i > 0 and j > 0
if x[i] == y[j]
append (i, j) to pairs
i = i - 1
j = j - 1
else if alpha[i-1, j] >= alpha[i, j-1]
i = i - 1
else
j = j - 1
reverse pairs
return pairs

(4)​

Given matrices α\alpha and β\beta, match x[i]x[i] with some equal character y[j]y[j]. Combine a common subsequence before these positions, this matched pair, and a common subsequence after them.

Explanation​

  • For each position jj in y\mathbf{y}:
    • If x[i]=y[j]x[i]=y[j], the maximal length using this pair is α[i−1,j−1]+1+β[i+1,j+1]\alpha[i-1,j-1]+1+\beta[i+1,j+1].
  • The maximum value obtained through this process gives the desired length.

Pseudocode​

function maxLengthWithPosition(x, y, alpha, beta, i)
maxLength = 0
for j = 1 to length(y)
if x[i] == y[j]
currentLength = alpha[i-1, j-1] + 1 + beta[i+1, j+1]
maxLength = max(maxLength, currentLength)
return maxLength

If no character of yy matches x[i]x[i], there is no common subsequence containing this position; the function returns 00 in that case.

Knowledge​

动态规划 最长公共子序列 递归

难点思路​

对于递归关系的理解和矩阵填充的具体实现可能会比较复杂,需要仔细考虑每一步的递推关系。

解题技巧和信息​

  • 动态规划表格填充方法:先初始化,然后按照递推关系逐步填充。
  • 递归关系需要对字符串字符的匹配情况进行详细考虑,以确保递推关系的正确性。

重点词汇​

  • common subsequence 公共子序列
  • recurrence relation 递推关系
  • prefix 前缀
  • suffix 后缀
  • dynamic programming 动态规划

参考资料​

  1. Introduction to Algorithms, 3rd Edition, Cormen et al., Chapter 15.

Reference​