Submission #705189


Source Code Expand

#include <bits/stdc++.h>

using namespace std;

#define REP(i,a,b) for(int i=a;i<(int)b;i++)
#define rep(i,n) REP(i,0,n)

int const INF = 1<<29;

struct tree; typedef tree* tree_ptr;

struct tree {
  tree_ptr lch, rch;
  int num;
  tree()
    : lch(NULL), rch(NULL), num(-INF) { }
};

void consume(string& S, char expected) {
  if(S[0] == expected) {
    S = S.substr(1);
    return;
  }
  cout << S << ", " << expected << endl;
  assert(0);
}

bool consume_if(string& S, char expected) {
  if(S[0] == expected) {
    consume(S, expected);
    return true;
  }
  else {
    return false;
  }
}

int read_num(string& S) {
  stringstream ss(S);
  int x; ss >> x;
  ss >> S;
  return x;
}

void make_tree(string& S, tree_ptr curr) {
  if(consume_if(S, '(')) {
    tree_ptr lch = new(tree);
    curr->lch = lch;
    make_tree(S, lch);
    consume(S, ')');
    
    consume(S, '[');
    curr->num = read_num(S);
    consume(S, ']');
    
    consume(S, '(');
    tree_ptr rch = new(tree);
    curr->rch = rch;
    make_tree(S, rch);
    consume(S, ')');
    return;
  }
}

void dfs(tree* ptr1, tree* ptr2) {
  if(ptr1->num == -INF || ptr2->num == -INF) { return; }
  
  if(ptr1->lch && ptr2->lch) {
    cout << "(";
    dfs(ptr1->lch, ptr2->lch);
    cout << ")";
  }
  cout << "[" << (ptr1->num + ptr2->num) << "]";
  if(ptr1->rch && ptr2->rch) {
    cout << "(";
    dfs(ptr1->rch, ptr2->rch);
    cout << ")";
  }
}

int main() {

  string a; cin >> a;
  string b; cin >> b;
  
  tree_ptr atree = new(tree);
  tree_ptr btree = new(tree);
  
  make_tree(a, atree);
  make_tree(b, btree);

  dfs(atree, btree);
  cout << endl;
  
  return 0;
}

Submission Info

Submission Time
Task C - みさわさんの根付き木
User aizu_e
Language C++14 (GCC 5.4.1)
Score 100
Code Size 1736 Byte
Status AC
Exec Time 8 ms
Memory 384 KiB

Judge Result

Set Name All
Score / Max Score 100 / 100
Status
AC × 53
Set Name Test Cases
All 0_sample_0, 0_sample_1, 0_sample_2, 0_sample_3, 0_sample_4, 10_random_00, 10_random_01, 10_random_02, 10_random_03, 10_random_04, 10_random_05, 10_random_06, 10_random_07, 10_random_08, 10_random_09, 10_random_10, 10_random_11, 10_random_12, 10_random_13, 10_random_14, 10_random_15, 10_random_16, 10_random_17, 10_random_18, 10_random_19, 10_random_20, 10_random_21, 10_random_22, 10_random_23, 10_random_24, 10_random_25, 10_random_26, 10_random_27, 10_random_28, 10_random_29, 10_random_30, 10_random_31, 10_random_32, 10_random_33, 10_random_34, 10_random_35, 10_random_36, 10_random_37, 10_random_38, 10_random_39, 20_unbalanced_0, 20_unbalanced_1, 20_unbalanced_2, 20_unbalanced_3, 21_small_unbalanced_0, 21_small_unbalanced_1, 21_small_unbalanced_2, 21_small_unbalanced_3
Case Name Status Exec Time Memory
0_sample_0 AC 4 ms 256 KiB
0_sample_1 AC 5 ms 256 KiB
0_sample_2 AC 4 ms 256 KiB
0_sample_3 AC 4 ms 256 KiB
0_sample_4 AC 4 ms 256 KiB
10_random_00 AC 5 ms 256 KiB
10_random_01 AC 5 ms 256 KiB
10_random_02 AC 5 ms 256 KiB
10_random_03 AC 5 ms 256 KiB
10_random_04 AC 5 ms 256 KiB
10_random_05 AC 6 ms 256 KiB
10_random_06 AC 5 ms 256 KiB
10_random_07 AC 5 ms 256 KiB
10_random_08 AC 5 ms 256 KiB
10_random_09 AC 5 ms 256 KiB
10_random_10 AC 8 ms 384 KiB
10_random_11 AC 6 ms 256 KiB
10_random_12 AC 5 ms 256 KiB
10_random_13 AC 5 ms 256 KiB
10_random_14 AC 5 ms 256 KiB
10_random_15 AC 5 ms 256 KiB
10_random_16 AC 5 ms 256 KiB
10_random_17 AC 5 ms 256 KiB
10_random_18 AC 5 ms 256 KiB
10_random_19 AC 5 ms 256 KiB
10_random_20 AC 5 ms 256 KiB
10_random_21 AC 5 ms 256 KiB
10_random_22 AC 5 ms 256 KiB
10_random_23 AC 5 ms 256 KiB
10_random_24 AC 5 ms 256 KiB
10_random_25 AC 6 ms 256 KiB
10_random_26 AC 5 ms 256 KiB
10_random_27 AC 5 ms 256 KiB
10_random_28 AC 5 ms 256 KiB
10_random_29 AC 5 ms 256 KiB
10_random_30 AC 5 ms 256 KiB
10_random_31 AC 5 ms 256 KiB
10_random_32 AC 5 ms 256 KiB
10_random_33 AC 5 ms 256 KiB
10_random_34 AC 5 ms 256 KiB
10_random_35 AC 5 ms 256 KiB
10_random_36 AC 5 ms 256 KiB
10_random_37 AC 5 ms 256 KiB
10_random_38 AC 5 ms 256 KiB
10_random_39 AC 5 ms 256 KiB
20_unbalanced_0 AC 5 ms 256 KiB
20_unbalanced_1 AC 5 ms 256 KiB
20_unbalanced_2 AC 5 ms 256 KiB
20_unbalanced_3 AC 5 ms 256 KiB
21_small_unbalanced_0 AC 6 ms 256 KiB
21_small_unbalanced_1 AC 5 ms 256 KiB
21_small_unbalanced_2 AC 5 ms 256 KiB
21_small_unbalanced_3 AC 5 ms 256 KiB