#include <iostream>
#include <string>
#include <queue>
using namespace std;
struct Node {
char data;
Node* left;
Node* right;
};
// Function to create a new node
Node* NewNode(char data) {
Node* node = new Node;
node->data = data;
node->left = nullptr;
node->right = nullptr;
return node;
}
// Function to find the index in the in-order sequence
int Search(const string& in, int start, int end, char value) {
for (int i = start; i <= end; i++) {
if (in[i] == value) {
return i;
}
}
return -1; // Return -1 if not found
}
// Function to build a binary tree from in-order and post-order sequences
Node* BuildTree(const string& in, const string& post, int inStart, int inEnd, int& postIndex) {
if (inStart > inEnd) {
return nullptr;
}
// Create a new node, and use postIndex as the root position
Node* node = NewNode(post[postIndex--]);
if (inStart == inEnd) {
return node;
}
// Find the index of the node in the 'in' sequence
int inIndex = Search(in, inStart, inEnd, node->data);
// Recursively build left and right subtrees
node->right = BuildTree(in, post, inIndex + 1, inEnd, postIndex);
node->left = BuildTree(in, post, inStart, inIndex - 1, postIndex);
return node;
}
// Function to print the level order traversal
void PrintLevelOrder(Node* root) {
if (root == nullptr) return;
queue<Node*> q;
q.push(root);
while (!q.empty()) {
int nodeCount = q.size();
while (nodeCount > 0) {
Node* node = q.front();
cout << node->data;
q.pop();
if (node->left != nullptr)
q.push(node->left);
if (node->right != nullptr)
q.push(node->right);
nodeCount--;
}
}
cout << endl;
}
int main() {
string in;
string post;
cin >> in >> post;
int len = in.size();
int postIndex = len - 1;
Node* root = BuildTree(in, post, 0, len - 1, postIndex);
PrintLevelOrder(root);
return 0;
}