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

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define FOR(i, k, n) for (int i = (k); i < (int)(n); ++i)
#define REP(i, n) FOR(i, 0, n)
#define FORIT(i, c)                                                            \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)

template <class T> void debug(T begin, T end) {
  for (T i = begin; i != end; ++i)
    cerr << *i << " ";
  cerr << endl;
}
inline bool valid(int x, int y, int W, int H) {
  return (x >= 0 && y >= 0 && x < W && y < H);
}

typedef long long ll;
const int INF = 100000000;
const double EPS = 1e-8;
const int MOD = 1000000007;
int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};

int main() {
  int N;
  while (cin >> N) {
    vector<string> mark(N);
    REP(i, N) cin >> mark[i];
    vector<bool> re(N);
    REP(i, N) if (mark[i][mark[i].size() - 1] == 'v') {
      re[i] = true;
      mark[i] = mark[i].substr(0, mark[i].size() - 1);
    }
    vector<string> name(N);
    vector<int> val(N);
    REP(i, N) if (mark[i] != "-" && mark[i] != "") {
      REP(j, mark[i].size()) if (isdigit(mark[i][j])) {
        name[i] = mark[i].substr(0, j);
        val[i] = atoi(mark[i].substr(j).c_str());
        break;
      }
    }
    map<string, vector<int>> indexes;
    map<int, vector<int>> jump;
    vector<bool> used(N);
    vector<int> ans;
    REP(i, N) {
      if (val[i] >= 2) {
        indexes[name[i]].push_back(i);
        continue;
      }
      if (val[i] == 1) {
        jump[i] = indexes[name[i]];
        indexes[name[i]].clear();
      }
      if (re[i]) {
        continue;
      }

      stack<int> stk;
      stk.push(i);
      while (!stk.empty()) {
        int k = stk.top();
        stk.pop();
        if (used[k])
          continue;
        ans.push_back(k);
        used[k] = true;
        REP(j, jump[k].size()) { stk.push(jump[k][j]); }
        if (k - 1 >= 0 && re[k - 1]) {
          stk.push(k - 1);
        }
      }
    }
    assert(ans.size() == N);
    REP(i, N) cout << ans[i] + 1 << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define FOR(i, k, n) for (int i = (k); i < (int)(n); ++i)
#define REP(i, n) FOR(i, 0, n)
#define FORIT(i, c)                                                            \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)

template <class T> void debug(T begin, T end) {
  for (T i = begin; i != end; ++i)
    cerr << *i << " ";
  cerr << endl;
}
inline bool valid(int x, int y, int W, int H) {
  return (x >= 0 && y >= 0 && x < W && y < H);
}

typedef long long ll;
const int INF = 100000000;
const double EPS = 1e-8;
const int MOD = 1000000007;
int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
typedef pair<string, int> P;

int main() {
  int N;
  while (cin >> N && N) {
    vector<string> mark(N);
    REP(i, N) cin >> mark[i];
    vector<bool> re(N);
    REP(i, N) if (mark[i][mark[i].size() - 1] == 'v') {
      re[i] = true;
      mark[i] = mark[i].substr(0, mark[i].size() - 1);
    }
    vector<string> name(N);
    vector<int> val(N);
    REP(i, N) if (mark[i] != "-" && mark[i] != "") {
      REP(j, mark[i].size()) if (isdigit(mark[i][j])) {
        name[i] = mark[i].substr(0, j);
        val[i] = atoi(mark[i].substr(j).c_str());
        break;
      }
    }
    stack<int> stk;
    vector<bool> used(N);
    vector<int> answer;
    stk.push(0);
    map<string, deque<int>> indexes;
    map<string, bool> recur;
    while (!stk.empty()) {
      int k = stk.top();
      stk.pop();
      assert(k >= 0 && k < N);
      if (used[k])
        continue; // ???
      if (k != N - 1)
        stk.push(k + 1);

      // if(val[k] >= 2 && !used[index[P(name[k], val[k] - 1)]]) continue;
      if (val[k] >= 2 && !recur[name[k]]) {
        indexes[name[k]].push_front(k);
        continue;
      }
      if (re[k] && !used[k + 1])
        continue;

      answer.push_back(k);
      used[k] = true;

      if (name[k] != "") {
        if (val[k] == 1 && indexes[name[k]].size() >= 1) {
          recur[name[k]] = true;
          indexes[name[k]].push_front(k);
          stk.push(indexes[name[k]][val[k]]);
        } else {
          if (indexes[name[k]].size() - 1 >= val[k]) {
            stk.push(indexes[name[k]][val[k]]);
          } else {
            recur[name[k]] = false;
            indexes[name[k]].clear();
          }
        }
      }
      if (k >= 1 && re[k - 1])
        stk.push(k - 1);
    }
    assert(answer.size() == N);
    REP(i, answer.size()) cout << answer[i] + 1 << endl;
    // cout << endl;
  }
  return 0;
}