一个简单的算法关于取不同的,希望能够得到帮助
现在想要实现 取两个字符串中不同的部分组成新的字符串。
例子:字符串1: 310 字符串2:210 取不同后新的字符串3:32
不要给例子的做法。想要试用所有字符串的
[解决办法]
dim s1 as string
dim s2 as string
dim s3 as string
dim p1 as long,L1 as long
dim s as string
randomize
L=len(s1)
p1=rnd*l
l1=(l-p1)*rnd
s=mid(s1,p1,l1)
L=len(s2)
p1=rnd*L
l1=(l-p1)*rnd
s=s & mid(s2,p1,l1)
[解决办法]
Option Explicit
Public Function Link_Diff(ByVal String1 As String, ByVal String2 As String) As String
Dim tmp1 As String, tmp2 As String, p As String
Dim i As Long, j As Long
If Len(String1) <= Len(String2) Then
tmp1 = String1
tmp2 = String2
Else
tmp1 = String2
tmp2 = String1
End If
i = 0
Do While (i < Len(tmp1))
j = Len(tmp1) - i
Do While j
p = Mid(tmp1, i + 1, j)
If InStr(tmp2, p) Then
tmp1 = Replace(tmp1, p, "")
tmp2 = Replace(tmp2, p, "")
End If
j = j - 1
Loop
i = i + 1
Loop
If Len(String1) <= Len(String2) Then
Link_Diff = tmp1 & tmp2
Else
Link_Diff = tmp2 & tmp1
End If
End Function
Private Sub Command1_Click()
Text3 = Link_Diff(Text1, Text2)
End Sub