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

#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <utility>
#include <vector>
#define INF 100000
using namespace std;

int main() {
  double ans = 0;
  map<int, int> check;
  string S;
  cin >> S;
  int temp = 20001;
  vector<int> hight(S.length() + 1);
  int hmin = 0;
  for (int i = 0; i <= S.length(); i++) {
    hight[i] = temp;
    check[temp]++;
    hmin = min(hmin, temp);
    if (S[i] == '\\\\') {
      temp--;
      continue;
    }
    if (S[i] == '/') {
      temp++;
      continue;
    }
    if (S[i] == '_') {
      continue;
    }
  }
  hight[S.length()] = temp;
  /*for (int i=0; i<=S.length(); i++) {
      cout << hight[i] << " " << check[hight[i]]<<endl;
  }*/
  int st = hight[0];
  vector<double> Q;
  int i = 0;
  while (i <= S.length()) {
    while (S[i] == '/' || S[i] == '_') {
      if (S[i] == '/') {
        check[hight[i]]--;
        st++;
        i++;
      } else if (S[i] == '_') {
        i++;
        check[hight[i]]--;
      }
    }
    if (i > S.length()) {
      break;
    }
    while (check[st] <= 1) {
      i++;
      st--;
      while (S[i] == '_') {
        check[hight[i]]--;
        i++;
      }
      if (i > S.length()) {
        break;
      }
    }
    if (i > S.length()) {
      break;
    }
    int ff = 0;
    double count = 0;
    check[hight[i]]--;
    for (int j = i + 1; j <= S.length(); j++) {
      if (hight[j] == st and ff) {
        if (count == 0) {
          break;
        }
        count += 0.5;
        Q.push_back(count);
        ans += count;
        i = j - 1;
        // cout << j<<"|"<<count<<endl;
        count = 0;
        break;
      } else {
        double hoge = (2 * st - hight[j - 1] - hight[j]) / 2.0;
        count += hoge;
        check[hight[j]]--;
        ff = 1;
        // cout << "|"<<count<<"("<<hoge<<")" << "|"<<endl;
      }
    }
    i++;
  }
  printf("%d", (int)ans);
  cout << endl;
  if (Q.size() != 0) {
    cout << Q.size() << " ";
  } else {
    cout << 0;
  }
  for (int i = 0; i < Q.size(); i++) {
    if (i == Q.size() - 1) {
      printf("%d", (int)Q[i]);
    } else {
      printf("%d ", (int)Q[i]);
    }
  }
  cout << endl;
}

Error: -11

Fixed submission:


#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <utility>
#include <vector>
#define INF 100000
using namespace std;

int main() {

  double ans = 0;
  map<int, int> check;
  vector<char> S;
  char p;
  int q = 0;
  while (cin >> p) {
    S[q++] = p;
  }
  cout << S.size();
  int temp = 30000;
  vector<int> hight(q + 1);
  int hmin = 0;
  for (int i = 0; i <= q; i++) {
    hight[i] = temp;
    check[temp]++;
    hmin = min(hmin, temp);
    if (S[i] == '\\\\') {
      temp--;
      continue;
    }
    if (S[i] == '/') {
      temp++;
      continue;
    }
    if (S[i] == '_') {
      continue;
    }
  }
  hight[q] = temp;
  /*for (int i=0; i<=S.length(); i++) {
      cout << hight[i] << " " << check[hight[i]]<<endl;
  }*/
  int st = hight[0];
  vector<double> Q;
  int i = 0;
  while (i <= q) {
    while (S[i] == '/' || S[i] == '_') {
      if (S[i] == '/') {
        check[hight[i]]--;
        st++;
        i++;
      } else if (S[i] == '_') {
        i++;
        check[hight[i]]--;
      }
    }
    if (i > q) {
      break;
    }
    while (check[st] <= 1) {
      i++;
      st--;
      while (S[i] == '_') {
        check[hight[i]]--;
        i++;
      }
      if (i > q) {
        break;
      }
    }
    if (i > q) {
      break;
    }
    int ff = 0;
    double count = 0;
    check[hight[i]]--;
    for (int j = i + 1; j <= q; j++) {
      if (hight[j] == st and ff) {
        if (count == 0) {
          break;
        }
        count += 0.5;
        Q.push_back(count);
        ans += count;
        i = j - 1;
        // cout << j<<"|"<<count<<endl;
        count = 0;
        break;
      } else {
        double hoge = (2 * st - hight[j - 1] - hight[j]) / 2.0;
        count += hoge;
        check[hight[j]]--;
        ff = 1;
        // cout << "|"<<count<<"("<<hoge<<")" << "|"<<endl;
      }
    }
    i++;
  }
  cout << ans << endl;
  if (Q.size() != 0) {
    cout << Q.size() << " ";
  } else {
    cout << 0;
  }
  for (int i = 0; i < Q.size(); i++) {
    if (i == Q.size() - 1) {
      cout << Q[i];
    } else {
      cout << Q[i] << " ";
    }
  }
  cout << endl;
}