AcWing 3598. 二叉树遍历
原题链接
简单
#include <bits/stdc++.h>
using namespace std;
string qian, zho;
int find(int x) {
return zho.find(qian[x]);
}
void build(int ql, int qr, int zl, int zr) {
int k = find(ql);
int lcnt = k - zl;
int rcnt = zr - k;
if (lcnt > 0) build(ql + 1, ql + lcnt, zl, k - 1);
if (rcnt > 0) build(qr + 1 - rcnt, qr, k + 1, zr);
cout << qian[ql];
}
void solve() {
while (cin >> qian >> zho) {
build(0, qian.size() - 1, 0, zho.size() - 1);
cout<<"\n";
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
solve();
return 0;
}