LCS
Algorithm
Summary
LCS 问题有很多的子问题,大致包括以下:
-
最长公共子串,要求子串在原字符串中是连续的
-
最长公共子序列
-
最大子序列
-
最长递增子序列
最长公共子串
暴力求解
def LCSubStr(str1, str2, m, n):
LCSuff = [[0 for i in range(n + 1)] for j in range(m + 1)]
# 最长公共子串的长度
result = 0
# str1 中的最后相同的位置
p = 0
for i in range(m + 1):
for j in range(n + 1):
if(i == 0 or j == 0):
LCSuff[i][j] = 0
elif(str1[i - 1] == str2[j - 1]):
LCSuff[i][j] = LCSuff[i-1][j-1] + 1
result = max(result, LCSuff[i][j])
p = i
else:
LCSuff[i][j] = 0
return str1[p - result:p], result
X = 'OldSite:GeeksforGeeks.org'
Y = 'NewSite:GeeksQuiz.com'
m = len(X)
n = len(Y)
print('Length of Longest Common Substring is',
LCSubStr(X, Y, m, n)) 修改历史10 次提交
- refactor: reorganize documentation structure and update Navbar componentxiaocheng··
2fb8f42 - chore(project): clean up obsolete configuration and build artifactsxiaocheng··
3574bd3 - new struct for lblogsxiaocheng··
8c9b28e - move blogs to docsxiaocheng··
c8535a0 - update codechenweigao··
e4c6887 - update fix: timeline errorchenweigao··
fb4b591 - update file structurechenweigao··
3456ab7 - update Datechenweigao··
3c14689 - init v3chenweigao··
b770fc2 - init v2chenweigao··
505f81b