After extensive practice, Gilbert has finally become a Master Gardener.
The first line contains two positive integers, H and T, representing the height of the expression tree and the type of query, respectively.
The next line is the expression tree (in prefix expression). The expression contains only +, -, * and digits. All the numbers in the expression are integers between 0 and 9.
It is guaranteed that the length of the expression is 2H+1 - 1.
Output the maximum beauty of the expression tree after performing at most one swap.
Note that you should have a '\n' in the end.
You may consider to use the following reference code.
#include <stdio.h> #include <stdlib.h> #include <stdbool.h> #define mod 998244353 // Define a modulo for the beauty calculation. typedef struct _Node { int val; // {0 ~ 9: number, -1: +, -2: -, -3: *} int ans; // evaluate result struct _Node *lc, *rc, *p; //Left child, Right child, Parent } Node; char expr[2200000] = {}; // Input string representing the preorder expression of the tree. Node *tree[2200000] = {}; // Array to store pointers to nodes for easy access. int h, t, n; int idx = 0; // Function to build the expression tree from the input string. Node *build() { if(idx >= n) return NULL; Node *r = (Node *)malloc(sizeof(Node)); tree[idx] = r; r->ans = -1, r->lc = r->rc = r->p = NULL; if(expr[idx] >= '0' && expr[idx] <= '9'){ r->val = expr[idx] - '0'; idx++; } else { if(expr[idx] == '+') r->val = -1; else if(expr[idx] == '-') r->val = -2; else r->val = -3; idx++; r->lc = build(); r->rc = build(); r->lc->p = r; r->rc->p = r; } return r; } // Function to evaluate the expression tree and calculate its beauty. int eval(Node *t) { } // Function to swap the subtrees rooted at nodes a and b. void swap(Node *a, Node *b) { } // Depth-First Search function to check if target node is in the subtree rooted at 'now'. bool dfs(Node *now, Node *target) { } // Function to check if two nodes have an
ancestor-descendantrelationship. bool check(Node *a, Node *b) { } int main() { scanf("%d %d", &h, &t); scanf("%s", expr); n = (1 << (h + 1)) - 1; // n = 2 ^ (h + 1) - 1 build(); int ans = eval(tree[0]); if(t == 1) { } else { } printf("%d\n", ans); }