-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy path536.go
47 lines (41 loc) · 821 Bytes
/
536.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
// UVa 536 - Tree Recovery
package main
import (
"fmt"
"os"
)
func index(n byte, lst []byte) int {
for i := range lst {
if n == lst[i] {
return i
}
}
return -1
}
func post(preord, inord []byte) []byte {
if len(preord) == 0 {
return []byte{}
}
idx := index(preord[0], inord)
return append(append(post(preord[1:idx+1], inord[0:idx]), post(preord[idx+1:], inord[idx+1:])...), preord[0])
}
func main() {
in, _ := os.Open("536.in")
defer in.Close()
out, _ := os.Create("536.out")
defer out.Close()
var line string
for {
if _, err := fmt.Fscanf(in, "%s", &line); err != nil {
break
}
preord := []byte(line)
fmt.Fscanf(in, "%s", &line)
inord := []byte(line)
postord := post(preord, inord)
for i := range postord {
fmt.Fprintf(out, "%c", postord[i])
}
fmt.Fprintln(out)
}
}