Algorithm series (vi) three methods for the longest common subsequence (LCS) problem (continuous subsequence)

Source: Internet
Author: User
Tags continue first string require

The longest common subsequence (LCS) problem has two ways of defining a subsequence, one is that the subsequence does not require discontinuity, and one is that the subsequence must be sequential. In the last chapter, we introduce two algorithms to solve the problem of the final common subsequence that the subsequence does not require, and this chapter will introduce how to solve the longest common subsequence problem by using the algorithm when the subsequence must be continuous.

For example, the two string "Abcdea" and "AEBCDA" in the preceding chapter, if the subsequence does not require continuous, its longest common subsequence is "ABCDA", and if the subsequence requirement is contiguous, its longest common subsequence should be "BCD". In this case, it is possible for two strings to have multiple common substrings of the same length, such as "askdfiryetd" and "Trkdffirey" two strings, there are two common substrings of length 3, respectively "KDF" and "fir", so the nature of the problem has changed. You need to find out all the possible instances of a common substring of two strings, and then take the longest one, if you have more than one longest common substring, take only one.

If both the string "Abcdea" and "AEBCDA" are aligned with the leftmost a character, the longest common substring that can match is "a". However, if you align the a character of the first string with the e character of the second string, the longest common substring that can match is "BCD." It can be seen that starting from different positions of two strings to match to get different results, therefore, the algorithm used in this article is to give two of all possible alignment of the string, each alignment of the characters to match, find the longest matching substring.

I. Recursive method

First look at the recursive method. The recursive approach is simpler by comparing the first characters of two strings to equal, and if they are equal, adding them to the end of a known common substring, and then removing the remaining substring of the two string after the first character is removed. If the first character of the two string is not equal, the possible longest common substring is calculated with three alignment policies, it then takes the longest one, compared to the currently known longest common substring, replaces the longest common substring that is currently known, with the longest common substring that is currently known. The first strategy is to delete the first character of a string, continues to match the remaining substring with the second string; the second strategy is to delete the first character of the second string and continue to match the remaining substring with the first string; The third strategy is to delete the first character of the two strings and then continue to match the remaining two strings. Substring. The deletion of the first character corresponds to the character alignment shift, and the entire algorithm is implemented as follows:

 180 void Recursionlcs (const std::string& str1, const std::string& STR2, std::string& LCS) 1   
       
Bayi {str1.length () = 0 | | str2.length () = 0) 183 return;   
       
184 185 if (str1[0] = = Str2[0]) 186 {187 LCS + = str1[0];   
       
188 Recursionlcs (STR1.SUBSTR (1), STR2.SUBSTR (1), LCS);   
       
189} 190 Else 191 {std::string Strtmp1,strtmp2,strtmp3;   
       
193 194 Recursionlcs (STR1.SUBSTR (1), str2, STRTMP1);   
       
195 Recursionlcs (STR1, Str2.substr (1), STRTMP2);   
       
196 Recursionlcs (STR1.SUBSTR (1), STR2.SUBSTR (1), StrTmp3);   
       
197 std::string strlongest = getlongeststring (STRTMP1, STRTMP2, StrTmp3);   
       
198 if (Lcs.length () < Strlongest.length ()) 199 LCS = Strlongest;   
      200} 
201} 

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.