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

#include "bits/stdc++.h"

using namespace std;

// int N,M,K,L,R,H,W;
long long int N, M, K, L, R, H, W;

constexpr long long int MOD = 1000000007;
// constexpr  int MOD=1000000007;
// constexpr int MOD=998244353;
// constexpr long long int MOD=998244353;

constexpr long double EPS = 1e-8;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  int ans = 0;
  string s;
  cin >> s;
  s = 'P' + s + 'P';
  for (int i = 1; i <= s.size(); i++) {
    if (s[i] == 'O') {
      int add = 0;
      while (s[i + add] == 'O')
        add++;
      bool flag = true;
      for (int k = i - 1; k >= i - add; k--) {
        // cout<<"J "<<k<<endl;
        flag &= s[k] == 'J';
        if (!flag)
          break;
      }
      for (int k = i + add; k < i + 2 * add; k++) {
        // cout<<"I "<<k<<endl;
        flag &= s[k] == 'I';
        if (!flag)
          break;
      }
      if (flag)
        ans = max(ans, add);
      i += add - 1;
    }
  }
  cout << ans << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include "stdio.h"

int num;
char letter[1000001] = {};
int sum[3][1000001] = {};
int ans = 0;
bool flag = 1;

int main() {
  // FILE *file;
  // file = fopen("1.txt", "r");
  scanf("%s", letter);
  for (int i = 0; i < 1000001; i++) {
    for (int j = 0; j < 3; j++) {
      sum[j][i + 1] = sum[j][i];
    }
    if (letter[i] == 'J')
      sum[0][i + 1]++;
    else if (letter[i] == 'O')
      sum[1][i + 1]++;
    else if (letter[i] == 'I')
      sum[2][i + 1]++;
    else {
      num = i;
      break;
    }
  }
  for (int i = 0; i < num; i++) {
    if (letter[i] == 'J') {
      for (int j = i + 3 + ans * 3; j <= num; j += 3) {
        //	printf(" %d\\n", j);
        if ((sum[1][j] - sum[1][i]) > (sum[0][j] - sum[0][i]))
          break;
        if ((sum[2][j] - sum[2][i]) > (sum[0][j] - sum[0][i]))
          break;
        if ((sum[2][j] - sum[2][i]) > (sum[1][j] - sum[1][i]))
          break;
        if (sum[1][j] - sum[1][i] != 0 || sum[2][j] - sum[2][i] != 0) {
          if (sum[0][j] - sum[0][i] !=
              sum[0][i + (sum[0][j] - sum[0][i]) * 3] - sum[0][i])
            break;
          if (sum[0][i + (sum[0][i + (sum[0][j] - sum[0][i]) * 3] - sum[0][i]) *
                             3] -
                  sum[0][i] !=
              sum[1][i + (sum[1][i + (sum[0][j] - sum[0][i]) * 3] - sum[1][i]) *
                             3] -
                  sum[1][i])
            break;
          if (sum[2][i + (sum[2][i + (sum[0][j] - sum[0][i]) * 3] - sum[2][i]) *
                             3] -
                  sum[2][i] !=
              sum[1][i + (sum[1][i + (sum[0][j] - sum[0][i]) * 3] - sum[1][i]) *
                             3] -
                  sum[1][i])
            break;
          if (sum[0][i + (sum[0][i + (sum[0][j] - sum[0][i]) * 3] - sum[0][i]) *
                             3] -
                  sum[0][i] !=
              sum[2][i + (sum[2][i + (sum[0][j] - sum[0][i]) * 3] - sum[2][i]) *
                             3] -
                  sum[2][i])
            break;
          flag = true;
          for (int k = 0; k < sum[0][j] - sum[0][i]; k++) {
            if (letter[i + k] != 'J') {
              flag = false;
              break;
            }
          }
          if (!flag)
            break;
          for (int k = 0; k < sum[0][j] - sum[0][i]; k++) {
            if (letter[i + k + sum[0][j] - sum[0][i]] != 'O') {
              flag = false;
              break;
            }
          }
          if (!flag)
            break;
          for (int k = 0; k < sum[0][j] - sum[0][i]; k++) {
            if (letter[i + k + (sum[0][j] - sum[0][i]) * 2] != 'I') {
              flag = false;
              break;
            }
          }
          if (ans < sum[0][j] - sum[0][i])
            ans = sum[0][j] - sum[0][i];
          i += (sum[0][j] - sum[0][i]) * 3 - 1;
          break;
        }
      }
    }
  }
  printf("%d\\n", ans);
  return 0;
}