-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy path850.go
79 lines (70 loc) · 1.36 KB
/
850.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
78
79
// UVa 850 - Crypt Kicker II
package main
import (
"bufio"
"fmt"
"os"
"strings"
)
const base = "the quick brown fox jumps over the lazy dog"
func match(line string) map[byte]byte {
if len(line) != len(base) {
return nil
}
dict := make(map[byte]byte)
for i := range line {
if b, ok := dict[line[i]]; ok && b != base[i] {
return nil
}
dict[line[i]] = base[i]
}
return dict
}
func decodeLine(line string, dict map[byte]byte) string {
d := make([]byte, len(line))
for i := range line {
d[i] = dict[line[i]]
}
return string(d)
}
func decode(lines []string) []string {
var dict map[byte]byte
for _, line := range lines {
if dict = match(line); dict != nil {
break
}
}
if dict == nil {
return []string{"no solution"}
}
decoded := make([]string, len(lines))
for i, line := range lines {
decoded[i] = decodeLine(line, dict)
}
return decoded
}
func main() {
in, _ := os.Open("850.in")
defer in.Close()
out, _ := os.Create("850.out")
defer out.Close()
s := bufio.NewScanner(in)
s.Split(bufio.ScanLines)
var kase int
var line string
s.Scan()
fmt.Sscanf(s.Text(), "%d", &kase)
for s.Scan(); kase > 0; kase-- {
var lines []string
for s.Scan() {
if line = s.Text(); line == "" {
break
}
lines = append(lines, line)
}
fmt.Fprintln(out, strings.Join(decode(lines), "\n"))
if kase > 1 {
fmt.Fprintln(out)
}
}
}