Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <string>
- using namespace std;
- struct node {
- char val;
- node *left, *right;
- node(char x): val(x) {}
- };
- pair<string, string> parse(string s) {
- int n = (int) s.length();
- int cnt = 0, ind = -1;
- for (int i = 1; i < n - 1; i++) {
- if (s[i] == '(') {
- cnt++;
- } else if (s[i] == ')') {
- cnt--;
- } else if (s[i] == ',' && cnt == 0) {
- ind = i;
- break;
- }
- }
- string a = s.substr(1, ind - 1);
- string b = s.substr(ind + 1, n - ind - 2);
- return make_pair(a, b);
- }
- node *build(string s) {
- node *root;
- if (s.length() == 1) {
- root = new node(s[0]);
- return root;
- } else {
- root = new node(0);
- }
- pair<string, string> res = parse(s);
- root->left = build(res.first);
- root->right = build(res.second);
- return root;
- }
- int main() {
- int k;
- cin >> k;
- while (k--) {
- string s;
- cin >> s;
- node *root = build(s);
- node *ptr = root;
- string t;
- cin >> t;
- for (int i = 0; i < t.length(); i++) {
- if (t[i] == '0') {
- ptr = ptr->left;
- } else {
- ptr = ptr->right;
- }
- if (ptr->val != 0) {
- cout << ptr->val;
- ptr = root;
- }
- }
- cout << endl;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement