-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy path630.go
65 lines (58 loc) · 1.18 KB
/
630.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
// UVa 630 - Anagrams (II)
package main
import (
"fmt"
"io"
"os"
"sort"
"strings"
)
var vocabulary [][2]string
func solve(out io.Writer, word string) {
fmt.Fprintf(out, "Anagrams for: %s\n", word)
var count int
sw := sortWord(word)
for _, w := range vocabulary {
if sw == w[1] {
count++
fmt.Fprintf(out, "%3d) %s\n", count, w[0])
}
}
if count == 0 {
fmt.Fprintf(out, "No anagrams for: %s\n", word)
}
}
func sortWord(word string) string {
bytes := strings.Split(word, "")
sort.Strings(bytes)
return strings.Join(bytes, "")
}
func main() {
in, _ := os.Open("630.in")
defer in.Close()
out, _ := os.Create("630.out")
defer out.Close()
var t, n int
var word string
first := true
for fmt.Fscanf(in, "%d", &t); t > 0; t-- {
fmt.Fscanf(in, "\n%d", &n)
vocabulary = make([][2]string, n)
for i := range vocabulary {
fmt.Fscanf(in, "%s", &vocabulary[i][0])
vocabulary[i][1] = sortWord(vocabulary[i][0])
}
sort.Slice(vocabulary, func(i, j int) bool { return vocabulary[i][0] < vocabulary[j][0] })
if first {
first = false
} else {
fmt.Fprintln(out)
}
for {
if fmt.Fscanf(in, "%s", &word); word == "END" {
break
}
solve(out, word)
}
}
}