-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy path454.go
69 lines (61 loc) · 1.14 KB
/
454.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
// UVa 454 - Anagrams
package main
import (
"bufio"
"fmt"
"io"
"os"
"sort"
"strings"
)
var keepLetter = func(r rune) rune {
if r >= 'a' && r <= 'z' || r >= 'A' && r <= 'Z' {
return r
}
return -1
}
func solve(out io.Writer, words []string) {
wordMap := make(map[string]string)
for _, word := range words {
s := strings.Split(strings.Map(keepLetter, word), "")
sort.Strings(s)
wordMap[word] = strings.Join(s, "")
}
sort.Strings(words)
for i := 0; i < len(words)-1; i++ {
for j := i + 1; j < len(words); j++ {
if wordMap[words[i]] == wordMap[words[j]] {
fmt.Fprintf(out, "%s = %s\n", words[i], words[j])
}
}
}
}
func main() {
in, _ := os.Open("454.in")
defer in.Close()
out, _ := os.Create("454.out")
defer out.Close()
s := bufio.NewScanner(in)
s.Split(bufio.ScanLines)
var kase int
var words []string
var line string
first := true
s.Scan()
fmt.Sscanf(s.Text(), "%d", &kase)
for s.Scan(); kase > 0; kase-- {
for s.Scan() {
if line = s.Text(); line == "" {
break
}
words = append(words, line)
}
if first {
first = false
} else {
fmt.Fprintln(out)
}
solve(out, words)
words = nil
}
}