给定两个字符串str1和str2,输出两个字符串的最长公共子串
题目保证str1和str2的最长公共子串存在且唯一。
//时空Omn
func LCS( str1 string , str2 string ) string {
m, n := len(str1), len(str2) //行,列
res, end := 0, 0
dp := make([][]int, m+1)
for i:= range dp {
dp[i] = make([]int, n+1)
}
for i:=1; i <= m; i++{
for j:=1; j <= n; j++{
if str1[i-1] == str2[j-1] {
dp[i][j] = dp[i-1][j-1] + 1
} else {
dp[i][j] = 0
}
if dp[i][j] > res {
res = dp[i][j]
end = i // 注意
}
}
}
if res == 0 {
return ""
}
return str1[end-res: end]
}