#include #include #include using namespace std; template struct Node { T element; Node* left; Node* right; Node* parent; Node(const T& e, Node* p) : element(e), parent(p), left(nullptr), right(nullptr) {} }; template class BinTree { public: Node* root; int height; vector arr; BinTree() : root(nullptr), height(0) {} BinTree(const vector& arrs) : arr(arrs), height(0) {} void test() { for (auto x : arr) { cout << x << " "; } } Node* returnroot() { return this->root; } Node* build(int& index, Node* parent) { if (index >= arr.size() || arr[index] == 0) { index++; return nullptr; } Node* node = new Node(arr[index], parent); index++; node->left = build(index, node); node->right = build(index, node); return node; } void buildTree() { int index = 0; this->root = build(index, nullptr); } Node* create(const string& post, const string& in, int ps, int pe, int is, int ie, bool& ok) { if (ps > pe || is > ie) { return nullptr; } T root = post[pe]; int pos = -1; for (int i = is; i <= ie; i++) { if (in[i] == root) { pos = i; break; } } if (pos == -1) { ok = false; return nullptr; } Node* node = new Node(root, nullptr); int len = pos - is; node->left = create(post, in, ps, ps + len - 1, is, pos - 1, ok); node->right = create(post, in, ps + len, pe - 1, pos + 1, ie, ok); return node; } int getHeight(Node* node) { if (node == nullptr) return 0; int lh = getHeight(node->left); int rh = getHeight(node->right); if (lh > rh) return lh + 1; else return rh + 1; } string preorderStr(Node* node) { if (node == nullptr) return ""; string res = ""; res += node->element; res += preorderStr(node->left); res += preorderStr(node->right); return res; } //تن³ِ void preorder(Node* node) { if (node == nullptr) return; cout << node->element << " "; preorder(node->left); preorder(node->right); } void inorder(Node* node) { if (node == nullptr) return; inorder(node->left); cout << node->element << ", parent"; //cout << ( node->parent == nullptr ? 0 : node->parent->element) << endl; inorder(node->right); } void postorder(Node* node) { if (node == nullptr) return; postorder(node->left); postorder(node->right); cout << node->element << " "; } Node* inorder2(Node* node, const T& x) { if (node == nullptr) return nullptr; if (node->element == x) { return node; } Node* leftResult = inorder2(node->left, x); if (leftResult != nullptr) { return leftResult; } Node* rightResult = inorder2(node->right, x); return rightResult; } void findparent(const T& e) { auto x = inorder2(this->root, e); if (x == nullptr || x->parent == nullptr) cout << 0 << endl; else cout << x->parent->element << endl; } }; bool check(const string& post, const string& in) { if (post.length() != in.length()) return false; int cnt[26] = {0}; for (int i = 0; i < post.length(); i++) { cnt[post[i] - 'A']++; } for (int i = 0; i < in.length(); i++) { cnt[in[i] - 'A']--; if (cnt[in[i] - 'A'] < 0) return false; } return true; } int main() { string post, in; while (getline(cin, post) && getline(cin, in)) { if (post.empty() || in.empty()) break; if (!check(post, in)) { cout << "INVALID" << endl; continue; } BinTree tree; bool ok = true; tree.root = tree.create(post, in, 0, post.length() - 1, 0, in.length() - 1, ok); if (!ok) { cout << "INVALID" << endl; } else { tree.height = tree.getHeight(tree.root) - 1; cout << tree.height << endl; string pre = tree.preorderStr(tree.root); cout << pre << endl; } } return 0; }