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

#include <bits/stdc++.h>

#define _overload(_1, _2, _3, name, ...) name
#define _rep(i, n) _range(i, 0, n)
#define _range(i, a, b) for (int i = int(a); i < int(b); ++i)
#define rep(...) _overload(__VA_ARGS__, _range, _rep, )(__VA_ARGS__)

#define _rrep(i, n) _rrange(i, n, 0)
#define _rrange(i, a, b) for (int i = int(a) - 1; i >= int(b); --i)
#define rrep(...) _overload(__VA_ARGS__, _rrange, _rrep, )(__VA_ARGS__)

#define _all(arg) begin(arg), end(arg)
#define uniq(arg) sort(_all(arg)), (arg).erase(unique(_all(arg)), end(arg))
#define getidx(ary, key) lower_bound(_all(ary), key) - begin(ary)
#define clr(a, b) memset((a), (b), sizeof(a))
#define bit(n) (1LL << (n))
#define popcount(n) (__builtin_popcountll(n))

template <class T> bool chmax(T &a, const T &b) {
  return (a < b) ? (a = b, 1) : 0;
}
template <class T> bool chmin(T &a, const T &b) {
  return (b < a) ? (a = b, 1) : 0;
}

using namespace std;

const int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
const int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};

string yaml[50010];

int main(void) {
  string s;
  cin >> s;
  vector<string> keys;

  while (s != "") {
    int cur = 1;
    while (cur < s.size() && s[cur] != '.')
      cur++;
    keys.push_back(s.substr(1, cur - 1));
    s = s.substr(cur);
  }

  cin.ignore();

  int n = 0;
  while (1) {
    getline(cin, yaml[n++]);
    if (cin.eof())
      break;
  }

  // cout << n << endl;
  bool not_found = false;

  int cur = 0, idx = 0, pos = 0;
  while (1) {
    const int len = keys[idx].size();
    while (cur < n) {
      if (pos + len <= yaml[cur].size() &&
          keys[idx] == yaml[cur].substr(pos, len))
        break;
      cur++;
    }

    // cout << cur << " " << idx << " " << pos << endl;

    if (cur == n) {
      not_found = true;
      break;
    }

    if (idx + 1 == keys.size())
      break;
    cur++, idx++;

    if (cur == n || yaml[cur][pos] != ' ') {
      not_found = true;
      break;
    }

    while (pos < yaml[cur].size() && yaml[cur][pos] == ' ')
      pos++;
  }

  if (not_found) {
    cout << "no such property" << endl;
    return 0;
  }

  string elem = yaml[cur].substr(pos + 1 + keys[keys.size() - 1].size());
  if (elem == "")
    cout << "object" << endl;
  else {
    elem = elem.substr(1, elem.size() - 1);
    cout << "string \\"" << elem << "\\"" << endl;
  }
  return 0;
}

Error: -6
terminate called after throwing an instance of 'std::out_of_range'
  what():  basic_string::substr: __pos (which is 2) > this->size() (which is 0)


Fixed submission:

#include <bits/stdc++.h>

#define _overload(_1, _2, _3, name, ...) name
#define _rep(i, n) _range(i, 0, n)
#define _range(i, a, b) for (int i = int(a); i < int(b); ++i)
#define rep(...) _overload(__VA_ARGS__, _range, _rep, )(__VA_ARGS__)

#define _rrep(i, n) _rrange(i, n, 0)
#define _rrange(i, a, b) for (int i = int(a) - 1; i >= int(b); --i)
#define rrep(...) _overload(__VA_ARGS__, _rrange, _rrep, )(__VA_ARGS__)

#define _all(arg) begin(arg), end(arg)
#define uniq(arg) sort(_all(arg)), (arg).erase(unique(_all(arg)), end(arg))
#define getidx(ary, key) lower_bound(_all(ary), key) - begin(ary)
#define clr(a, b) memset((a), (b), sizeof(a))
#define bit(n) (1LL << (n))
#define popcount(n) (__builtin_popcountll(n))

template <class T> bool chmax(T &a, const T &b) {
  return (a < b) ? (a = b, 1) : 0;
}
template <class T> bool chmin(T &a, const T &b) {
  return (b < a) ? (a = b, 1) : 0;
}

using namespace std;

const int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
const int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};

string yaml[50010];

int main(void) {
  string s;
  cin >> s;
  vector<string> keys;

  while (s != "") {
    int cur = 1;
    while (cur < s.size() && s[cur] != '.')
      cur++;
    keys.push_back(s.substr(1, cur - 1));
    s = s.substr(cur);
  }

  cin.ignore();

  int n = 0;
  while (1) {
    getline(cin, yaml[n++]);
    if (cin.eof())
      break;
  }

  bool not_found = false;

  int cur = 0, pos = 0;
  while (1) {
    const int len = keys[pos].size();
    while (cur < n) {
      if (keys[pos] == yaml[cur].substr(pos, len))
        break;
      cur++;
    }

    // cout << cur << " " << pos << endl;

    if (cur == n) {
      not_found = true;
      break;
    }

    if (pos / 2 + 1 == keys.size())
      break;
    cur++, pos += 2;
  }

  if (not_found) {
    cout << "no such property" << endl;
    return 0;
  }

  string elem = yaml[cur].substr(pos + 1 + keys[keys.size() - 1].size());
  if (elem == "")
    cout << "object" << endl;
  else {
    elem = elem.substr(1, elem.size() - 1);
    cout << "string \\"" << elem << "\\"" << endl;
  }
  return 0;
}