-
Notifications
You must be signed in to change notification settings - Fork 2
/
A1119.cpp
54 lines (51 loc) · 1.09 KB
/
A1119.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
#include <cstdio>
#include <vector>
using namespace std;
vector<int> ans;
int *pre, *post, unique = 1;
int findFromPre (int x, int l, int r) {
for (int i = l; i <= r; i++) {
if (x == pre[i]) {
return i;
}
}
return -1;
}
void setIn (int prel, int prer, int postl, int postr) {
if (prel == prer) {
ans.push_back(pre[prel]);
return;
}
if (pre[prel] == post[postr]) {
int x = findFromPre(post[postr - 1], prel + 1, prer);
if (x - prel > 1) {
setIn(prel + 1, x - 1, postl, postl + x - prel - 2);
ans.push_back(post[postr]);
setIn(x, prer, postl + x - prel - 2 + 1, postr - 1);
} else {
unique = 0;
ans.push_back(post[postr]);
setIn(x, prer, postl + x - prel - 2 + 1, postr - 1);
}
}
}
int main() {
int n = 0;
scanf("%d", &n);
pre = new int [n];
post = new int [n];
for (int i = 0; i < n; i++) {
scanf("%d", &pre[i]);
}
for (int i = 0; i < n; i++) {
scanf("%d", &post[i]);
}
setIn(0, n - 1, 0, n - 1);
printf("%s\n", unique ? "Yes" : "No");
printf("%d", ans[0]);
for (int i = 1; i < ans.size(); i++) {
printf(" %d", ans[i]);
}
printf("\n");
return 0;
}