The extension of the longest common subsequence of LCS (continuous subsequence)

Source: Internet
Author: User

People who have read the introduction to algorithms should know that a very classic example of dynamic planning is the longest common subsequence of LCS (longest common length. Next we will review the concept of LCS.

Assume there are two strings, x = <A, B, C, B, D, a, B>, y = <B, D, C, A, B, A>, then their longest common subsequences are <B, C, B, A>, which features that each character can be discontinuous. LCS problems are also widely used in practice, for example, for re-checking papers.

It is said that the essence of a dynamic planning algorithm lies in the state transition equation, so let's recall the LCS state transition equation by the way. If C [I, j] is used to represent the length of LCS of sequence XI and Yi, the state transition equation is available:

 

The focus of this article is as follows. If the conditions of LCS are tightened, the characters in the subsequence must be continuous. So how should we solve this longest public continuous subsequence?

In order to facilitate writing, after the article mentioned "the longest public continuous subsequence", I will be replaced by STRICT-LCS.

For ease of understanding, we still use the previous two strings to illustrate what is strict-LCs. X = <A, B, C, B, D, a, B>, y = <B, D, C, A, B, A>. Then their longest common continuous subsequences are <B, d>.

We still use dynamic programming to solve the problem. It must be changed based on the original one. First, we define C [I, j] as slightly different from the original meaning. Here C [I, j] refers to the length of the STRICT-LCS with the last element XI (= YJ), such as X = <a, B, c>, y = <, b, d> so c [3, 3] = 0, regardless of the above, if Xi and YJ are not equal, you have to clear C [I, j, as a new start.

In LCS, the value of C [I, j] increases with the value of I and J, and has a cumulative effect; but in STRICT-LCS, C [I, j] values may be cleared at any time.

After talking about this, write out the state transition equation:

 

Pseudocode is also relatively simple.

 1 STRICT-LCS-LENGTH(X, Y) 2 m = length[X] 3 m = length[Y] 4 for i = 1 to m 5     do c[i, 0] = 0 6 for j = 0 to n 7     do c[0, j] = 0 8 for i = 1 to m 9     do for j = 1 to n10         do if Xi=Yj11             then c[i, j] = c[i-1, j-1]+112             else c[i, j] = 013 return c

The following figure shows the derivation process.

We can find the largest lattice 2, so we can see that the longest common continuous subsequence of X and Y is BD, and AB, and the length of the two subsequences is 2.

E

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.