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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <fstream>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
const double EPS = 1e-9;
typedef vector<int> vint;
typedef pair<int, int> pint;
#define rep(i, n) REP(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define MSG(a) cout << #a << " " << a << endl;
#define REP(i, x, n) for (int i = x; i < n; i++)
template <class T> T RoundOff(T a) { return int(a + .5 - (a < 0)); }
template <class T, class C> void chmax(T &a, C b) {
  if (a < b)
    a = b;
}
template <class T, class C> void chmin(T &a, C b) {
  if (b < a)
    a = b;
}
template <class T, class C> pair<T, C> mp(T a, C b) { return make_pair(a, b); }

int main() {
  string line;
  cin >> line;

  int n = line.size();

  vector<pair<char, int>> table;

  char now = line[0];
  int cnt = 0;
  rep(i, n) {
    cnt += now == line[i];

    if (now != line[i] || i + 1 == n) {
      table.push_back(mp(now, cnt));
      now = line[i];
      cnt = 1;
    }
  }

  if (now != line[n - 2])
    table.push_back(mp(now, 1));

  int res = 0;
  REP(i, 1, table.size() - 1) {
    if (table[i].first == 'O') {
      int num = table[i].second;

      if (table[i - 1].first == 'J' && num <= table[i - 1].second &&
          table[i + 1].first == 'I' && num <= table[i + 1].second)
        chmax(res, num);
    }
  }

  cout << res << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <fstream>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
const double EPS = 1e-9;
typedef vector<int> vint;
typedef pair<int, int> pint;
#define rep(i, n) REP(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define MSG(a) cout << #a << " " << a << endl;
#define REP(i, x, n) for (int i = x; i < n; i++)
template <class T> T RoundOff(T a) { return int(a + .5 - (a < 0)); }
template <class T, class C> void chmax(T &a, C b) {
  if (a < b)
    a = b;
}
template <class T, class C> void chmin(T &a, C b) {
  if (b < a)
    a = b;
}
template <class T, class C> pair<T, C> mp(T a, C b) { return make_pair(a, b); }

int comp(char c) {
  if (c == 'J')
    return 0;
  if (c == 'O')
    return 1;
  if (c == 'I')
    return 2;
}

int main() {
  string line;
  cin >> line;

  int n = line.size();

  vector<vint> table(n + 1, vint(3, 0));
  rep(i, n) rep(j, 3) table[i + 1][j] = table[i][j] + (comp(line[i]) == j);

  int res = 0, k;
  rep(i, n) for (k = (n - i) / 3; 0 < k; k--) {
    if (table[i + k][0] - table[i][0] != k)
      continue;
    if (table[i + 2 * k][1] - table[i + k][1] != k)
      continue;
    if (table[i + 3 * k][2] - table[i + 2 * k][2] != k)
      continue;

    if (res < k) {
      res = k;
      break;
    }
  }

  cout << res << endl;
}