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

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <string>
using namespace std;
int main() {
  char a;
  int Jlv = 0, Olv = 0, Ilv = 0, max = 0;
  int Jflg = 1, Oflg = 0, Iflg = 0;
  while (scanf("%c", &a) != EOF) {
    if (a == 10)
      break;
    // a=getchar();
    if (Jflg == 1) {
    Jland:
      if (a == 'J')
        Jlv++;
      else if (a == 'O') {
        Oflg = 1;
        Jflg = 0;
        goto Oland;
      } else
        Jlv = 0;
    }
    if (Oflg == 1) {
    Oland:
      if (a == 'O')
        Olv++;
      else if (a == 'I') {
        Iflg = 1;
        Oflg = 0;
        goto Iland;
      } else if (Jlv < Olv) {
        Jflg = 1;
        Jlv = 0;
        Oflg = 0;
        Olv = 0;
      } else if (a == 'J') {
        Jflg = 1;
        Jlv = 0;
        Oflg = 0;
        Olv = 0;
        Iflg = 0;
        Ilv = 0;
        goto Jland;
      } else {
        Jflg = 1;
        Jlv = 0;
        Oflg = 0;
        Olv = 0;
      }
    }
    if (Iflg == 1) {
    Iland:
      if (a == 'I')
        Ilv++;
      if (Olv == Ilv && Olv <= Jlv && max < Olv) {
        max = Olv;
        Jflg = 1;
        Jlv = 0;
        Oflg = 0;
        Olv = 0;
        Iflg = 0;
        Ilv = 0;
      }
      if (Olv < Ilv) {
        Jflg = 1;
        Jlv = 0;
        Oflg = 0;
        Olv = 0;
        Iflg = 0;
        Ilv = 0;
      }
      if (a != 'I' && a != 'J') {
        Jflg = 1;
        Jlv = 0;
        Oflg = 0;
        Olv = 0;
        Iflg = 0;
        Ilv = 0;
      }
      if (a == 'J') {
        Jflg = 1;
        Jlv = 0;
        Oflg = 0;
        Olv = 0;
        Iflg = 0;
        Ilv = 0;
        goto Jland;
      }
    }
    // cout<<"ok"<<endl;
    // cout<<Jlv<<Olv<<Ilv<<endl;
  }
  printf("%d\\n", max);
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
using namespace std;
char a[2000500] = {0};
int main() {
  int max = 0;
  int max2 = 0;
  scanf("%s", a);
  for (int i = 0; i <= 1000000; i++) {
    if (a[i] == '\\0')
      break;
    int jcnt = 1, ocnt = 1, icnt = 1;
    int jflg = 0, oflg = 0, iflg = 0;
    if (a[i] == 'J' && a[i + 1] == 'O' && a[i + 2] == 'I' && max == 0)
      max = 1;
    if (a[i] == 'J') {
      for (int j = i + 1; j <= 1000000; j++) {
        if (a[j] == 'J') {
          // cout<<"jok"<<endl;
          jcnt++;
          jflg = 1;
        } else if (a[j] != 'J')
          break;
      }
      if (a[i + jcnt] == 'O' && jflg == 1) {
        for (int k = i + jcnt + 1; k <= 1000000; k++) {
          if (a[k] == 'O') {
            // cout<<"ook"<<endl;
            ocnt++;
            oflg = 1;
          } else if (a[k] != 'O') {
            break;
          }
        }
        if (a[i + jcnt + ocnt] == 'I' && oflg == 1 && jcnt == ocnt) {
          for (int l = i + jcnt + ocnt + 1; l <= 1000000; l++) {
            if (a[l] == 'I') {
              // cout<<"iok"<<endl;
              icnt++;
              iflg = 1;
            }
            if (ocnt <
                icnt) // 原因　else
                      // if(ocnt<icnt)　としていたため,if(a[l]=='I')が優先され、icntがocntを超えてもこちらの処理に飛ばなかった
            {
              icnt -= 1;
              break;
            } else if (a[l] != 'I') {
              i += jcnt;
              break;
            }
          }
        }
      }
    }
    if (iflg == 1 && jcnt == ocnt && ocnt == icnt && jcnt == icnt &&
        max <= icnt) {
      max = icnt;
      i += jcnt + ocnt;
    }
  }
  printf("%d\\n", max);
  return 0;
}