最长公共子串(The Longest Common Substring)
LCS问题就是求两个字符串最长公共子串的问题。解法就是用一个矩阵来记录两个字符串中所有位置的两个字符之间的匹配情况,若是匹配则为1,否则为0。然后求出对角线最长的1的序列,其对应的位置就是最长匹配子串的位置。
def find_lcsubstr(s1, s2):
m=[[0 for i in range(len(s2)+1)] for j in range(len(s1)+1)] #生成0矩阵,为方便后续计算,比字符串
def sub(arr):
finish=[] # the list containing all the subsequences of the specified sequence
size = len(arr) # the number of elements in the specified sequence
end = 1 j) % 2: # this result is 1, so do not have to write ==