def C1(i, j):
x = " " + i_word
y = " " + j_word
if i == 0 or j == 0:
return 0
elif (i > 0 and j > 0) and x[i] == y[j]:
return C1(i-1, j-1) + 1
elif (i > 0 and j > 0) and x[i] != y[j]:
print(i, j)
return max(C1(i-1, j), C1(i, j-1))
def C2(i, j, k):
x = " " + i_word
y = " " + j_word
z = " " + k_word
if i == 0 or j == 0 or k == 0:
return 0
elif (i > 0 and j > 0 and k > 0) and x[i] == y[j] == z[k]:
return C2(i-1, j-1, k-1) + 1
elif (i > 0 and j > 0 and k > 0) and x[i] != y[j] != z[k]:
return max(C2(i-1, j, k), C2(i, j-1, k), C2(i, j, k-1))
我試著用這樣的方法寫出了求兩個字串和求三個字串LCS的程式碼,兩個字串的可以正常運行,但不知道為什麼三個字串的會回傳出None來,希望各位可以為小弟解答