Problem p01552 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define FOR(i, k, n) for (int i = (k); i < int(n); ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) begin(x), end(x)

using namespace std;

pair<int, size_t> indent(const string &s, size_t i) {
  int cnt = 0;
  while (i < s.size() && s[i] == ' ') {
    ++i;
    ++cnt;
  }
  return make_pair(cnt, i);
}

pair<string, size_t> get_str(const string &s, size_t i) {
  string res;
  while (isalnum(s[i]) || s[i] == ' ') {
    res += s[i];
    ++i;
  }
  return make_pair(res, i);
}

struct Item {
  int depth;
  string key;
  int type;
  string obj_str;
  vector<Item> obj_map;
};

pair<vector<Item>, size_t> mapping(const string &s, size_t i,
                                   int old_depth = -1);

pair<Item, size_t> mapping_item(const string &s, size_t i, int front_depth) {
  int depth;
  tie(depth, i) = indent(s, i);
  if (front_depth != -1 && depth != front_depth)
    throw "fail";
  string key;
  tie(key, i) = get_str(s, i);
  ++i;
  if (s[i] == '\\n') { // mapping
    vector<Item> mapping_child;
    tie(mapping_child, i) = mapping(s, i + 1, depth);
    return make_pair((Item){depth, key, 1, "", std::move(mapping_child)}, i);
  } else { // string
    string obj;
    tie(obj, i) = get_str(s, i + 1);
    return make_pair((Item){depth, key, 0, obj, vector<Item>()}, i + 1);
  }
}

pair<vector<Item>, size_t> mapping(const string &s, size_t i, int old_depth) {
  vector<Item> res;
  Item front;
  tie(front, i) = mapping_item(s, i, -1);
  if (front.depth < old_depth)
    throw "fail";
  res.push_back(front);
  while (i < s.size()) {
    Item next;
    size_t next_i;
    try {
      tie(next, next_i) = mapping_item(s, i, front.depth);
    } catch (...) {
      break;
    }
    res.push_back(std::move(next));
    i = next_i;
  }
  return make_pair(std::move(res), i);
}

string query(const vector<Item> &obj, const string &s, size_t i) {
  string key_name;
  tie(key_name, i) = get_str(s, i + 1);
  for (auto &item : obj) {
    if (item.key == key_name) {
      if (item.type == 0) {
        if (i == s.size()) {
          return "string \\"" + item.obj_str + "\\"";
        } else {
          return "no such property";
        }
      } else {
        if (i == s.size()) {
          return "object";
        } else {
          return query(item.obj_map, s, i);
        }
      }
    }
  }
  return "no such property";
}

int main() {
  string q;
  getline(cin, q);
  string yaml;
  string line;
  while (getline(cin, line))
    yaml += line + "\\n";
  auto res = mapping(yaml, 0).first;
  cout << query(res, q, 0) << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define FOR(i, k, n) for (int i = (k); i < int(n); ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) begin(x), end(x)

using namespace std;

pair<int, size_t> indent(const string &s, size_t i) {
  int cnt = 0;
  while (i < s.size() && s[i] == ' ') {
    ++i;
    ++cnt;
  }
  return make_pair(cnt, i);
}

pair<string, size_t> get_str(const string &s, size_t i) {
  string res;
  while (isalnum(s[i]) || s[i] == ' ') {
    res += s[i];
    ++i;
  }
  return make_pair(res, i);
}

struct Item {
  int depth;
  string key;
  int type;
  string obj_str;
  vector<Item> obj_map;
};

pair<vector<Item>, size_t> mapping(const string &s, size_t i);

pair<Item, size_t> mapping_item(const string &s, size_t i) {
  int depth;
  tie(depth, i) = indent(s, i);
  string key;
  tie(key, i) = get_str(s, i);
  ++i;
  if (s[i] == '\\n') { // mapping
    vector<Item> mapping_child;
    tie(mapping_child, i) = mapping(s, i + 1);
    return make_pair((Item){depth, key, 1, "", std::move(mapping_child)}, i);
  } else { // string
    string obj;
    tie(obj, i) = get_str(s, i + 1);
    return make_pair((Item){depth, key, 0, obj, vector<Item>()}, i + 1);
  }
}

pair<vector<Item>, size_t> mapping(const string &s, size_t i) {
  vector<Item> res;
  Item front;
  tie(front, i) = mapping_item(s, i);
  res.push_back(front);
  while (i < s.size()) {
    Item next;
    size_t next_i;
    tie(next, next_i) = mapping_item(s, i);
    if (next.depth != front.depth)
      break;
    res.push_back(std::move(next));
    i = next_i;
  }
  return make_pair(std::move(res), i);
}

string query(const vector<Item> &obj, const string &s, size_t i) {
  string key_name;
  tie(key_name, i) = get_str(s, i + 1);
  for (auto &item : obj) {
    if (item.key == key_name) {
      if (item.type == 0) {
        if (i == s.size()) {
          return "string \\"" + item.obj_str + "\\"";
        } else {
          return "no such property";
        }
      } else {
        if (i == s.size()) {
          return "object";
        } else {
          return query(item.obj_map, s, i);
        }
      }
    }
  }
  return "no such property";
}

int main() {
  string q;
  getline(cin, q);
  string yaml;
  string line;
  while (getline(cin, line))
    yaml += line + "\\n";
  auto res = mapping(yaml, 0).first;
  cout << query(res, q, 0) << endl;
  return 0;
}