Longest common subsequence problem

本文介绍了最长公共子序列问题的基本概念及解决方法。通过定义两个序列X和Y的前缀,利用LCS函数来找出两个序列中最长的共同子序列。当序列元素相等时,会在LCS(Xi-1,Yj-1)的基础上加入该元素;如果不相等,则保留LCS(Xi,Yj-1)和LCS(Xi-1,Yj)中较长的那个序列。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

http://en.wikipedia.org/wiki/Longest_common_subsequence_problem

 

LCS function defined

Let two sequences be defined as follows: X = (x1, x2...xm) and Y = (y1, y2...yn). The prefixes of X are X1, 2,...m; the prefixes of Y are Y1, 2,...n. Let LCS(Xi, Yj) represent the set of longest common subsequence of prefixes Xi and Yj. This set of sequences is given by the following.

LCS\left(X_{i},Y_{j}\right) = \begin{cases}   \empty & \mbox{ if }\ i = 0 \mbox{ or }  j = 0 \\   \textrm{ ( } LCS\left(X_{i-1},Y_{j-1}\right), x_i) & \mbox{ if } x_i = y_j \\   \mbox{longest}\left(LCS\left(X_{i},Y_{j-1}\right),LCS\left(X_{i-1},Y_{j}\right)\right) & \mbox{ if } x_i \ne y_j \\ \end{cases}

To find the longest subsequences common to Xi and Yj, compare the elements xi and yi. If they are equal, then the sequence LCS(Xi-1, Yj-1) is extended by that element, xi. If they are not equal, then the longer of the two sequences, LCS(Xi, Yj-1), and LCS(Xi-1, Yj), is retained. (If they are both the same length, but not identical, then both are retained.) Notice that the subscripts are reduced by 1 in these formulas. That can result in a subscript of 0. Since the sequence elements are defined to start at 1, it was necessary to add the requirement that the LCS is empty when a subscript is zero.

 

http://www.csie.ntnu.edu.tw/~u91029/LongestCommonSubsequence.html 

转载于:https://www.cnblogs.com/cutepig/archive/2010/05/21/1741203.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值