0
我想实现一个版本LCS的,做了以下I/O:Python字符串LCS
输入:superLCS( '猫', '汽车')
输出: 'CA#' ,'ca#']
目前,我的程序适用于此,但如果字母不合适,它不起作用。例如,如果输入是:superLCS('art','cad'),则输出['###','###']。应该输出[ '一##', '#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)