模拟二进制进位、字符串的子系列
Private Sub CommandButton5_Click() ''LCS
''先试试求子串 '' 25,S
Dim S1, S2, S3 As String
Dim i, j, n, m As Integer
Dim A() As Integer
Dim bEnd, bJW As Boolean ''结束标志,进位标志
Dim SN As Long
''====start=====
S1 = Cells(25, "S")
n = Len(S1)
ReDim A(1 To n)
For i = 1 To n
A(i) = 0
Next i
bEnd = False
m = 0
While Not bEnd
'' AddOne
i = 1: bJW = True
While i <= n And bJW
If A(i) = 0 Then
A(i) = 1
bJW = False
Else
A(i) = 0
bJW = True
End If
i = i + 1
Wend
bEnd = True
S2 = ""
S3 = ""
For i = 1 To n
S2 = S2 & A(i)
If A(i) = 1 Then
S3 = S3 & Mid(S1, i, 1)
End If
If A(i) = 0 Then
bEnd = False
End If
Next i
m = m + 1
Cells(m + 26, "R") = m
Cells(m + 26, "S") = "'" + S2
Cells(m + 26, "T") = "'" + S3
Wend
MsgBox "Ooooooook!!!"
End Sub
一个长度为n=len(s)的字符串,很容易证明,它的子系列有2^n-1个(不包含空串),上面代码实现了输入任意字符串,输出它的全部子系列的功能。其中较核心的算法是用一个长度为n的一维数组模拟二进制的进位操作,形成100~111系列(以3位为例),字符串ABC ,它的子系列为:
用生成的二进制,按位拼接字符串(该位为1就输出,0就忽略)。