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

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <queue>
#include <string>

using namespace std;

class stack {
public:
  int s[10000];
  int t, temp;

public:
  void initialize();
  void push(int x);
  int pop();
};

void stack::initialize() { t = 0; }

void stack::push(int x) {
  t++;
  s[t] = x;
}

int stack::pop() {
  temp = s[t];
  t--;
  return temp;
}

int main() {
  stack Lake_right;
  queue<int> Lake;
  int depth = 0, A = 0, i = 0, last = 0, n = 0, k = 0;
  double S = 0;
  string str;

  Lake_right.initialize();

  getline(cin, str);
  n = str.length();

  while (i != n) {
    if (str[i] == '\\\\') {
      S += 0.5 + depth;
      depth++;

    } else if (S != 0 && str[i] == '/') {
      depth--;
      S += 0.5 + depth;
    } else if (S != 0 && str[i] == '_') {
      S += depth;
    }

    // cout << "S=" << S << endl;

    if (S != 0 && depth == 0) {
      Lake.push((int)S);
      A += (int)S;
      S = 0;
      last = i;
      k++;
    }

    i++;
  }

  S = 0;
  depth = 0;

  i = n - 1;

  while (i != last - 1) {
    if (str[i] == '/') {
      S += 0.5 + depth;
      depth++;

    } else if (S != 0 && str[i] == '\\\\') {
      depth--;
      S += 0.5 + depth;
    } else if (S != 0 && str[i] == '_') {
      S += depth;
    }

    // cout << "S=" << S << endl;

    if (S != 0 && depth == 0) {
      Lake_right.push((int)S);
      A += (int)S;
      S = 0;
      k++;
    }

    i--;
  }

  while (Lake_right.t != 0) {
    Lake.push(Lake_right.pop());
  }

  printf("%d\\n", A);
  printf("%d", k);

  while (!Lake.empty()) {
    printf(" %d", Lake.front());
    Lake.pop();
  }

  printf("\\n");

  return 0;
}


Error: -11

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <queue>
#include <string>

using namespace std;

class stack {
public:
  int s[10000];
  int t;

public:
  void initialize();
  void push(int x);
  int pop();
  int size();
};

void stack::initialize() { t = 0; }

void stack::push(int x) {
  t++;
  s[t] = x;
}

int stack::pop() {
  t--;
  return s[t];
}
int stack::size() { return t; }

int main() {
  stack slash;
  stack backslash;
  queue<int> Lake;
  stack Lake2;
  int A, k;
  int n, start, countslash, countbackslash;
  double lake;
  string str;

  getline(cin, str);
  n = str.length();
  start = 0;

  slash.initialize();
  backslash.initialize();

  for (int i = start; i < n; i++) {

    if (str[i] == '\\\\') {
      // cout << "\\\\" << endl;
      slash.push(i);
      lake += (slash.size() - 0.5);

      // cout << slash.size();

      countslash++;

      // cout << "lake = " << lake << endl;
    } else if (str[i] == '/') {
      countbackslash++;

      if (slash.size() != 0) {
        // cout << "/" << endl;
        backslash.push(i);
        lake += (slash.size() - backslash.size() + 0.5);
        // cout << "lake = " << lake << endl;
      }
    } else {
      lake += slash.size();
      // cout << "lake = " << lake << endl;
    }

    if (slash.size() != 0 && slash.size() == backslash.size()) {
      Lake.push(lake);
      k++;
      A += lake;
      start = i + 1;
      slash.initialize();
      backslash.initialize();
    }
  }

  if (countslash != countbackslash) {
    start = n - 1;

    for (int i = start; i >= 0; i--) {
      slash.initialize();
      backslash.initialize();

      if (str[i] == '/') {
        backslash.push(i);
        lake += (backslash.size() - 0.5);
      } else if (str[i] == '\\\\') {
        if (!backslash.size()) {
          slash.push(i);
          lake += (backslash.size() - slash.size() + 0.5);
        }
      } else {
        lake += backslash.size();
      }

      if (backslash.size() != 0 && slash.size() == backslash.size()) {
        Lake2.push(lake);
        k++;
        A += lake;
        start = i + 1;
        slash.initialize();
        backslash.initialize();
      }
    }
  }

  while (Lake2.size() != 0) {
    Lake.push(Lake2.pop());
  }

  printf("%d\\n", A);
  printf("%ld ", Lake.size());

  for (int i = 1; i <= k; i++) {

    printf("%d", Lake.front());
    if (i != k) {
      printf(" ");
      Lake.pop();

    } else {
      printf("\\n");
    }
  }

  return 0;
}