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

#include <bits/stdc++.h>
using namespace std;

#define State string::const_iterator
class ParseError {};

int number(State &begin);
int factor(State &begin);
void consume(State &begin, char expected);

// 数字の列をパースして、その数を返す。
int number(State &begin) {
  int ret = 0;

  while (isdigit(*begin)) {
    ret *= 10;
    ret += *begin - '0';
    begin++;
  }

  return ret;
}

// 括弧か数をパースして、その評価結果を返す。
int factor(State &begin) {

  consume(begin, '['); // '('を飛ばす。

  if (*begin != '[') {
    return number(begin) / 2 + 1; //[23]のような形
  }

  //[[23][31][52]]のような形より[]が多い時に通る。
  priority_queue<int> q;
  while (*begin == '[') {
    q.push(-factor(begin)); //[[23][31][52]]のような形になるまで再帰
    consume(begin, ']');
  }
  int ans = 0;
  int siz = q.size();
  for (int i = 0; i < siz / 2 + 1; i++) {
    ans += -q.top();
    q.pop();
  }
  return ans;
}

// beginがexpectedを指していたらbeginを一つ進める。
void consume(State &begin, char expected) {
  if (*begin == expected) {
    begin++;
  } else {
    cerr << "Expected '" << expected << "' but got '" << *begin << "'" << endl;
    cerr << "Rest string is '";
    while (*begin) {
      cerr << *begin++;
    }
    cerr << "'" << endl;
    throw ParseError();
  }
}

int main() {

  int n;

  cin >> n;

  while (n--) {

    string input;

    cin >> input;

    State s = input.begin();

    cout << factor(s) << endl;
  }

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

class Node {
  int value;
  list<Node *> next;
  Node *prev;

public:
  Node();
  Node(Node *p);
  ~Node();
  void set_v(int v);
  void add_next(int v);
  void del_next();
  void set_prev(Node *p);
  Node *next_ret(int index);
  Node *prev_ret();
  int value_ret();
  int next_size_ret();
};

class Tree {
  Node root;
  Node *pos;

public:
  Tree();
  ~Tree();
  void add(int v);
  void move_next(int index);
  void move_back();
  void set_root();
  int pos_size_ret();
  int pos_value_ret();
  Node *pos_ret();
  void DLELTE_DFS(Node *node);

  int solve();
  int DFS(Node *node);
};

Node::Node() {
  this->prev = this;
  value = -1;
}

Node::Node(Node *p) {
  this->prev = p;
  value = -1;
}

Node::~Node() { this->del_next(); }

void Node::set_v(int v) { this->value = v; }

void Node::add_next(int v) {
  Node *n = new Node(this);
  n->value = v;
  next.push_back(n);
}

void Node::del_next() {

  list<Node *>::iterator pre_itr = this->next.begin();
  list<Node *>::iterator itr = pre_itr;
  for (itr++; itr != this->next.end(); itr++) {
    delete *pre_itr;
    pre_itr = itr;
  }

  delete *pre_itr;

  this->next.resize(0);
}

void Node::set_prev(Node *p) { this->prev = p; }

Node *Node::next_ret(int index) {
  list<Node *>::iterator itr = this->next.begin();
  for (int i = 0; i < index; i++) {
    itr++;
  }
  return *itr;
}

Node *Node::prev_ret() { return this->prev; }

int Node::value_ret() { return this->value; }

int Node::next_size_ret() { return this->next.size(); }

Tree::Tree() { this->pos = &(this->root); }

Tree::~Tree() { DLELTE_DFS(&(this->root)); }

void Tree::add(int v) { pos->add_next(v); }

void Tree::move_next(int index) { this->pos = this->pos->next_ret(index); }

void Tree::move_back() { this->pos = this->pos->prev_ret(); }

void Tree::set_root() { this->pos = &(this->root); }

int Tree::pos_size_ret() { return this->pos->next_size_ret(); }

int Tree::pos_value_ret() { return this->pos->value_ret(); }

Node *Tree::pos_ret() { return this->pos; }

int Tree::solve() { return this->pos->value_ret(); }

int Tree::DFS(Node *node) {

  if (node->next_size_ret() == 0) {
    return (node->value_ret() / 2 + 1);
  }

  int add = 0;

  priority_queue<int> q;

  for (int i = 0; i < node->next_size_ret(); i++) {
    q.push(-DFS(node->next_ret(i)));
  }

  int count = q.size();

  for (int loop = 0; loop < count / 2 + 1; loop++) {
    add += -q.top();
    q.pop();
  }

  return add;
}

void Tree::DLELTE_DFS(Node *node) {

  if (node->next_size_ret() == 0) {
    return;
  }

  for (int i = 0; i < node->next_size_ret(); i++) {
    DLELTE_DFS(node->next_ret(i));
  }

  node->del_next();
}

string input;
int n;

int main() {

  cin >> n;
  cin.ignore();

  while (n--) {

    getline(cin, input);

    Tree t;

    for (int i = 1; i < input.size(); i++) {

      if (input[i] == '[') {
        t.add(0);
        t.move_next(t.pos_size_ret() - 1);
      } else if (input[i] == ']') {
        t.move_back();
      } else {
        string snum = "";
        while (input[i] != ']') {
          snum += input[i];
          i++;
        }
        i--;
        stringstream ss;
        ss << snum;
        int num;
        ss >> num;
        t.add(num);
      }
    }

    t.set_root();
    cout << t.DFS(t.pos_ret()) << endl;
  }

  cin >> n;

  return 0;
}