问题描述:
有两个字符串,求最长公共子串的长度,例如
"ANKNGIEK"
"AKGBIOK"
上面这两个字符串,公共子序列为AKGIK
,长度为5。最长公共子序列就是说有一个字符串在两个字符串中都出现过,这里只考虑从头到尾的顺序,也就是说AKGIK
,这个字符串中的字母,在第一个字符串中出现的顺序,和在第二个字符串中出现的顺序是一样的。
第一种方法,从尾到头查找
假如有两个字符串,分别为ABCBDAB
和BDCABA
,其公共子串为BCBA
,(其实还有一个BDAB
,这里忽略多个子串的问题。)
就是从字符串的最后一个字母到第一个字母的顺序查找,过程如下:
X轴 | X0 | X1 | X2 | X3 | X4 | X5 | X6 | ||
---|---|---|---|---|---|---|---|---|---|
Y轴 | A |
B |
C |
B |
D |
A |
B |
||
Y0 | B |
4 | 4 | 4 | 4 | 3 | 2 | 1 | |
Y1 | D |
3 | 3 | 3 | 3 | 3 | 2 | 1 | |
Y2 | C |
3 | 3 | 3 | 2 | 2 | 2 | 1 | |
Y3 | A |
3 | 2 | 2 | 2 | 2 | 2 | 1 | |
Y4 | B |
2 | 2 | 2 | 2 | 1 | 1 | 1 | |
Y5 | A |
1 | 1 | 1 | 1 | 1 | 1 | 0 | |
0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
表格的所有格子初始值为0,首先从第一个字符串中拿出最后一个字母B
,到第二个字符串中从尾到头进行查找,如果出现相同的,就给交叉的格子加1.
完整代码
package main
import (
"fmt"
)
func main() {
str1 := "ANKNGIEK"
str2 := "AKGBIOK"
n := LCSlength(str1, len(str1),str2,len(str2))
fmt.Println(n)
}
var LCS [1024][1024]int
func LCSlength(x string, m int,y string,n int)int{
for i := m - 1; i >= 0; i-- {
for j := n - 1; j >= 0; j-- {
LCS[i][j] = LCS[i+1][j+1]
if x[i] == y[j] {
LCS[i][j]++
}
if LCS[i][j+1] > LCS[i][j] {
LCS[i][j] = LCS[i][j+1]
}
if LCS[i+1][j] > LCS[i][j] {
LCS[i][j] = LCS[i+1][j]
}
}
}
return LCS[0][0]
}
第二种方法,从头到尾查找
也就是从字符串的第一个字符一直查找到字符串的最后一个字符。思路与第一种没有什么不同,只是顺序完全倒过来,具体代码如下:
package main
import (
"fmt"
)
func main() {
str1 := "ABCBDABGNAIGIAENIGEGWM"
str2 := "BDCABAFANIGINIEINGINWM"
n := LCSlength(str1, len(str1), str2, len(str2))
fmt.Println(n)
}
var LCS [1024][1024]int
func LCSlength(x string, m int, y string, n int) int {
for i := 1; i <= m; i++ {
for j := 1; j <= n; j++ {
if x[i-1] == y[j-1] {
LCS[i][j] = LCS[i-1][j-1] + 1
} else {
LCS[i][j] = max(LCS[i][j-1], LCS[i-1][j])
}
}
}
return LCS[m][n]
}
func max(a, b int) int {
if a > b {
return a
}
return b
}
回头看一下第一种方法的另一种写法,就是思路完全一样, 只是顺序倒过来:
package main
import (
"fmt"
)
func main() {
str1 := "ABCBDABGNAIGIAENIGEGWM"
str2 := "BDCABAFANIGINIEINGINWM"
n := LCSlength(str1, len(str1), str2, len(str2))
fmt.Println(n)
}
var LCS [1024][1024]int
func LCSlength(x string, m int, y string, n int) int {
for i := m - 1; i >= 0; i-- {
for j := n - 1; j >= 0; j-- {
LCS[i][j] = LCS[i+1][j+1]
if x[i] == y[j] {
LCS[i][j]++
} else {
LCS[i][j] = max(LCS[i][j+1], LCS[i+1][j])
}
}
}
return LCS[0][0]
}
func max(a, b int) int {
if a > b {
return a
}
return b
}
输出最长公共子序列
上面用两种方法计算出了最长公共子序列的长度,接下来将最长公共子序列进行输出
package main
import (
"fmt"
)
func main() {
str1 := "ABCBDABGNAIGIAENIGEGWM"
str2 := "BDCABAFANIGINIEINGINWM"
str := LCSlength(str1, len(str1), str2, len(str2))
fmt.Println(str)
fmt.Println(len(str))
}
var LCS [1024][1024]int
func LCSlength(x string, m int, y string, n int) string {
//从头到尾查找,最后结果保存在表格的m,n位置,也就是表格的末尾
for i := 1; i <= m; i++ {
for j := 1; j <= n; j++ {
if x[i-1] == y[j-1] {
LCS[i][j] = LCS[i-1][j-1] + 1
} else {
LCS[i][j] = max(LCS[i][j-1], LCS[i-1][j])
}
}
}
var str string
for m > 0 && n > 0 {
if x[m-1] == y[n-1] {
//因为长度结果保存在表格末尾,所以从尾到头追加字符,
str += string(x[m-1])
m--
n--
} else if LCS[m][n-1] > LCS[m-1][n] {
n--
} else {
m--
}
}
//最后将得到的字符串反转
return reverseStr(str)
}
func max(a, b int) int {
if a > b {
return a
}
return b
}
//字符串反转,因为最后得到的字符串是反过来的。
func reverseStr(s string) string {
runes := []rune(s)
for from, to := 0, len(runes)-1; from < to; from, to = from+1, to-1 {
runes[from], runes[to] = runes[to], runes[from]
}
return string(runes)
}
接下来用从尾到头查找的方法,最后长度结果保存在表格0,0里面,所以采取依次从头到尾追加字符的方法,最后不用反转字符串。
package main
import (
"fmt"
)
func main() {
str1 := "ABCBDABGNAIGIAENIGEGWM"
str2 := "BDCABAFANIGINIEINGINWM"
str := LCSlength(str1, len(str1), str2, len(str2))
fmt.Println(str)
fmt.Println(len(str))
}
var LCS [1024][1024]int
func LCSlength(x string, m int, y string, n int) string {
//从尾到头查找
for i := m - 1; i >= 0; i-- {
for j := n - 1; j >= 0; j-- {
LCS[i][j] = LCS[i+1][j+1]
if x[i] == y[j] {
LCS[i][j]++
}
if LCS[i][j+1] > LCS[i][j] {
LCS[i][j] = LCS[i][j+1]
}
if LCS[i+1][j] > LCS[i][j] {
LCS[i][j] = LCS[i+1][j]
}
}
}
var str string
a := 0
b := 0
for a < m && b < n {
if x[a] == y[b] {
str += string(x[a])
a++
b++
} else if LCS[a][b+1] < LCS[a+1][b] {
a++
} else {
b++
}
}
return str
}
package main
import (
"fmt"
)
func main() {
str1 := "ABCBDABGNAIGIAENIGEGWM"
str2 := "BDCABAFANIGINIEINGINWM"
str := LCSlength(str1, len(str1), str2, len(str2))
fmt.Println(str)
fmt.Println(len(str))
}
var LCS [1024][1024]int
func LCSlength(x string, m int, y string, n int) string {
//从尾到头查找,最后结果保存在0,0中,
for i := m - 1; i >= 0; i-- {
for j := n - 1; j >= 0; j-- {
LCS[i][j] = LCS[i+1][j+1]
if x[i] == y[j] {
LCS[i][j]++
} else {
LCS[i][j] = max(LCS[i][j+1], LCS[i+1][j])
}
}
}
var str string
a := 0
b := 0
for a < m && b < n {
if x[a] == y[b] {
//因为长度结果保存在0,0中,所以从头到尾追加字符,最后结果不用反转。
str += string(x[a])
a++
b++
} else if LCS[a][b+1] < LCS[a+1][b] {
a++
} else {
b++
}
}
return str
}
func max(a, b int) int {
if a > b {
return a
}
return b
}