Problem p02266 (C++) — buggy submission, verdict: Time Limit Exceeded

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

#define BUF_SIZE 20008

using namespace std;

int main() {

  char buf[BUF_SIZE];
  int heightTable[BUF_SIZE];
  int maxHeightTable
      [BUF_SIZE]; //??????????????????(?????????)????????????????????????????´?

  fgets(buf, BUF_SIZE, stdin);

  int height = 0;    //?????????0??¨??????
  int maxHeight = 0; //??????????????°???????????????????????????
  char current, history = 's';
  int i;

  for (i = 0;; i++) {
    current = buf[i];
    if (history == '/') {
      height += 1;
    } else if (history == '\\\\') {
      height -= 1;
    } else {
      // Do 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;
    }
  }

  /*
  for(int k = 0; k <= i; k++){
          printf("height[%d]:%d\\n",k,heightTable[k]);
  }

  for(int k = 0; k <= i; k++){
                  printf("maxHeight[%d]:%d\\n",k,maxHeightTable[k]);
  }*/

  //??¢????¨????
  stack<int> ST;
  int total = 0;

  int left = 0;       //?°´??????????????????
  int baseHeight = 0; //?°´??¢?????????
  int S = 0;          //?°´??????????????¢???(????????????)
  int tmp;            //?°´??????????????¢???(????????????)

  int joutei = 0;
  int katei = 0;
  int k = 0;

  int 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]]) { //??????????????????????????????????????????????????????????????????????????????
          //?????????????????§?????????????????????maxHeightTable[i-1]
          left = maxHeightTable[i - 1];
          baseHeight = heightTable[left];
          k = left;
          // printf("I am max i:%d left:%d\\n",i,left);
          while (true) {
            joutei = 10 * (baseHeight -
                           heightTable[k]); //??????(????§???¢??????????????§OK)
            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 {    // if(heightTable[maxHeightTable[i]] > heightTable[i])
                 // //?????????????????????????????????????±±???????????´???
          //????????????????????????????????????????????¨???????????????????????¢???(??????????±????????????¨??????)
          for (k = i - 1; k >= 0; k--) {
            if (heightTable[k] == heightTable[i]) {
              break;
            }
          }
          left = k;
          // printf("Other left:%d\\n",left);
          baseHeight = heightTable[left];
          while (true) {
            joutei = 10 * (baseHeight -
                           heightTable[k]); //??????(????§???¢??????????????§OK)
            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: TLE

Fixed submission:

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

#define BUF_SIZE 20001

using namespace std;

int main() {
  char buf[BUF_SIZE];
  int heightTable[BUF_SIZE];
  int maxHeightTable
      [BUF_SIZE]; //??????????????????(?????????)????????????????????????????´?

  fgets(buf, BUF_SIZE, stdin);

  int height = 0; //?????????0??¨??????
  int maxHeight = 0;
  char current, history = 's';
  int i;

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

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

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

  //??¢????¨????
  stack<int> ST;
  int total = 0;

  int left = 0;       //?°´??????????????????
  int baseHeight = 0; //?°´??¢?????????
  int S = 0;          //?°´??????????????¢???(????????????)
  double tmp;         //?°´??????????????¢???(????????????)

  double joutei = 0;
  double katei = 0;
  int k = 0;

  int pool_count = 0;

  for (; i >= 0;) { //?????????????????????

    if (buf[i - 1] == '/') { //???????????????????????????????????????
      tmp = 0;
      if (maxHeightTable[i] ==
          i) { //??????????????????????????????????????????????????????????????????????????????
        //?????????????????§?????????????????????maxHeightTable[i-1]
        left = maxHeightTable[i - 1];
        baseHeight = heightTable[left];
        k = left;
        while (true) {
          joutei =
              baseHeight - heightTable[k]; //??????(????§???¢??????????????§OK)
          katei = baseHeight - heightTable[k + 1]; //??????
          tmp += (joutei + katei) / 2;
          if (katei == 0) {
            break;
          }
          k += 1;
        }
        // double??????int?????????
        S = (int)tmp;
        if (S != 0) {
          total += S;
          ST.push(S);
          pool_count++;
        }
        i = left; //????????°?????????????§????
      } else if (
          maxHeightTable[i] !=
          0) { //?????????????????????????????????????±±???????????´???(????????\\???)
        //????????????????????????????????????????????¨???????????????????????¢???
        for (k = i - 1; k >= 0; k--) {
          if (heightTable[k] == heightTable[i]) {
            break;
          }
        }
        left = k;
        baseHeight = heightTable[left];
        while (true) {
          joutei =
              baseHeight - heightTable[k]; //??????(????§???¢??????????????§OK)
          katei = baseHeight - heightTable[k + 1]; //??????
          tmp += (joutei + katei) / 2;
          if (katei == 0) {
            break;
          }
          k++;
        }
        // double??????int?????????
        S = (int)tmp;
        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");
}