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

#include <bits/stdc++.h>

using namespace std;
typedef pair<int, int> P;

int main(void) {
  string s;
  cin >> s;

  int sum_all = 0, sum, lev = 0;
  stack<int> st;
  stack<P> stP;
  for (int i = 0; i < s.size(); i++) {
    if (s[i] == '\\\\')
      st.push(i);
    else if (s[i] == '/') {
      if (st.empty())
        continue;
      int j = st.top();
      st.pop();

      sum_all += i - j;
      sum = i - j;
      while (stP.size() > 0 && j < stP.top().first) {
        sum += stP.top().second;
        stP.pop();
      }
      stP.push(P(j, sum));
    }
  }

  vector<int> v;
  while (stP.size()) {
    v.push_back(stP.top().second);
    stP.pop();
  }
  reverse(v.begin(), v.end());

  cout << sum_all << endl;
  cout << v.size();
  for (int i = 0; i < v.size(); i++)
    cout << " " << v[i];
  cout << endl;

  return 0;
}


Error: 0

Fixed submission:

#include <iostream>
#include <stack>
#include <stdio.h>

#define BUF_SIZE 20000

using namespace std;

stack<int> ST;

void calcS() {}

int main() {

  char buf[BUF_SIZE];
  int heightTable[BUF_SIZE], maxHeightTable[BUF_SIZE], height = 0,
                                                       maxHeight = 0, i;

  fgets(buf, BUF_SIZE, stdin);

  char current, history = 's';

  for (i = 0;; i++) {
    current = buf[i];
    if (history == '/') {
      height += 1;
    } else if (history == '\\\\') {
      height -= 1;
    } else {
      // nothing
    }
    heightTable[i] = height;
    if (i == 0) {
      if (buf[0] == '\\\\') {
        maxHeightTable[i] = 0;
      } else {
        maxHeightTable[i] = -1;
      }

    } else {
      maxHeightTable[i] = maxHeightTable[i - 1];
    }

    if (maxHeight <= height && ((history == '/' && current == '\\\\') ||
                                (history == '_' && current == '\\\\') ||
                                (history == '/' && current == '\\n'))) {
      maxHeightTable[i] = i;
      maxHeight = height;
    }
    history = current;

    if (current == '\\n') {
      break;
    }
  }

  int total = 0, left = 0, baseHeight = 0, S = 0, tmp, joutei = 0, katei = 0,
      k = 0, pool_count = 0;

  for (; i > 0;) {

    if (maxHeightTable[i - 1] == -1) {
      break;
    } else {

      if (buf[i - 1] == '/') {
        tmp = 0;
        if (maxHeightTable[i] == i ||
            heightTable[i] > heightTable[maxHeightTable[i - 1]]) {
          left = maxHeightTable[i - 1];
          baseHeight = heightTable[left];
          k = left;
          while (true) {
            joutei = 10 * (baseHeight - heightTable[k]);
            katei = 10 * (baseHeight - heightTable[k + 1]);
            tmp += (joutei + katei) / 2;
            if (baseHeight == heightTable[k + 1]) {
              break;
            }
            k++;
          }
          S = tmp / 10;
          if (S != 0) {
            total += S;
            ST.push(S);
            pool_count++;
          }
          i = left;
        } else {
          for (k = i - 1; k >= 0; k--) {
            if (heightTable[k] == heightTable[i]) {
              break;
            }
          }
          left = k;
          baseHeight = heightTable[left];
          while (true) {
            joutei = 10 * (baseHeight - heightTable[k]);
            katei = 10 * (baseHeight - heightTable[k + 1]);
            tmp += (joutei + katei) / 2;
            if (baseHeight == heightTable[k + 1]) {
              break;
            }
            k += 1;
          }
          S = tmp / 10;
          if (S != 0) {
            total += S;
            ST.push(S);
            pool_count++;
          }
          i = left;
        }
      } else {
        i--;
      }
    }
  }

  printf("%d\\n", total);
  printf("%d", pool_count);
  while (!ST.empty()) {
    printf(" %d", ST.top());
    ST.pop();
  }
  printf("\\n");
}