Private Sub lcs(i As Integer, j As Integer, x As String, b() As Integer)
If i = 0 Or j = 0 Then
Exit Sub
ElseIf b(i, j) = 1 Then
Call lcs(i - 1, j - 1, x, b)
Text3.Text = Text3.Text + Mid(x, i, 1)
ElseIf b(i, j) = 2 Then
Call lcs(i - 1, j, x, b)
Else: Call lcs(i, j - 1, x, b)
End If
End Sub
Private Sub Command1_Click()
Dim x As String
Dim y As String
Dim m As Integer
Dim n As Integer
Dim i As Integer
Dim j As Integer
Dim c() As Integer
Dim b() As Integer
x = Text1.Text
y = Text2.Text
m = Len(Text1.Text)
n = Len(Text2.Text)
ReDim c(0 To m, 0 To n) As Integer
ReDim b(0 To m, 0 To n) As Integer
For i = 1 To m
c(i, 0) = 0
Next i
For i = 1 To n
c(0, i) = 0
Next i
For i = 1 To m
For j = 1 To n
If Mid(x, i, 1) = Mid(y, j, 1) Then
c(i, j) = c(i - 1, j) + 1
b(i, j) = 1
ElseIf (c(i - 1, j) >= c(i, j - 1)) Then
c(i, j) = c(i - 1, j)
b(i, j) = 2
Else: c(i, j) = c(i, j - 1)
b(i, j) = 3
End If
Next j
Next i
Call lcs(i - 1, j - 1, x, b)
End Sub
Private Sub Command2_Click()
Text1.Text = ""
Text2.Text = ""
Text3.Text = ""
End Sub