-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy path213.go
77 lines (68 loc) · 1.23 KB
/
213.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
71
72
73
74
75
76
77
// UVa 213 - Message Decoding
package main
import (
"bufio"
"fmt"
"io"
"os"
"strconv"
"strings"
)
var s *bufio.Scanner
func buildKey(header string) map[string]byte {
keyMap := make(map[string]byte)
var key int64
digit := uint(1)
for i := range header {
if key == (2<<(digit-1) - 1) {
key = 0
digit++
}
binary := strconv.FormatInt(key, 2)
binary = strings.Repeat("0", int(digit)-len(binary)) + binary
keyMap[binary] = header[i]
key++
}
return keyMap
}
func getLength(l string) int {
var n int
fmt.Sscanf(l, "%b", &n)
return n
}
func solve(out io.Writer, header string) {
keyMap := buildKey(header)
var l int
var line, code string
s.Scan()
for line = s.Text(); line != "000"; {
l, line = getLength(line[:3]), line[3:]
for {
code, line = line[:l], line[l:]
if len(line) < l {
s.Scan()
line += s.Text()
}
if strings.Count(code, "1") == l {
break
}
fmt.Fprintf(out, "%c", keyMap[code])
}
}
fmt.Fprintln(out)
}
func main() {
in, _ := os.Open("213.in")
defer in.Close()
out, _ := os.Create("213.out")
defer out.Close()
s = bufio.NewScanner(in)
s.Split(bufio.ScanLines)
var line string
for s.Scan() {
if line = s.Text(); line == "" {
break
}
solve(out, line)
}
}