-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathkattis_learningtocode.cpp
More file actions
94 lines (87 loc) · 2.61 KB
/
Copy pathkattis_learningtocode.cpp
File metadata and controls
94 lines (87 loc) · 2.61 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
90
91
92
93
94
/**Kattis - learningtocode
* Writing a recursive parser. Not too hard except for the traps.
* 1. $ can be part of the string without {
* 2. The variables can be super long, but if such variables exist, they will not be used for
* printing so we can just stop computing their values
*
* Other than that, we should replace spaces so we can use istringstream while considering the
* spaces. Then we can put them back when we are printing the result
*
* Time: O(n), Space: O(?)
*/
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
unordered_map<string, string> envt;
const int MAXLENGTH = 10001;
pair<string, bool> expr(istringstream &iss)
{ // The prefix of iss should be an expression, returns whether the expr is a variable
char c;
iss >> c;
if (c == '`' || c == '\"') {
string s = "";
while (iss >> c && c != '`' && c != '\"' && s.length() < MAXLENGTH) {
if (c == '$' && iss.peek() == '{') {
iss >> c; // {
auto [res, is_var] = expr(iss);
if (!is_var) {
iss >> c; // }
}
s += res;
}
else {
s += c;
}
}
return make_pair(s, false);
}
else { // must be a variable
string var = "";
var += c;
while (iss >> c && (isalpha(c) || c == '_')) {
var += c;
}
if (envt.find(var) == envt.end()) {
throw "Variable " + var + " not found";
}
return make_pair(envt[var], true);
}
}
int main()
{
string line;
while (true) {
getline(cin, line);
if (line == "end.") break;
bool isdecl = (line[0] == 'v');
if (isdecl) {
line = line.substr(4, line.length() - 4);
}
else {
line = line.substr(6, line.length() - 6);
}
line = line.substr(0, line.length() - 1); // remove the ;
line = regex_replace(line, regex(" "), "~");
istringstream iss(line);
string temp;
if (isdecl) {
string var, val;
char c;
while (iss >> c && c != '~') {
var += c;
}
iss >> c; // =
iss >> c; // ~
val = expr(iss).first;
envt[var] = val;
}
else {
string res = expr(iss).first;
res = regex_replace(res, regex("~"), " ");
cout << res << endl;
}
}
return 0;
}