Problem p00587 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
// #include<cassert>
#define REP(i, s, n) for (int i = s; i < n; i++)
#define rep(i, n) REP(i, 0, n)
#define inf (1 << 29)

using namespace std;

struct Tree {
  bool root;
  bool child[2];
  Tree *next[2]; // left -> false, right -> true
  Tree() {
    child[0] = child[1] = false;
    root = false;
  }
};

string s[2];
Tree tree[2];

void ddfs(Tree *t) {
  if (!t->child[0] && !t->child[1])
    return;

  if (t->child[0]) {
    ddfs(t->next[0]);
    t->child[0] = false;
    delete t->next[0];
  }

  if (t->child[1]) {
    ddfs(t->next[1]);
    t->child[1] = false;
    delete t->next[1];
  }
}

void init() {
  tree[0].root = tree[1].root = true;
  rep(i, 2) ddfs(&tree[i]);
}

void parse(int index, int &pos, Tree *t, int depth) {
  if (pos >= s[index].size())
    return;
  // assert(s[index][pos] == '(');
  bool LR = false;

  ++pos;
  while (pos < s[index].size() && !(LR && s[index][pos] == ')')) {

    if (s[index][pos] == '(') {
      t->next[LR] = new Tree;
      t->next[LR]->root = false;
      t->next[LR]->child[0] = t->next[LR]->child[1] = false;
      t->child[LR] = true;
      parse(index, pos, t->next[LR], depth + 1);
      if (pos < s[index].size() && LR && s[index][pos] == ')')
        break;
    }
    if (s[index][pos] == ',')
      LR = true;

    ++pos;
  }
  ++pos;
}

void tree_walk(Tree *t, int depth) {
  cout << '(';
  if (t->child[0])
    tree_walk(t->next[0], depth + 1);
  cout << ',';
  if (t->child[1])
    tree_walk(t->next[1], depth + 1);
  cout << ')';
}

void dfs_i(Tree *t1, Tree *t2, Tree *ans) {
  if (t1->child[0] && t2->child[0]) {
    ans->next[0] = new Tree;
    ans->child[0] = true;
    ans->next[0]->child[0] = ans->next[0]->child[1] = false;
    dfs_i(t1->next[0], t2->next[0], ans->next[0]);
  }

  if (t1->child[1] && t2->child[1]) {
    ans->next[1] = new Tree;
    ans->child[1] = true;
    ans->next[1]->child[0] = ans->next[1]->child[1] = false;
    dfs_i(t1->next[1], t2->next[1], ans->next[1]);
  }
}

void dfs_u(Tree *t, Tree *ans) {
  if (t->child[0]) {
    if (ans->child[0]) { // already exists
      dfs_u(t->next[0], ans->next[0]);
    } else { // not yet
      ans->next[0] = new Tree;
      ans->child[0] = true;
      ans->next[0]->child[0] = ans->next[0]->child[1] = false;
      dfs_u(t->next[0], ans->next[0]);
    }
  }

  if (t->child[1]) {
    if (ans->child[1]) { // already exists
      dfs_u(t->next[1], ans->next[1]);
    } else { // not yet
      ans->next[1] = new Tree;
      ans->child[1] = true;
      ans->next[1]->child[0] = ans->next[1]->child[1] = false;
      dfs_u(t->next[1], ans->next[1]);
    }
  }
}

void compute(char c) {
  Tree ans;
  ans.root = true;
  ans.child[0] = ans.child[1] = false;
  if (c == 'i') {
    dfs_i(&tree[0], &tree[1], &ans);
    tree_walk(&ans, 0);
    cout << endl;
  } else if (c == 'u') {
    dfs_u(&tree[0], &ans);
    dfs_u(&tree[1], &ans);
    tree_walk(&ans, 0);
    cout << endl;
  }

  ddfs(&ans);
}

// deleteまわりも後で書く <- 忘れてた <- でも通らない <- なんでや <-
// parseが小さいケースでしかあってない <- 構文解析間違ってた

int main() {
  /*
  cin >> s[0];
  int pos;
  parse(0,pos=0,&tree[0],0);
  cout << s[0] << endl;
  tree_walk(&tree[0],0);
  cout << endl;
  return 0;
  */
  char c;
  while (cin >> c) {
    cin >> s[0] >> s[1];
    init();

    int pos;
    rep(i, 2) {
      pos = 0;
      parse(i, pos, &tree[i], 0);
      // cout << "output tree[" << i << "]" << endl;
      // cout << s[i] << endl;
      // tree_walk(&tree[i],0);
      // cout << endl;
    }

    compute(c);
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
// #include<cassert>
#define REP(i, s, n) for (int i = s; i < n; i++)
#define rep(i, n) REP(i, 0, n)
#define inf (1 << 29)

using namespace std;

struct Tree {
  bool root;
  bool child[2];
  Tree *next[2]; // left -> false, right -> true
  Tree() {
    child[0] = child[1] = false;
    root = false;
  }
};

string s[2];
Tree tree[2];

void init() {
  tree[0].root = tree[1].root = true;
  rep(i, 2) {
    tree[i].child[0] = tree[i].child[1] = false;
    delete tree[i].next[0];
    delete tree[i].next[1];
  }
}

void parse(int index, int &pos, Tree *t, int depth) {
  if (pos >= s[index].size())
    return;
  // assert(s[index][pos] == '(');
  bool LR = false;

  ++pos;
  while (pos < s[index].size() && !(LR && s[index][pos] == ')')) {

    if (s[index][pos] == '(') {
      t->next[LR] = new Tree;
      t->next[LR]->root = false;
      t->next[LR]->child[0] = t->next[LR]->child[1] = false;
      t->child[LR] = true;
      parse(index, pos, t->next[LR], depth + 1);
    }
    if (s[index][pos] == ',')
      LR = true;

    ++pos;
  }
  ++pos;
}

void tree_walk(Tree *t, int depth) {
  cout << '(';
  if (t->child[0])
    tree_walk(t->next[0], depth + 1);
  cout << ',';
  if (t->child[1])
    tree_walk(t->next[1], depth + 1);
  cout << ')';
}

void dfs_i(Tree *t1, Tree *t2, Tree *ans) {
  if (t1->child[0] && t2->child[0]) {
    ans->next[0] = new Tree;
    ans->child[0] = true;
    ans->next[0]->child[0] = ans->next[0]->child[1] = false;
    dfs_i(t1->next[0], t2->next[0], ans->next[0]);
  }

  if (t1->child[1] && t2->child[1]) {
    ans->next[1] = new Tree;
    ans->child[1] = true;
    ans->next[1]->child[0] = ans->next[1]->child[1] = false;
    dfs_i(t1->next[1], t2->next[1], ans->next[1]);
  }
}

void dfs_u(Tree *t, Tree *ans) {
  if (t->child[0]) {
    if (ans->child[0]) { // already exists
      dfs_u(t->next[0], ans->next[0]);
    } else { // not yet
      ans->next[0] = new Tree;
      ans->child[0] = true;
      ans->next[0]->child[0] = ans->next[0]->child[1] = false;
      dfs_u(t->next[0], ans->next[0]);
    }
  }

  if (t->child[1]) {
    if (ans->child[1]) { // already exists
      dfs_u(t->next[1], ans->next[1]);
    } else { // not yet
      ans->next[1] = new Tree;
      ans->child[1] = true;
      ans->next[1]->child[0] = ans->next[1]->child[1] = false;
      dfs_u(t->next[1], ans->next[1]);
    }
  }
}

void compute(char c) {
  Tree ans;
  ans.root = true;
  ans.child[0] = ans.child[1] = false;
  if (c == 'i') {
    dfs_i(&tree[0], &tree[1], &ans);
    tree_walk(&ans, 0);
    cout << endl;
  } else if (c == 'u') {
    dfs_u(&tree[0], &ans);
    dfs_u(&tree[1], &ans);
    tree_walk(&ans, 0);
    cout << endl;
  }
}

// deleteまわりも後で書く

int main() {

  char c;
  while (cin >> c) {
    cin >> s[0] >> s[1];
    init();

    int pos;
    rep(i, 2) {
      pos = 0;
      parse(i, pos, &tree[i], 0);
      // cerr << "output the tree[" << i << "]" << endl;
      // tree_walk(&tree[i],0);
      // cout << endl;
    }

    compute(c);
  }
  return 0;
}