-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy path531.go
More file actions
64 lines (58 loc) · 1.03 KB
/
531.go
File metadata and controls
64 lines (58 loc) · 1.03 KB
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
// UVa 531 - Compromise
package main
import (
"bufio"
"fmt"
"os"
"strings"
)
func lcs(a, b []string) string {
l1, l2 := len(a), len(b)
dp := make([][][]string, l1+1)
for i := range dp {
dp[i] = make([][]string, l2+1)
}
for i := 1; i <= l1; i++ {
for j := 1; j <= l2; j++ {
if a[i-1] == b[j-1] {
dp[i][j] = append(dp[i-1][j-1], a[i-1])
} else {
if len(dp[i-1][j]) > len(dp[i][j-1]) {
dp[i][j] = dp[i-1][j]
} else {
dp[i][j] = dp[i][j-1]
}
}
}
}
return strings.Join(dp[l1][l2], " ")
}
func main() {
in, _ := os.Open("531.in")
defer in.Close()
out, _ := os.Create("531.out")
defer out.Close()
s := bufio.NewScanner(in)
s.Split(bufio.ScanLines)
var a, b []string
var l string
first := true
for s.Scan() {
if l = s.Text(); l == "" {
break
}
if l == "#" {
if first = !first; first {
fmt.Fprintln(out, lcs(a, b))
a, b = nil, nil
}
} else {
tokens := strings.Fields(l)
if first {
a = append(a, tokens...)
} else {
b = append(b, tokens...)
}
}
}
}