Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'in 9 15 7 6 11 12 4 out 9 11 12 ,輸出要求,和最大的LIS
Dim ins = "9 15 7 6 11 12 4"
Dim data1 = ins.Split(separator:=" ", options:=System.StringSplitOptions.RemoveEmptyEntries)
Dim n = UBound(data1)
Dim d(n) As Int16
For i = 0 To n
d(i) = data1(i)
Next
Dim d1(n) As Int16
d.CopyTo(d1, 0)
Array.Sort(d1)
Dim c(n, n)
Dim cf(n, n)
For i = 0 To n
c(i, 0) = 0
c(0, i) = 0
Next i
Dim path As New ArrayList
For i = 1 To n
For j = 1 To n
If d(i) = d1(j) Then
c(i, j) = c(i - 1, j - 1) + 1
cf(i, j) = 3
path.Add(d(i))
Else
c(i, j) = Math.Max(c(i, j - 1), c(i - 1, j))
If c(i, j) = c(i, j - 1) Then cf(i, j) = 2
If c(i, j) = c(i - 1, j) Then cf(i, j) = 1
End If
Next
Next
MsgBox(c(n, n))
Dim ss = ""
For i = 0 To n
For j = 0 To n
ss = ss & c(i, j)
Next
ss = ss & vbNewLine
Next
MsgBox(ss)
Dim pathArray = path.ToArray
lcs(pathArray, cf, n, n)
MsgBox(sss)
End Sub
Dim sss = ""
Sub lcs(ByVal pathArray, ByVal cf, ByVal i, ByVal j)
If i = 0 Or j = 0 Then
Return
ElseIf cf(i, j) = 3 Then
lcs(pathArray, cf, i - 1, j - 1)
sss = sss & pathArray(i - 1) & " "
ElseIf cf(i, j) = 1 Then
lcs(pathArray, cf, i - 1, j)
Else
lcs(pathArray, cf, i, j - 1)
End If
End Sub
End Class
沒有留言:
張貼留言