-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy path755.go
75 lines (69 loc) · 1.48 KB
/
755.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
// UVa 755 - 487--3279
package main
import (
"fmt"
"os"
"sort"
"strconv"
"strings"
)
var charMap = map[byte]int{
'A': 2, 'B': 2, 'C': 2,
'D': 3, 'E': 3, 'F': 3,
'G': 4, 'H': 4, 'I': 4,
'J': 5, 'K': 5, 'L': 5,
'M': 6, 'N': 6, 'O': 6,
'P': 7, 'R': 7, 'S': 7,
'T': 8, 'U': 8, 'V': 8,
'W': 9, 'X': 9, 'Y': 9,
}
func solve(telephones []string) map[string]int {
numbers := make(map[string]int)
for _, telephone := range telephones {
var number strings.Builder
for j := range telephone {
switch {
case telephone[j] >= '0' && telephone[j] <= '9':
number.WriteByte(telephone[j])
case telephone[j] >= 'A' && telephone[j] <= 'Z':
number.WriteString(strconv.Itoa(charMap[telephone[j]]))
}
}
numbers[number.String()]++
}
return numbers
}
func main() {
in, _ := os.Open("755.in")
defer in.Close()
out, _ := os.Create("755.out")
defer out.Close()
var kase, n int
for fmt.Fscanf(in, "%d", &kase); kase > 0; kase-- {
if _, err := fmt.Fscanf(in, "\n%d", &n); err != nil {
break
}
telephones := make([]string, n)
for i := range telephones {
fmt.Fscanf(in, "%s", &telephones[i])
}
numbers := solve(telephones)
var dups []string
for k, v := range numbers {
if v > 1 {
dups = append(dups, k)
}
}
if len(dups) == 0 {
fmt.Fprintln(out, "No duplicates.")
} else {
sort.Strings(dups)
for _, dup := range dups {
fmt.Fprintf(out, "%s-%s %d\n", dup[:3], dup[3:], numbers[dup])
}
}
if kase > 1 {
fmt.Fprintln(out)
}
}
}