-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheconom.go
More file actions
89 lines (76 loc) · 1.24 KB
/
Copy patheconom.go
File metadata and controls
89 lines (76 loc) · 1.24 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// <start> ::= LETTER | <expression>
// <expression> ::= OPEN <operation> CLOSE | LETTER
// <operation> ::= OPERATOR <expression> <expression>
package main
import (
"fmt"
"unicode/utf8"
"unicode"
"bufio"
"os"
)
type RPN struct {
s string
current int
dict map[string]int
}
func (e *RPN) Peek() byte {
return e.s[e.current]
}
func (e *RPN) HasNext() bool {
return e.current < utf8.RuneCountInString(e.s)
}
func (e *RPN) Parse() int {
if (! e.HasNext()) {
return len(e.dict)
} else {
if (e.Peek() == '(') {
e.Expression()
return e.Parse()
} else {
e.Letter()
return e.Parse()
}
}
}
func (e *RPN) Next() {
e.current++
}
func (e *RPN) Expression() {
if (unicode.IsLetter(rune(e.Peek()))) {
e.Letter()
} else {
start := e.current
e.Open()
e.Operator()
e.Expression()
e.Expression()
end := e.current
expr := e.s[start:end]
_, ok := e.dict[expr]
if (! ok) {
e.dict[expr]++
}
e.Close()
}
}
func (e *RPN) Letter() {
e.Next()
}
func (e *RPN) Open() {
e.Next()
}
func (e *RPN) Close() {
e.Next()
}
func (e *RPN) Operator() {
e.Next()
}
func main() {
var n RPN
reader := bufio.NewReader(os.Stdin)
n.current = 0
n.dict = make(map[string]int)
n.s, _ = reader.ReadString('\n')
fmt.Println(n.Parse())
}