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

#include <algorithm>
#include <cassert>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>

#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define all(c) (c).begin(), (c).end()
#define mp make_pair
#define pb push_back
#define each(i, c)                                                             \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define dbg(x) cerr << __LINE__ << ": " << #x << " = " << (x) << endl

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pi;
const int inf = (int)1e9;
const double INF = 1e12, EPS = 1e-9;

struct obj {
  vector<string> key;
  vector<string> property;
  vector<obj *> obj;
};
int n, p;

string ks, in[50000];
obj *parse(int pid = -1) {
  obj *res = new obj;
  while (p < n) {
    int id = 0;
    for (; isspace(in[p][id]); id++)
      ;
    if (id <= pid)
      break;

    int pos = in[p].find(":");
    string key = in[p].substr(id, pos - id);
    if (pos == in[p].size() - 1) {
      res->key.pb(key);
      res->property.pb("");
      p++;
      res->obj.pb(parse(id));
    } else {
      res->key.pb(key);
      res->property.pb(in[p].substr(pos + 2));
      res->obj.pb(NULL);
      p++;
    }
  }
  return res;
}

int main() {
  getline(cin, ks);
  while (getline(cin, in[n]))
    n++;

  obj *root = parse();

  each(i, ks) if (*i == '.') *i = ' ';
  stringstream ss(ks);
  vector<string> v;
  while (ss >> ks)
    v.pb(ks);

  rep(i, v.size() - 1) {
    bool ok = 0;
    rep(j, root->key.size()) if (root->key[j] == v[i]) {
      if (root->property[j] != "")
        break;

      root = root->obj[j];
      ok = 1;
    }
    if (!ok) {
      cout << "no such property" << endl;
      return 0;
    }
  }
  rep(j, root->key.size()) if (root->key[j] == v.back()) {
    if (root->property[j] != "")
      cout << "string \\"" << root->property[j] << "\\"" << endl;
    else
      cout << "object" << endl;
    return 0;
  }
  cout << "no such property" << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cassert>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>

#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define all(c) (c).begin(), (c).end()
#define mp make_pair
#define pb push_back
#define each(i, c)                                                             \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define dbg(x) cerr << __LINE__ << ": " << #x << " = " << (x) << endl

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pi;
const int inf = (int)1e9;
const double INF = 1e12, EPS = 1e-9;

struct obj {
  vector<string> key;
  vector<string> property;
  vector<obj> obj;
};
int n, p;
obj null;

string ks, in[50000];
obj parse(int pid = -1) {
  obj res;
  while (p < n) {
    int id = 0;
    for (; isspace(in[p][id]); id++)
      ;
    if (id <= pid)
      break;

    int pos = in[p].find(":");
    string key = in[p].substr(id, pos - id);
    if (pos == in[p].size() - 1) {
      res.key.pb(key);
      res.property.pb("");
      p++;
      res.obj.pb(parse(id));
    } else {
      res.key.pb(key);
      res.property.pb(in[p].substr(pos + 2));
      res.obj.pb(null);
      p++;
    }
  }
  return res;
}

int main() {
  getline(cin, ks);
  while (getline(cin, in[n]))
    n++;

  obj root = parse();

  each(i, ks) if (*i == '.') *i = ' ';
  stringstream ss(ks);
  vector<string> v;
  while (ss >> ks)
    v.pb(ks);

  rep(i, v.size() - 1) {
    bool ok = 0;
    rep(j, root.key.size()) if (root.key[j] == v[i]) {
      if (root.property[j] != "")
        break;

      root = root.obj[j];
      ok = 1;
    }
    if (!ok) {
      cout << "no such property" << endl;
      return 0;
    }
  }
  rep(j, root.key.size()) if (root.key[j] == v.back()) {
    if (root.property[j] != "")
      cout << "string \\"" << root.property[j] << "\\"" << endl;
    else
      cout << "object" << endl;
    return 0;
  }
  cout << "no such property" << endl;

  return 0;
}