20. Dynamic Programming 3

Longest Common Subsequence(LCS) Problem

  1. what is the subsequence:
    ⟨A2,C3,B4,B7,C8,W11⟩ is a subsequence of ⟨T,A,C,B,B,W,B,C,W,T,W⟩.

  2. Given two sequences X and Y , a sequence Z is called the common subsequence of X and Y if Z is a subsequence of both X and Y.

  3. The longest-common-subsequence problem is to find a common subsequence Z = LCS(X,Y) with the maximum length from both sequences X andY.

Denote by
Xm = ⟨x1,x2,...,xm⟩
Yn = ⟨y1,y2,...,yn⟩
Our task then is to find an LCS(Xm,Yn) of Xm and Yn.

Solution

  1. Characterize the structure of an optimal solution (the key observation)


  2. Recurrence of the optimal solution

Smart Use of the LCS algorithm

Question: Given a sequence of n elements, the problem is to find a longest increasing (or decreasing) subsequence of the sequence.

Answer:
Let X be the original sequence and Y the sorted sequence of X in increasing order. Then, the longest increasing subsequence of X is LCS(X,Y)

Question 2: given three sequences X , Y and Z with length nX , nY and nZ , respectively, find a longest common subsequence W among the three sequences, what is the running time of the proposed algorithm?

The crucial thing of the algorithm is to find the dependency of the subproblem and main problem

Question 3: 最长回文字符串


  1. When the question is asking whether the result is correct,the easiest thing is to find a counterexample and prove it false。
  2. For DP, the time complexity is O(n), n is the input number rather than the size of input.
  3. the most important part of DP is the storing slots(one dimension or two) and optimal substructure.
  4. to deal with the DP, make sure the aim, the transfer function(how to divide huge to small questions) and the small questions are also the same question to original question, the only difference is the size or scale of the problem.
  5. sometimes for different condition, the transfer function is different.
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 背景 一年多以前我在知乎上答了有关LeetCode的问题, 分享了一些自己做题目的经验。 张土汪:刷leetcod...
    土汪阅读 12,779评论 0 33
  • 月赢到会馆后翻看了手机,有50多条的未读信息,大多数都是祝她生日快乐的,也有发红包的,而月赢在支付宝里发现赵思凯给...
    赢月照星空阅读 178评论 0 4
  • 美,就是 不管自己身处何方 何地 在暑往寒来 春夏秋冬中 静静地孕育自己的生命 吸取天地间的灵气 安然接受风雨的洗...
    陈糊涂阅读 121评论 0 0
  • 今天送完孩子,我坐在车上点开我们郑州路小学家长教育群,读了这样一篇文章:在教育孩子的时候,你选择了挣钱,不去管教孩...
    孙若菡妈妈阅读 221评论 0 0