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

#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
int main() {

  char str[128] = {0};
  int cnt = 1;
  int data[128][128] = {{0}};
  int q = 1;
  bool f = false;
  bool god = false;
  int hoge = 0;

  memset(data, -1, sizeof(data));

  while (scanf("%s", str) != EOF) {

    if (strlen(str) == 2 || strlen(str) == 1)
      hoge++;

    for (int i = 0;;) {
      if (str[i] == ',') {
        i++;
        continue;
      } else if (str[i] != ',' && str[i + 1] == ',') {
        data[cnt][q] = str[i] - 48;
        q++;
        i++;
      } else if (str[i] != ',' && str[i + 1] == 0) {
        data[cnt][q] = str[i] - 48;
        break;
      } else if (str[i] != ',' && str[i + 1] != ',' && str[i + 2] == ',') {
        data[cnt][q] = (str[i] - 48) * 10 + (str[i + 1] - 48);
        q++;
        i += 2;
      } else if (str[i] != ',' && str[i + 1] != ',' && str[i + 2] == 0) {
        data[cnt][q] = (str[i] - 48) * 10 + (str[i + 1] - 48);
        break;
      }
    }
    q = 1;
    cnt++;

    if (hoge == 2)
      f = true;

    if (f == true) {
      q = 2;
      int dp[128][128] = {{0}};
      int x = (cnt / 2);

      memset(dp, -1, sizeof(dp));

      dp[1][1] = data[1][1];
      bool flg = false;

      for (int i = 1; i < cnt; i++) {
        for (int j = 1; j <= q; j++) {
          if (q <= x && flg == false) {
            if (j == 1) {
              dp[i + 1][j] = dp[i][j] + data[i + 1][j];
            } else if (j == q + 1) {
              dp[i + 1][j] = dp[i][j - 1] + data[i + 1][j];
            }

            else {
              dp[i + 1][j] =
                  max(dp[i][j - 1] + data[i + 1][j], dp[i][j] + data[i + 1][j]);
            }
          } else if (flg == true) {
            dp[i + 1][j] =
                max(dp[i][j + 1] + data[i + 1][j], dp[i][j] + data[i + 1][j]);
          }
        }
        // printf("q:%d\\n",q);
        if (flg == false && q < x)
          q++;
        else if (q == x) {
          flg = true;
          q--;
        } else
          q--;
      }

      printf("%d\\n", dp[cnt - 1][1]);
      hoge = 0;
      memset(data, -1, sizeof(data));
      cnt = 1;
      q = 1;
    }
    f = false;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
int main() {

  char str[128] = {0};
  int cnt = 1;
  int data[128][128] = {{0}};
  int q = 1;
  bool f = false;
  int hoge = 0;

  memset(data, -1, sizeof(data));

  while (scanf("%s", str) != EOF) {

    if (strlen(str) == 1 || strlen(str) == 2)
      hoge++;

    for (int i = 0; i < strlen(str);) {
      if (str[i] == ',')
        continue;
      else {
        if (str[i + 1] == ',' || str[i + 1] == 10) {
          data[cnt][q] = str[i] - 48;
          i++;
        } else {
          data[cnt][q] = (str[i] - 48) * 10 + (str[i] - 48);
          i += 2;
        }
        q++;
      }
    }
    q = 1;
    cnt++;

    if (hoge == 2)
      f = true;

    if (f == true) {
      q = 2;
      int dp[128][128] = {{0}};
      int x = (cnt / 2);

      memset(dp, -1, sizeof(dp));

      dp[1][1] = data[1][1];
      bool flg = false;

      for (int i = 1; i < cnt; i++) {
        for (int j = 1; j <= q; j++) {
          if (q <= x && flg == false) {
            if (j == 1) {
              dp[i + 1][j] = dp[i][j] + data[i + 1][j];
            } else if (j == q + 1) {
              dp[i + 1][j] = dp[i][j - 1] + data[i + 1][j];
            }

            else {
              dp[i + 1][j] =
                  max(dp[i][j - 1] + data[i + 1][j], dp[i][j] + data[i + 1][j]);
            }
          } else if (flg == true) {
            dp[i + 1][j] =
                max(dp[i][j + 1] + data[i + 1][j], dp[i][j] + data[i + 1][j]);
          }
        }
        // printf("q:%d\\n",q);
        if (flg == false && q < x)
          q++;
        else if (q == x) {
          flg = true;
          q--;
        } else
          q--;
      }

      printf("%d\\n", dp[cnt - 1][1]);
      hoge = 0;
      memset(data, -1, sizeof(data));
      cnt = 1;
      q = 1;
    }
    f = false;
  }
  return 0;
}