-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtwochartsbecomeone.cpp
72 lines (65 loc) · 1.21 KB
/
twochartsbecomeone.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
#include <bits/stdc++.h>
using namespace std;
const int MAXN(1'000'000 + 10);
vector<vector<int>> ga(MAXN+1);
vector<vector<int>> gb(MAXN+1);
int buildg(const string &s, vector<vector<int>> &g) {
assert(s[0] != '(');
assert(s.back() != '(');
vector<string> stk;
int pai = -1;
for (auto c : s) {
if (c == ' ' or c == '\n') continue;
if (isdigit(c)) {
if (stk.empty()) {
stk.push_back(string(1, c));
}
else {
if (isdigit(stk.back().back())) {
stk.back().push_back(c);
}
else {
stk.push_back(string(1, c));
}
}
}
else {
if (c == '(') {
pai = stoi(stk.back());
stk.push_back(string(1, c));
}
else {
int filho = stoi(stk.back());
stk.pop_back();
stk.pop_back();
pai = stoi(stk.back());
g[pai].emplace_back(filho);
}
}
}
if (pai == -1) return stoi(s);
return pai;
}
void run() {
string a, b;
getline(cin, a);
getline(cin, b);
int pa =buildg(a, ga);
int pb =buildg(b, gb);
for (int i = 0; i <= MAXN; i++) {
sort(ga[i].begin(), ga[i].end());
sort(gb[i].begin(), gb[i].end());
if (ga[i] != gb[i]) {
cout << "No\n";
return;
}
}
if (pb != pa) {
cout << "No\n";
return;
}
cout << "Yes\n";
}
int main() {
run();
}