-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy path10192.go
50 lines (43 loc) · 810 Bytes
/
10192.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
// UVa 10192 - Vacation
package main
import (
"bufio"
"fmt"
"os"
)
func lcs(c1, c2 []byte) int {
l1, l2 := len(c1), len(c2)
dp := make([][]int, l1+1)
for i := range dp {
dp[i] = make([]int, l2+1)
}
for i := 1; i <= l1; i++ {
for j := 1; j <= l2; j++ {
if c1[i-1] == c2[j-1] {
dp[i][j] = dp[i-1][j-1] + 1
} else {
dp[i][j] = max(dp[i-1][j], dp[i][j-1])
}
}
}
return dp[l1][l2]
}
func main() {
in, _ := os.Open("10192.in")
defer in.Close()
out, _ := os.Create("10192.out")
defer out.Close()
s := bufio.NewScanner(in)
s.Split(bufio.ScanLines)
var count int
for s.Scan() {
s1 := s.Text()
if s1 == "#" {
break
}
s.Scan()
s2 := s.Text()
count++
fmt.Fprintf(out, "Case #%d: you can visit at most %d cities.\n", count, lcs([]byte(s1), []byte(s2)))
}
}