我想实现一个执行以下 I/O 的 LCS 版本:
输入:superLCS('cat','car')
输出:['ca#','ca#']
目前,我的程序适用于此,但如果字母不合适,它就不起作用。
例如,如果输入是:superLCS('art','cad'),则输出 ['###','###']。它应该输出 ['a##','#a#']
代码:
def superLCS(s1,s2):
return helper(s1,s2,'','')
def helper(s1,s2,res1,res2): #s1 is string 1, s2 is string 2, res1 is result1, res2 is result2
if s1 == '' or s2 == '': #if either string is empty return the result
return [res1,res2]
if s1[0] == s2[0]: #if they are equal, put their string in the list
res1 += s1[0]
res2 += s1[0]
return helper(s1[1:],s2[1:],res1,res2)
else: #if they arent, add a # to the list
res2 += '#'
res1 += '#'
return helper(s1[1:],s2[1:],res1,res2)