How to find the longest continuous common subsequence and longest continuous substring

Source: Internet
Author: User

Some time ago I studied how to find the longest continuous public subsequence and the longest continuous substring. One of the previous students asked me how to solve this problem:

The key to the problem is how to define sub-problems,

Assume that:

XM = x1 X2 X3... XM

YN = Y1 Y2 Y3... YN

1. Longest Common subsequence (not consecutive)

Defines F (m, n) as the length of the longest subsequence between xm and YN.

So F (M, 0) = f (0, m) = 0

If XM! = YN, then f (m, n) = max {f (m-1, n), F (M, n-1 )}

If XM = YN, F (m, n) = f (S-1, n-1) + 1

The problem lies in F (m, n ). Use Bottom-up dynamic programming to solve the problem according to the formula.

2. The eldest son string (must be continuous)

Defines F (m, n) as the length of the longest substring between xm and YN and ends with XM & YN. Because the requirements are continuous, when defining F, an extra requirement string ends at XM & YN.

So F (M, 0) = f (0, m) = 0

If XM! = YN, then f (m, n) = 0

If XM = YN, F (m, n) = f (S-1, n-1) + 1

Because the longest string may not end at the end of XM/YN, all possible F (p, q) | 0 <p <m, 0 <q <n, the largest F (p, q) is the solution. Use Bottom-up dynamic programming to solve the problem according to the formula.

 

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.