forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
173 lines (131 loc) · 4.8 KB
/
main.cpp
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/// Source : https://leetcode.com/problems/parse-lisp-expression/description/
/// Author : liuyubobobo
/// Time : 2017-11-27
#include <iostream>
#include <string>
#include <unordered_map>
#include <cassert>
/// Parsing
/// Time Complexity: O(n)
/// Space Complexity: O(n)
using namespace std;
class Solution {
public:
int evaluate(string expression) {
int end;
int res = evaluate(expression, 0, end, unordered_map<string, int>());
assert(end == expression.size());
return res;
}
private:
int evaluate(const string& expression, int start, int& end,
const unordered_map<string, int>& table){
if(expression[start] == '-' || expression[start] == '+' || isdigit(expression[start])){
end = nextNonDigit(expression, start + 1);
return stoi(expression.substr(start, end - start));
}
if(expression[start] != '('){
end = nextNonLetter(expression, start + 1);
string var = expression.substr(start, end - start);
assert(table.find(var) != table.end());
return table.find(var)->second;
}
assert(expression[start] == '(');
// add
if(expression[start + 1] == 'a'){
assert(expression[start + 2] == 'd');
assert(expression[start + 3] == 'd');
assert(expression[start + 4] == ' ');
int end1;
int e1 = evaluate(expression, start + 5, end1, table);
assert(expression[end1] == ' ');
int end2;
int e2 = evaluate(expression, end1 + 1, end2, table);
assert(expression[end2] == ')');
end = end2 + 1;
return e1 + e2;
}
// mult
if(expression[start + 1] == 'm'){
assert(expression[start + 2] == 'u');
assert(expression[start + 3] == 'l');
assert(expression[start + 4] == 't');
assert(expression[start + 5] == ' ');
int end1;
int e1 = evaluate(expression, start + 6, end1, table);
assert(expression[end1] == ' ');
int end2;
int e2 = evaluate(expression, end1 + 1, end2, table);
assert(expression[end2] == ')');
end = end2 + 1;
return e1 * e2;
}
// let
assert(expression[start + 1] == 'l');
assert(expression[start + 2] == 'e');
assert(expression[start + 3] == 't');
assert(expression[start + 4] == ' ');
unordered_map<string, int> newTable(table.begin(), table.end());
int newStart = start + 5;
while(true){
if(expression[newStart] == '(' || expression[newStart] == '-' ||
isdigit(expression[newStart])){
int inner_end;
int res = evaluate(expression, newStart, inner_end, newTable);
end = inner_end + 1;
return res;
}
int end1 = nextNonLetter(expression, newStart);
string var = expression.substr(newStart, end1 - newStart);
if(expression[end1] == ')'){
int inner_end;
int res = evaluate(expression, newStart, inner_end, newTable);
assert(end1 == inner_end);
end = inner_end + 1;
return res;
}
assert(expression[end1] == ' ');
int end2;
int inner_res = evaluate(expression, end1 + 1, end2, newTable);
assert(expression[end2] == ' ');
newTable[var] = inner_res;
newStart = end2 + 1;
}
assert(false);
}
int nextNonDigit(const string& expression, int start){
for(int i = start; ; i ++)
if(!isdigit(expression[i]))
return i;
assert(false);
return expression.size();
}
int nextNonLetter(const string& expression, int start){
for(int i = start; ; i ++)
if(!isdigit(expression[i]) && !isalpha(expression[i]))
return i;
assert(false);
return expression.size();
}
};
int main() {
cout << Solution().evaluate("(add 1 2)") << endl;
// 3
cout << Solution().evaluate("(mult 3 (add 2 3))") << endl;
// 15
cout << Solution().evaluate("(let x 2 (mult x 5))") << endl;
// 10
cout << Solution().evaluate("(let x 2 (mult x (let x 3 y 4 (add x y))))") << endl;
// 14
cout << Solution().evaluate("(let x 3 x 2 x)") << endl;
// 2
cout << Solution().evaluate("(let x 1 y 2 x (add x y) (add x y))") << endl;
// 5
cout << Solution().evaluate("(let x 2 (add (let x 3 (let x 4 x)) x))") << endl;
// 6
cout << Solution().evaluate("(let a1 3 b2 (add a1 1) b2)") << endl;
// 4
cout << Solution().evaluate("(let x 7 -12)") << endl;
// -12
return 0;
}