-
Notifications
You must be signed in to change notification settings - Fork 1
/
common_child.go
70 lines (57 loc) · 1.23 KB
/
common_child.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package main
import (
"bufio"
"fmt"
"os"
)
func commonChild(s1 string, s2 string) int {
var i, j int
m, n := len(s1), len(s2)
longestCommonSubsequence := make([][]int, m+1)
for i = range longestCommonSubsequence {
longestCommonSubsequence[i] = make([]int, n+1)
}
for i = 1; i <= m; i++ {
for j = 1; j <= n; j++ {
if s1[i-1] == s2[j-1] {
longestCommonSubsequence[i][j] = longestCommonSubsequence[i-1][j-1] + 1
} else {
longestCommonSubsequence[i][j] = max(
longestCommonSubsequence[i-1][j],
longestCommonSubsequence[i][j-1])
}
}
}
return longestCommonSubsequence[m][n]
}
func max(lhs int, rhs int) int {
if lhs > rhs {
return lhs
}
return rhs
}
func main() {
stdin, err := os.Open(os.Getenv("INPUT_PATH"))
if err != nil {
stdin = os.Stdin
}
defer stdin.Close()
stdout, err := os.Create(os.Getenv("OUTPUT_PATH"))
if err != nil {
stdout = os.Stdout
}
defer stdout.Close()
reader := bufio.NewReaderSize(stdin, 1024*1024)
writer := bufio.NewWriterSize(stdout, 1024*1024)
var s1, s2 string
_, err = fmt.Fscan(reader, &s1, &s2)
checkError(err)
result := commonChild(s1, s2)
fmt.Fprintln(writer, result)
writer.Flush()
}
func checkError(err error) {
if err != nil {
panic(err)
}
}