-
Notifications
You must be signed in to change notification settings - Fork 27
/
10252.go
51 lines (45 loc) · 799 Bytes
/
10252.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
// UVa 10252 - Common Permutation
package main
import (
"fmt"
"os"
)
func charMap(s string) map[byte]int {
cm := make(map[byte]int)
for i := range s {
cm[s[i]]++
}
return cm
}
func commonPermutation(a, b string) map[byte]int {
cb := charMap(b)
cm := make(map[byte]int)
for k, va := range charMap(a) {
if vb, ok := cb[k]; ok {
cm[k] = min(va, vb)
}
}
return cm
}
func main() {
in, _ := os.Open("10252.in")
defer in.Close()
out, _ := os.Create("10252.out")
defer out.Close()
var a, b string
for {
if _, err := fmt.Fscanf(in, "%s\n%s", &a, &b); err != nil {
break
}
cm := commonPermutation(a, b)
var i byte
for i = 'a'; i <= 'z'; i++ {
if v, ok := cm[i]; ok {
for ; v > 0; v-- {
fmt.Fprintf(out, "%c", i)
}
}
}
fmt.Fprintln(out)
}
}