-
Notifications
You must be signed in to change notification settings - Fork 27
/
10008.go
59 lines (53 loc) · 860 Bytes
/
10008.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
// UVa 10008 - What's Cryptanalysis?
package main
import (
"fmt"
"io"
"os"
"sort"
)
type dict struct {
c byte
n int
}
func output(out io.Writer, d map[byte]int) {
arr := make([]dict, len(d))
i := 0
for k, v := range d {
arr[i] = dict{k, v}
i++
}
sort.Slice(arr, func(i, j int) bool {
if arr[i].n == arr[j].n {
return arr[i].c < arr[j].c
}
return arr[i].n > arr[j].n
})
for _, v := range arr {
fmt.Fprintf(out, "%c %d\n", v.c, v.n)
}
}
func main() {
in, _ := os.Open("10008.in")
defer in.Close()
out, _ := os.Create("10008.out")
defer out.Close()
var n, ln int
var c byte
d := make(map[byte]int)
fmt.Fscanf(in, "%d", &n)
for {
if fmt.Fscanf(in, "%c", &c); c == '\n' {
if ln++; ln == n {
break
}
}
if c >= 'a' && c <= 'z' {
c -= 32
}
if c >= 'A' && c <= 'Z' {
d[c]++
}
}
output(out, d)
}