-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
48 lines (44 loc) · 881 Bytes
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package longestcommonsubsequence
func LongestCommonSubsequence(text1 string, text2 string) int {
var dp func(int, int) int
dp = func(i, j int) int {
if i == -1 || j == -1 {
return 0
}
if text1[i] == text2[j] {
return dp(i-1, j-1) + 1
}
if text1[i] != text2[j] {
return max(dp(i-1, j), dp(i, j-1))
}
return 0
}
return dp(len(text1)-1, len(text2)-1)
}
// DP table 優化
func LongestCommonSubsequenceDP(text1 string, text2 string) int {
m, n := len(text1), len(text2)
if m == 0 || n == 0 {
return 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 text1[i-1] == text2[j-1] {
dp[i][j] = dp[i-1][j-1] + 1
} else {
dp[i][j] = max(dp[i][j-1], dp[i-1][j])
}
}
}
return dp[m][n]
}
func max(a, b int) int {
if a > b {
return a
}
return b
}