首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >最优子结构性质

最优子结构性质
EN

Stack Overflow用户
提问于 2020-11-20 10:57:18
回答 1查看 112关注 0票数 2

假设我们有两个字符串X,Y,长度分别为n,m。我找到了X和Y的最长公共子序列Z。

如何证明最长公共子序列(LCS)的最优子结构性质?

EN

回答 1

Stack Overflow用户

发布于 2020-11-20 12:18:59

为了证明问题的子结构是最优的,您需要首先定义state是什么。

LCS问题:

代码语言:javascript
复制
Strings: X, Y
X = x1x2.....xN #length=N
Y = y1y2.....yM #length=M

定义国家/地区

对于LCS问题,让我们将状态D[i,j]定义为:

代码语言:javascript
复制
D[i,j] = longest common subsequence of substrings X[1:i] and Y[1:j]

Based on our definition above, we are looking to find the value of **D[N, M]**

提出递归关系(Di,j):

案例1:

代码语言:javascript
复制
x[i] == y[j] 

1. If the characters we are comparing are already the same, it can very well be part of the longest-common-subsequence for X[1:i] and Y[1:j].
2. The LCS might also turn-out from X[1:i-1] & Y[1:j]; OR
3. turn-out from X[1:i] & Y[1:j-1]

Thus,
D[i][j] = maxlen(D[i-1][j-1] + {X[i]}, D[i-1][j], D[i][j-1])

案例2:

代码语言:javascript
复制
x[i] != y[j]

Argument follows directly from previous case:

D[i][j] = maxlen(D[i-1][j], D[i][j-1])

最优的推理来自归纳,因为我们自下而上地构建最终答案D[N,M]

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64923145

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档