-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuickscope.cpp
More file actions
42 lines (34 loc) · 1.46 KB
/
Quickscope.cpp
File metadata and controls
42 lines (34 loc) · 1.46 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
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
int N; cin >> N;
vector<unordered_set<string>> variables; // DS segmented by indices of scope to isolate multiple declarations.
unordered_map<string, vector<string>> types; // DS to keep track of last declared variables irregardless of scope. The use of a vector is faster than using a Stack.
unordered_set<string> st;
variables.push_back(st);
while (N--) {
string token;
cin >> token;
if (token == "{") {
variables.push_back(st);
} else if (token == "}") {
for (auto& i : variables.back()) {
if (!types[i].empty()) types[i].pop_back(); // Avoid segmentation fault
}
variables.pop_back();
} else if (token == "DECLARE") {
string variable, type; cin >> variable >> type;
if (variables.back().find(variable) != variables.back().end()) {
cout << "MULTIPLE DECLARATION" << '\n';
return 0;
}
variables.back().insert(variable); types[variable].push_back(type);
} else {
string variable;
cin >> variable;
(!types[variable].empty()) ? cout << types[variable].back() << '\n' : cout << "UNDECLARED" << '\n';
}
}
return 0;
}