LCS problem algorithm-VB.net

Source: Internet
Author: User

The LCS problem is to find the longest common substring of two strings. The solution is to use a matrix to record the matching conditions between the two characters at all positions in two strings. If it matches, it is 1; otherwise, it is 0. Then we can find the longest 1 series of diagonal lines. The corresponding position is the longest position matching the substring.

The following is the matching matrix between string 21232523311324 and string 312123223445. The former is in the X direction and the latter is in the Y direction. It is not hard to find. The red part is the longest matching substring. The longest matching substring is 21232.


0 0 0 1 0 0 1 1 0 0 1 0 0 0
0 1 0 0 0 0 0 0 1 1 0 0 0 0
1 0 1 0 1 0 1 0 0 0 0 1 0 0
0 1 0 0 0 0 0 0 1 1 0 0 0 0
1 0 1 0 1 0 1 0 0 0 0 1 0 0
0 0 0 1 0 0 1 1 0 0 1 0 0 0
1 0 1 0 1 0 1 0 0 0 0 1 0 0
1 0 1 0 1 0 1 0 0 0 0 1 0 0
0 0 0 1 0 0 1 1 0 0 1 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 1 0
0 0 0 0 0 0 0 0 0 0 0 0 1 0
0 0 0 0 0 1 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0

However, it takes some time to find the longest diagonal series of 1 in the matrix of 0 and 1. By improving the matrix generation method and setting tag variables, you can save this time. The new matrix generation method is as follows:

0 0 0 1 0 0 1 1 0 0 1 0 0 0
0 1 0 0 0 0 0 0 0 2 1 0 0 0
1 0 2 0 1 0 1 0 0 0 0 1 0 0
0 2 0 0 0 0 0 0 1 1 0 0 0 0
1 0 3 0 1 0 1 0 0 0 0 1 0 0
0 0 0 4 0 0 0 2 1 0 1 0 0 0
1 0 1 0 5 0 1 0 0 0 0 2 0 0
1 0 1 0 1 0 1 0 0 0 0 1 0 0
0 0 0 2 0 0 2 1 0 0 1 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 1 0
0 0 0 0 0 0 0 0 0 0 0 0 1 0
0 0 0 0 0 1 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0

Needless to say, you probably have seen it. When matching a character, we do not simply assign 1 to the corresponding element, but the value of the element in the upper left corner plus one. We use two marking variables to mark the position of the element with the largest median value in the Matrix. During the matrix generation process, we can determine whether the value of the currently generated element is the largest. Based on this, we can change the value of the marking variable, by the time the matrix is complete, the longest position and length of the matched substring have come out.

This is faster, but it takes too much space. We noticed that in the improved matrix generation method, each row is generated, and the previous row is useless. Therefore, we only need to use a one-dimensional array. The final code is as follows:

Private Function LCS (ByVal str_1 As String, ByVal str_2 As String) As String
If str_1 = "" Or str_2 = "" Then Return ""

Dim c (str_1.Length) As Integer
Dim max, maxj, I, j As Integer
Maxj = 0: max = 0 these two are the flag Variables
For I = 0 To str_2.Length-1
For j = str_1.Length-1 To 0 Step-1
If str_2.Chars (I) = str_1.Chars (j) Then
If I = 0 Or j = 0 Then
C (j) = 1
Else
C (j) = c (j-1) + 1
End If
Else
C (j) = 0
End If
If c (j)> max Then change> to> =, the last longest matched substring is returned.
Max = c (j): maxj = j update flag variable
End If
Next
Next

If max = 0 Then Return ""
Return str_1.Substring (maxj-max + 1, max) is obtained directly from the flag variable.
End Function
Here you can see the problem: what if there are multiple longest matching substrings? I can only return the first one here. You can change it to the last one. To completely return all the longest matched substrings, an array of variable variables is required. Are you interested in modifying?

Related Article

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.