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

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

#define BUF_SIZE 20008

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");
}


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");
}