AcWing 897. 最长公共子序列-golang
原题链接
简单
作者:
一只鱼
,
2021-03-20 16:59:06
,
所有人可见
,
阅读 285
最长公共子序列-golang
golang
package main
import "fmt"
var n, m int
var strA, strB string
var arrA, arrB []rune
var f [][]int
func main(){
fmt.Scanf("%d%d", &n, &m)
f = make([][]int, n+1)
for i := 0; i <= n; i++{
f[i] = make([]int, m+1)
}
fmt.Scanf("%s", &strA)
fmt.Scanf("%s", &strB)
arrA, arrB = make([]rune, n +1), make([]rune, m +1)
for k, v := range strA{
arrA[k+1] = v
}
for k, v := range strB{
arrB[k+1] = v
}
for i := 1; i <= n; i++{
for j := 1; j <= m; j++{
if arrA[i] == arrB[j]{
f[i][j] = f[i-1][j-1] +1
}else{
f[i][j] = max(f[i-1][j], f[i][j-1])
}
}
}
fmt.Println(f[n][m])
}
func max(a, b int)int{
if a > b {
return a
}
return b
}