到目前为止我已经做到了
function lcs(xstr, ystr)
if xstr:len() == 0 or ystr:len() == 0 then
return ""
end
x = xstr:sub(1,1)
y = ystr:sub(1,1)
xs = xstr:sub(2)
ys = ystr:sub(2)
if x == y then
return x .. lcs(xs, ys)
else
l1 = lcs(xstr, ys)
l2 = lcs(xs, ystr)
if l1:len() > l2:len() then
return l1
else
return l2
end
end
end
print(lcs("abcd", "bcd"))
不幸的是,它只打印“d”而不是“bcd”。对我来说,“l2 = lcs(xs, ystr)”这一行似乎没有被执行,因为如果我在开头添加调试打印,它会打印出该函数没有被称为机智参数“bcd”和“bcd”,但我确信在 else 语句开始后值没问题。我将不胜感激任何帮助。