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

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

ll id;
int N, c;
string s;

const ll MAX = 2000000;

struct Node {
  ll v, len;
  string s;
  vector<Node *> children;
  Node() : v(1), len(0), s(string()) {}
};

int readNum() {
  int n = 0;
  while (c < N && isdigit(s[c])) {
    n = n * 10 + (s[c] - '0');
    c++;
  }
  return n;
}

string readS() {
  string ret;
  assert(s[c] != '(' && s[c] != ')');
  while (c < N && 'A' <= s[c] && s[c] <= 'Z') {
    ret += s[c];
    c++;
  }
  return ret;
}

char ans;

Node *f() {
  Node *node = new Node();
  if (isdigit(s[c])) {
    int n = readNum();
    node->v = n;

    if (s[c] != '(') {
      Node *res = f();
      node->children.push_back(res);
      node->len = min(MAX, node->len + res->len * res->v);
      return node;
    }

    while (c < N) {
      if (s[c] == '(') {
        c++;
      }

      Node *res = f();
      node->children.push_back(res);
      node->len = min(MAX, node->len + res->len * res->v);

      if (c < N && s[c] == ')') {
        c++;
        return node;
      }
    }
    return node;
  }

  // assert('A' <= s[c] && s[c] <= 'Z');

  if (!('A' <= s[c] && s[c] <= 'Z')) {
    int x = 0;
  }

  node->s = readS();
  node->len = min(MAX, (ll)node->s.size());

  // if(ans == 0) {
  //	if(ss.size() * v <= id) {
  //		id -= ss.size() * v;
  //		return;
  //	}

  //	while(id >= ss.size()) id -= ss.size();
  //	ans = ss[id];
  //}
  return node;
}

void g(Node *node) {
  if (node->children.size() == 0) {
    assert(id < node->s.size());
    ans = node->s[id];
    return;
  }

  while (!(id < node->len))
    id -= node->len;

  for (auto child : node->children) {
    if (child->len * child->v <= id) {
      id -= child->len * child->v;
    } else {
      g(child);
      assert(ans);
      return;
    }
  }
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  while (cin >> s >> id, s != "0") {
    N = s.size();
    c = 0;
    ans = 0;
    while (c < N && ans == 0) {
      Node *res = f();
      if (res->len * res->v <= id) {
        id -= res->len * res->v;
      } else {
        g(res);
        break;
      }
    }

    if (ans)
      cout << ans << endl;
    else
      cout << 0 << endl;
  }
}

Error: 0

Fixed submission:

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

ll id;
int N, c;
string s;

const ll MAX = 2000000;

struct Node {
  ll v, len;
  string s;
  vector<Node *> children;
  Node() : v(1), len(0), s(string()) {}
};

int readNum() {
  int n = 0;
  while (c < N && isdigit(s[c])) {
    n = n * 10 + (s[c] - '0');
    c++;
  }
  return n;
}

string readS() {
  string ret;
  assert(s[c] != '(' && s[c] != ')');
  while (c < N && 'A' <= s[c] && s[c] <= 'Z') {
    ret += s[c];
    c++;
  }
  return ret;
}

char ans;

Node *f() {
  Node *node = new Node();
  if (isdigit(s[c])) {
    int n = readNum();
    node->v = n;

    while (c < N) {
      if (s[c] == '(') {
        c++;
      }

      Node *res = f();
      node->children.push_back(res);
      node->len = min(MAX, node->len + res->len * res->v);

      if (s[c] == ')') {
        c++;
        return node;
      }
      if (res->s.size() == 1) {
        return node;
      }
    }
    return node;
  }

  assert('A' <= s[c] && s[c] <= 'Z');

  node->s = readS();
  node->len = min(MAX, (ll)node->s.size());

  // if(ans == 0) {
  //	if(ss.size() * v <= id) {
  //		id -= ss.size() * v;
  //		return;
  //	}

  //	while(id >= ss.size()) id -= ss.size();
  //	ans = ss[id];
  //}
  return node;
}

void g(Node *node) {
  if (node->children.size() == 0) {
    assert(id < node->s.size());
    ans = node->s[id];
    return;
  }

  while (!(id < node->len))
    id -= node->len;

  for (auto child : node->children) {
    if (child->len * child->v <= id) {
      id -= child->len * child->v;
    } else {
      g(child);
      assert(ans);
      return;
    }
  }
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  while (cin >> s >> id, s != "0") {
    N = s.size();
    c = 0;
    ans = 0;
    while (c < N && ans == 0) {
      Node *res = f();
      if (res->len * res->v <= id) {
        id -= res->len * res->v;
      } else {
        g(res);
        break;
      }
    }

    if (ans)
      cout << ans << endl;
    else
      cout << 0 << endl;
  }
}