VB6求兩個字串最長公共子串的問題

2021-04-09 07:22:39 字數 1134 閱讀 3363

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技術中心,」

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,bch...

求兩個字串的最長公共子串

問題 有兩個字串str和str2,求出兩個字串中最長公共子串長度。比如 str acbcbcef,str2 abcbced,則str和str2的最長公共子串為bcbce,最長公共子串長度為5。演算法思路 1 把兩個字串分別以行和列組成乙個二維矩陣。2 比較二維矩陣中每個點對應行列字元中否相等,相等的...

求兩個字串的最長公共子串

def longestcommonsequence str one,str two,case sensitive true str one 和 str two 的最長公共子串行 param str one 字串1 param str two 字串2 正確結果 param case sensitive...