-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy path195.go
63 lines (54 loc) · 1.18 KB
/
195.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
// UVa 195 - Anagram
package main
import (
"fmt"
"io"
"os"
"sort"
)
var (
chars []byte
out io.WriteCloser
visited []bool
)
func isLower(a byte) bool { return a >= 'a' && a <= 'z' }
func isUpper(a byte) bool { return !isLower(a) }
func toUpper(a byte) byte { return a - 'a' + 'A' }
func dfs(i int, word []byte) {
if i == len(chars) {
fmt.Fprintln(out, string(word))
} else {
for j := range chars {
if !visited[j] && !(j > 0 && chars[j] == chars[j-1] && !visited[j-1]) {
visited[j] = true
word[i] = chars[j]
dfs(i+1, word)
visited[j] = false
}
}
}
}
func main() {
in, _ := os.Open("195.in")
defer in.Close()
out, _ = os.Create("195.out")
defer out.Close()
var n int
var line string
for fmt.Fscanf(in, "%d", &n); n > 0; n-- {
fmt.Fscanf(in, "%s", &line)
chars = []byte(line)
sort.Slice(chars, func(i, j int) bool {
if isLower(chars[i]) && isLower(chars[j]) || isUpper(chars[i]) && isUpper(chars[j]) {
return chars[i] <= chars[j]
}
if isLower(chars[i]) {
return toUpper(chars[i]) <= chars[j]
}
return chars[i] <= toUpper(chars[j])
})
word := make([]byte, len(chars))
visited = make([]bool, len(chars))
dfs(0, word)
}
}