这篇博客的目的是为了提醒自己,以后笔试一定得及时交啊?!!!!
阿里笔试的最后一个题,我顺手就写python实现,代码如下,不知道对不对,反正当时好几个数据都过了。
不过,反正没交上去,那个题填代码的地方是空的,对错都没用了。先记下,改天细究:
def largest_common(query, text, is_first_same):
if (len(query) == 0 or len(text) == 0):
return 0
else:
a = query[0]
b = text[0]
if is_first_same:
if (a == b):
first_same_l = largest_common(query[1:], text[1:],True)+1
else:
first_same_l = 0
first_diff_l = max(largest_common(query[1:], text[0:],False), largest_common(query[0:],text[1:],False))
else:
if (a == b):
first_same_l = largest_common(query[1:], text[1:],True)
else:
first_same_l = 0
first_diff_l = max(largest_common(query[1:], text[0:],False), largest_common(query[0:],text[1:],False))
if first_same_l > first_diff_l :
return first_same_l
else:
return first_diff_l
if __name__ =='__main__':
a = "acbac"
b = "accbaccbabb"
print largest_common(a, b, True)
原文地址:http://blog.csdn.net/xia7139/article/details/38930319