forked from ngthanhtrung23/CompetitiveProgramming
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathF.cpp
More file actions
126 lines (111 loc) · 3.3 KB
/
F.cpp
File metadata and controls
126 lines (111 loc) · 3.3 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
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
#include <bits/stdc++.h>
#define FOR(i,a,b) for(int i=(a),_b=(b); i<=_b; i++)
#define FORD(i,a,b) for(int i=(a),_b=(b); i>=_b; i--)
#define REP(i,a) for(int i=0,_a=(a); i<_a; i++)
#define EACH(it,a) for(__typeof(a.begin()) it = a.begin(); it != a.end(); ++it)
#define DEBUG(x) { cout << #x << " = "; cout << (x) << endl; }
#define PR(a,n) { cout << #a << " = "; FOR(_,1,n) cout << a[_] << ' '; cout << endl; }
#define PR0(a,n) { cout << #a << " = "; REP(_,n) cout << a[_] << ' '; cout << endl; }
#define sqr(x) ((x) * (x))
#define ll long long
#define SZ(X) ((int) ((X).size()))
using namespace std;
string prog;
vector<ll> func[2011];
vector<string> func_names[2011];
void read() {
string t;
while (cin >> t) {
prog += ' ';
for(char c : t) {
if (c == '{' || c == '}' || c == ';') {
prog += ' ';
prog += c;
prog += ' ';
}
else prog += c;
}
}
// DEBUG(prog);
}
int nFunc;
vector<string> commands[2011];
map< vector<ll>, int > funcId;
#define hash hash__
ll hash(const string& s) {
ll res = 0;
for(char c : s)
res = res * 301 + c;
return res;
}
int main() {
ios :: sync_with_stdio(false);
freopen("polymorphism.in", "r", stdin);
freopen("polymorphism.out","w", stdout);
read();
string t;
stringstream ss(prog);
vector<ll> cur;
vector<string> cur_name;
int cur_func = 0;
while (ss >> t) {
if (t == "namespace") {
ss >> t; // name
cur.push_back(hash(t));
cur_name.push_back(t);
ss >> t; // {
}
else if (t == "function") {
ss >> t; // name
cur.push_back(hash(t));
cur_name.push_back(t);
++nFunc;
func[nFunc] = cur;
func_names[nFunc] = cur_name;
cur_func = nFunc;
funcId[cur] = nFunc;
ss >> t; // {
}
else if (t == "}") {
cur.pop_back();
cur_name.pop_back();
cur_func = 0;
}
else {
commands[cur_func].push_back(t);
ss >> t; //;
}
}
FOR(i,1,nFunc) {
cout << '[' << i << "] ";
REP(j,SZ(func_names[i])) {
cout << func_names[i][j];
if (j < SZ(func_names[i]) - 1) cout << '.';
else cout << '\n';
}
for(auto s : commands[i]) {
string comm = s;
REP(i,SZ(s))
if (comm[i] == '.') comm[i] = ' ';
stringstream ss(comm);
string t;
vector<ll> cur = func[i]; cur.pop_back();
vector<ll> add;
while (ss >> t) {
add.push_back(hash(t));
}
bool ok = false;
while (true) {
vector<ll> t = cur; t.insert(t.end(), add.begin(), add.end());
if (funcId.count(t)) {
cout << " (" << funcId[t] << ")\n";
ok = true;
break;
}
if (cur.empty()) break;
cur.pop_back();
}
if (!ok) cout << " (ERROR)\n";
}
}
}