VB6求两个字符串最长公共子串的问题

Function LCS(ByVal A As String, ByVal B As String) As String
If Len(A) * Len(B) = 0 Then LCS = "": Exit Function
Dim la As Integer, lb As Integer, achar() As String, bchar() As String, c() As Integer, i As Integer, j As Integer, max As Integer

la = Len(A)
lb = Len(B)

ReDim achar(la - 1)
ReDim bchar(lb - 1)
ReDim c(la - 1)

For i = 1 To la
achar(i - 1) = Mid(A, i, 1)
Next

For i = 1 To lb
bchar(i - 1) = Mid(B, i, 1)
Next

max = 0
For i = 0 To lb - 1
For j = la - 1 To 0 Step -1
If bchar(i) = achar(j) Then
If i * 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 max = c(j): LCS = Mid(A, j + 2 - max, max)

Next
Next
If max = 0 Then LCS = ""

End Function 

 

Private Sub Command1_Click()
MsgBox LCS("如果你想推荐本文到CSDN 技术中心,请选择下列的文章分类之一。文章保存时将自动提交到CSDN技术中心,通过审核后本文将出现在您的CSDN 技术中心的专栏中。", "如果您不希望本文被提交到CSDN技术中心,请选择""不发表到CSDN技术中心""")
End Sub

返回 “提交到CSDN技术中心,”

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值