alexjercan/bugnet huggingface dataset:
Problem p00494 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <string>
#include <vector>
using namespace std;

string joi(int lv) {
  string s = "";
  for (int i = 0; i < lv; i++)
    s += 'J';
  for (int i = 0; i < lv; i++)
    s += 'O';
  for (int i = 0; i < lv; i++)
    s += 'I';
  return s;
}

int main() {
  string s;
  vector<pair<int, int>> os;
  cin >> s;
  int cnt = 0, start;
  for (int i = 0; i < s.length(); i++) {
    if (s[i] == 'O') {
      if (cnt == 0)
        start = i;
      cnt++;
    } else if (cnt != 0) {
      os.push_back(make_pair(start, cnt));
      cnt = 0;
    }
  }
  int maxlv = 0;
  for (int i = 0; i < os.size(); i++) {
    if (os[i].second > os[i].first ||
        os[i].first + os[i].second * 2 > s.length())
      continue;
    if (os[i].second <= maxlv)
      continue;
    bool flag = true;
    for (int j = os[i].first - os[i].second; j < os[i].first; j++) {
      if (s[j] != 'J') {
        flag = false;
        break;
      }
    }
    if (!flag)
      continue;
    for (int j = os[i].first + os[i].second; j < os[i].first + os[i].second * 2;
         j++) {
      if (s[j] != 'I') {
        flag = false;
        break;
      }
    }
    if (flag)
      maxlv = os[i].second;
  }
  cout << maxlv << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <string>
using namespace std;

string joi(int lv) {
  string s = "";
  for (int i = 0; i < lv; i++)
    s += 'J';
  for (int i = 0; i < lv; i++)
    s += 'O';
  for (int i = 0; i < lv; i++)
    s += 'I';
  return s;
}

int main() {
  string s;
  cin >> s;
  for (int i = s.size() / 3; i >= 1; i--) {
    if (s.find(joi(i)) != string::npos) {
      cout << i << endl;
      return 0;
    }
  }
  cout << 0 << endl;
  return 0;
}
Problem p00494 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
char g1[1000000];
int g2[1000000];
int main() {
  string a;
  cin >> a;
  int ma = 0;
  int s = 0;
  int n = 0;
  while (n < a.length()) {
    int j = n;
    char h = a[n];
    while (a[j] == a[n]) {
      j++;
    }
    g1[s] = h;
    g2[s] = j - n;
    n = j;
    s++;
  }
  for (int i = 1; i < s - 1; i++) {
    if (g1[i] == 'O') {
      if (g1[i - 1] == 'J' && g1[i + 1] == 'I') {
        if (g2[i] <= g2[i + 1] && g2[i] <= g2[i - 1]) {
          ma = max(ma, g2[i]);
        }
      }
    }
  }
  cout << ma << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
int main() {
  string a;
  cin >> a;
  int ma = 0;
  for (int i = 1; i < a.length(); i++) {
    if (a[i] == 'O') {
      int j = i;
      while (a[j] == 'O') {
        j++;
      }
      int k = j - i;
      if (a[i + k] != 'I' || a[i - 1] != 'J')
        goto stop;
      j = 1;
      while (i - j >= 0 && a[i - j] == 'J') {
        j++;
      }
      int l = j - 1;
      j = 0;
      while (j + i + k < a.length() && a[j + k + i] == 'I') {
        j++;
      }
      int m = j;
      if (m >= k && l >= k) {
        ma = max(k, ma);
      }
    }
  stop:;
  }
  cout << ma << endl;
}
Problem p00494 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>

using namespace std;
typedef pair<char, int> P;

string s;

int main() {
  cin >> s;
  vector<P> v;
  char c = s[0];
  int length = 1;
  for (int i = 1; i < s.size(); i++) {
    if (s[i] == c)
      length++;
    else {
      v.push_back(P(c, length));
      c = s[i], length = 1;
    }
  }

  v.push_back(P(c, length));

  int ans = 0;

  if (v.size() < 3) {
    cout << 0 << endl;
    return 0;
  }

  for (int i = 0; i < v.size() - 2; i++) {
    if (v[i].first == 'J' && v[i + 1].first == 'O' && v[i + 2].first == 'I') {
      if (v[i].second >= v[i + 1].second &&
          v[i + 1].second <= v[i + 2].second) {
        ans = max(ans, v[i + 1].second);
      }
    }
  }

  cout << ans << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>

using namespace std;
typedef pair<char, int> P;

string s;

int main() {
  cin >> s;
  vector<P> v;
  char c = s[0];
  int length = 1;
  for (int i = 1; i < s.size(); i++) {
    if (s[i] == c)
      length++;
    else {
      v.push_back(P(c, length));
      c = s[i], length = 1;
    }
  }

  v.push_back(P(c, length));

  int ans = 0;

  for (int i = 0; i < v.size() - 2; i++) {
    if (v[i].first == 'J' && v[i + 1].first == 'O' && v[i + 2].first == 'I') {
      if (v[i].second >= v[i + 1].second &&
          v[i + 1].second <= v[i + 2].second) {
        ans = max(ans, v[i + 1].second);
      }
    }
  }

  cout << ans << endl;

  return 0;
}
Problem p00494 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <queue>
#include <vector>

using namespace std;

struct char_len {
  char c;
  int num;
};

int main() {

  vector<char_len> v;

  string str;

  cin >> str;

  char c1 = str[0];
  int num1 = 1;
  int count = 0;
  char_len p1;
  for (int i = 1; i < str.size(); i++) {
    if (str[i] == c1) {
      num1++;
    } else {
      p1.c = c1;
      p1.num = num1;
      v.push_back(p1);
      count++;
      c1 = str[i];
      num1 = 1;
    }
  }
  p1.c = c1;
  p1.num = num1;
  v.push_back(p1);
  count++;

  int ans = 0;

  for (int i = 0; i < count - 2; i++) {
    if (v.at(i).c == 'J' && v.at(i + 1).c == 'O' && v.at(i + 2).c == 'I' &&
        v.at(i).num >= v.at(i + 1).num && v.at(i + 1).num <= v.at(i + 2).num &&
        v.at(i + 1).num > ans) {
      ans = v.at(i + 1).num;
    }
  }

  printf("%d\\n", ans);

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <queue>
#include <vector>

using namespace std;

struct char_len {
  char c;
  int num;
};

int main() {

  vector<char_len> v;

  string str;

  cin >> str;

  if (str.size() < 3) {
    printf("0\\n");
    return 0;
  }

  char c1 = str[0];
  int num1 = 1;

  char_len p1;
  for (int i = 1; i < str.size(); i++) {
    if (str[i] == c1) {
      num1++;
    } else {
      p1.c = c1;
      p1.num = num1;
      v.push_back(p1);
      c1 = str[i];
      num1 = 1;
    }
  }
  p1.c = c1;
  p1.num = num1;
  v.push_back(p1);

  int ans = 0;

  for (int i = 0; i < v.size() - 2; i++) {
    if (v.at(i).c == 'J' && v.at(i + 1).c == 'O' && v.at(i + 2).c == 'I' &&
        v.at(i).num >= v.at(i + 1).num && v.at(i + 1).num <= v.at(i + 2).num &&
        v.at(i + 1).num > ans) {
      ans = v.at(i + 1).num;
    }
  }

  printf("%d\\n", ans);

  return 0;
}
Problem p00494 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
int main() {
  string s;
  cin >> s;
  int ans = 0;
  for (int i = 0; i < s.size(); i++) {
    if (s[i] != 'J')
      continue;
    else {
      int now = i + 1;
      int J = 1, O = 0, I = 0;
      int flag1 = 1, flag2 = 0;
      while (s[now] == 'J') {
        // cout<<now<<endl;
        J++;
        now++;
      }
      while (s[now] == 'O' && flag1) {
        // cout<<now<<endl;
        O++;
        now++;
        flag2 = 1;
      }
      if (J < O) {
        i = now - 1;
        continue;
      }
      while (s[now] == 'I' && flag2) {
        // cout<<now<<endl;
        I++;
        now++;
      }
      if (O > I) {
        i = now - 1;
        continue;
      }
      ans = max(ans, min(I, min(O, J)));
      // cout<<ans<<endl;
      i = now - 1;
      // cout<<i<<endl;
      // cout<<s[i]<<endl;
    }
  }
  cout << ans << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  string s;
  cin >> s;
  int ans = 0;
  for (int i = 0; i < s.size(); i++) {
    if (s[i] != 'J')
      continue;
    else {
      int now = i + 1;
      int J = 1, O = 0, I = 0;
      int flag1 = 1, flag2 = 0;
      while (s[now] == 'J') {
        // cout<<now<<endl;
        J++;
        now++;
      }
      while (s[now] == 'O' && flag1) {
        // cout<<now<<endl;
        O++;
        now++;
        flag2 = 1;
      }
      if (J < O) {
        continue;
      }
      while (s[now] == 'I' && flag2) {
        // cout<<now<<endl;
        I++;
        now++;
      }
      if (O > I) {
        continue;
      }
      ans = max(ans, min(I, min(O, J)));
      // cout<<ans<<endl;
      i = now - 1;
      // cout<<i<<endl;
      // cout<<s[i]<<endl;
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p00494 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <queue>
#include <string>

using namespace std;

typedef pair<char, int> P;

int main() {
  string instr;
  queue<P> que;

  cin >> instr;

  int cnt = 1;

  for (int i = 1; i < (int)instr.size(); ++i) {
    if (instr[i] == instr[i - 1])
      ++cnt;
    else {
      que.push(make_pair(instr[i - 1], cnt));
      cnt = 1;
    }
  }
  que.push(make_pair(instr[instr.size() - 1], cnt));

  int ans = 0;

  while (!que.empty()) {
    P p = que.front();
    que.pop();
    if (p.first != 'J')
      continue;
    int len = p.second;
    if (que.empty())
      break;
    p = que.front();
    if (p.first != 'O') {
      if (p.first == 'I')
        que.pop();
      continue;
    }
    que.pop();
    if (p.second > len)
      continue;
    len = p.second;
    if (que.empty())
      break;
    p = que.front();
    if (p.first != 'I')
      continue;
    que.pop();
    if (len > p.second)
      continue;
    ans = max(ans, len);
  }

  cout << ans << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <string>

using namespace std;

int main() {
  int N, K;
  string instr;
  string JOI;
  string J, O, I;

  bool flag = false;

  cin >> instr;

  N = (int)instr.size();

  J = "";
  O = "";
  I = "";
  for (int i = 0; i < N / 3; ++i) {
    J += 'J';
    O += 'O';
    I += 'I';
  }

  for (K = N / 3; K >= 1; --K) {
    JOI = "";
    JOI += J.substr(0, K);
    JOI += O.substr(0, K);
    JOI += I.substr(0, K);
    if (instr.find(JOI) != instr.npos) {
      flag = true;
      break;
    }
  }

  cout << (flag ? K : 0) << endl;

  return 0;
}
Problem p00494 (C++) — buggy submission, verdict: Time Limit Exceeded

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

using namespace std;

int main() {
  char *JOI;
  int lvc = 0, j, o, i, c, J, O, I, minlv;

  JOI = new char[1000000];

  cin >> JOI;

  for (c = 0; c < strlen(JOI); c++) {
    J = O = I = 0;
    if (JOI[c] == 'J') {
      for (j = c; JOI[j] == 'J'; j++) {
        J++;
      }
      for (o = j; JOI[o] == 'O'; o++) {
        O++;
      }
      for (i = o; JOI[i] == 'I'; i++) {
        I++;
      }
      minlv = min(J, O);
      minlv = min(minlv, I);

      if (O <= minlv && minlv > lvc) {
        lvc = minlv;
      }
      c = i - 1;
    }
  }

  cout << lvc << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <string.h>
using namespace std;

int main() {

  int K, cnt;
  char *in;
  char *joi;
  char JOI[4] = {"JOI"};
  bool flg = false;

  in = new char[1000001];
  joi = new char[1000001];

  cin >> in;

  K = strlen(in) / 3;

  for (int i = K; i > 0; i--) {
    cnt = 0;
    for (int c = 0; c < 3; c++) {
      for (int j = 0; j < i; j++) {
        joi[cnt] = JOI[c];
        cnt++;
      }
    }
    joi[cnt] = '\\0';
    if (strstr(in, joi) != NULL) {
      cout << i << endl;
      flg = true;
      break;
    }
  }
  if (flg == false) {
    cout << 0 << endl;
  }

  return 0;
}
Problem p00494 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <functional>
using namespace std;
char s[1000001], ss[1000000];
int suu[1000000], cnt[1000000];
int main(void) {
  int i, j, kazu, max = 0, len;
  scanf("%s", s);
  kazu = 0;
  cnt[kazu] = 1;
  ss[kazu] = s[kazu];
  len = strlen(s);
  for (i = 1; i < len; i++) {
    if (ss[kazu] == s[i])
      cnt[kazu]++;
    else {
      kazu++;
      cnt[kazu] = 1;
      ss[kazu] = s[i];
    }
  }
  for (i = 0; i <= kazu; i++) {
    if (ss[i] == 'O') {
      if (i - 1 >= 0 && i + 1 <= kazu) {
        if (ss[i - 1] == 'J' && cnt[i - 1] >= cnt[i]) {
          if (ss[i + 1] == 'I' && cnt[i + 1] >= cnt[i]) {
            if (max < cnt[i])
              max = cnt[i];
          }
        }
      }
    }
  }
  printf("%d\\n", max);
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <functional>
using namespace std;
char s[1000001], ss[1000000];
int suu[1000000], cnt[1000000];
int main(void) {
  int i, j, kazu, max = 0;
  scanf("%s", s);
  for (i = 0; i < 1000000; i++)
    cnt[i] = 0;
  kazu = 0;
  cnt[kazu] = 1;
  ss[kazu] = s[kazu];
  for (i = 1; i < strlen(s); i++) {
    if (ss[kazu] == s[i])
      cnt[kazu]++;
    else {
      kazu++;
      cnt[kazu] = 1;
      ss[kazu] = s[i];
    }
  }
  for (i = 0; i <= kazu; i++) {
    if (ss[i] == 'O') {
      if (i - 1 >= 0 && i + 1 <= kazu) {
        if (ss[i - 1] == 'J' && cnt[i - 1] >= cnt[i]) {
          if (ss[i + 1] == 'I' && cnt[i + 1] >= cnt[i]) {
            if (max < cnt[i])
              max = cnt[i];
          }
        }
      }
    }
  }
  printf("%d\\n", max);
  return 0;
}
Problem p00494 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define SE second
#define FI first
vector<pair<int, char>> ss;
string str;
int ans, lim, j;

int main() {
  cin >> str;
  for (int i = 1; i <= str.size(); i++) {
    if (str[i - 1] == str[i]) {
      for (j = 1; j < str.size(); j++) {
        if (str[i + j - 1] == str[i + j])
          ;
        else
          break;
      }
      // cout << str[i-1] << endl;
      ss.push_back(make_pair(1 + j, str[i - 1]));
      i += j;
    } else
      ss.push_back(make_pair(1, str[i - 1]));
  }
  // printf("ss.size()=%d\\n",ss.size());
  for (int i = 2; i < ss.size(); i++) {
    if (ss[i - 2].SE == 'J' && ss[i - 1].SE == 'O' && ss[i].SE == 'I') {
      // if(ss[i-2].FI==ss[i-1].FI&&ss[i-1].FI==ss[i].FI)ans=max(ans,ss[i].FI);
      if (ss[i - 2].FI >= ss[i - 1].FI && ss[i].FI >= ss[i - 1].FI)
        ans = max(ans, ss[i - 1].FI);
    }
  }
  cout << ans << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int ans, s, lim;
string str1;
int solve(int here, int n);

int main() {

  cin >> str1;
  for (int i = 0; i < str1.size(); i++) {
    if (str1[i] == 'J') {
      for (s = 1; s < str1.size() / 2; s++) {
        // cout << j << "go go!" <<  endl;
        if (str1[i + s] == 'J')
          ;
        else
          break;
      }
      lim = solve(s + i - 1, s);
      ans = max(ans, lim);
      if (lim > 0)
        i += lim * 3 - 1;
      else
        i += s - 1;
    }
  }
  cout << ans << endl;
  // cin >> ans;

  return 0;
}

int solve(int here, int n) {
  int sum = 0;
  bool flag = false;

  for (int i = n; i > 0; i--) {
    for (int j = 1; j <= i; j++) {
      if (str1[here + j] == 'O' && str1[here + i + j] == 'I')
        ;
      else
        break;
      if (j == i) {
        flag = true;
        sum = max(sum, j);
      }
    }
    if (flag == true)
      break;
  }
  return sum;
}
Problem p00494 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;

const static char c[] = {'J', 'O', 'I'};
int N;
string S;
int T[3][1000001];
vector<pair<int, int>> v;

bool isLevelK(int k, int p) {
  if (p + k >= N || p + 2 * k >= N)
    return false;
  return T[1][p + k] == k && T[2][p + 2 * k] >= k;
}

int main() {
  cin >> S;
  N = S.size();

  for (int p = 0; p < 3; ++p) {
    for (int i = N - 1; i >= 0; --i) {
      if (S[i] == c[p]) {
        if (i + 1 == N)
          T[p][i] = 1;
        else
          T[p][i] = T[p][i + 1] + 1;
        if (p == 0)
          v.push_back(make_pair(-T[p][i], i));
      } else {
        T[p][i] = 0;
      }
    }
  }
  sort(v.begin(), v.end());
  int ans = 0;
  for (int i = 0; i < v.size(); ++i) {
    if (isLevelK(-v[i].first, v[i].second)) {
      ans = -v[i].first;
      break;
    }
  }
  cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;

const static char c[] = {'J', 'O', 'I'};
int N;
string S;
int T[3][100001];
vector<pair<int, int>> v;

bool isLevelK(int k, int p) {
  if (p + k >= N || p + 2 * k >= N)
    return false;
  return T[1][p + k] == k && T[2][p + 2 * k] >= k;
}

int main() {
  cin >> S;
  N = S.size();

  for (int p = 0; p < 3; ++p) {
    for (int i = N - 1, j = 0; i >= 0; --i) {
      if (S[i] == c[p]) {
        if (i + 1 == N)
          T[p][i] = 1;
        else
          T[p][i] = T[p][i + 1] + 1;
        if (p == 0)
          v.push_back(make_pair(-T[p][i], i));
      } else {
        T[p][i] = 0;
      }
    }
  }
  sort(v.begin(), v.end());
  int ans = 0;
  for (int i = 0; i < v.size(); ++i) {
    if (isLevelK(-v[i].first, v[i].second)) {
      ans = -v[i].first;
      break;
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p00494 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <vector>
using namespace std;

int main() {
  static char str[1200000];
  int len;
  vector<pair<char, int>> x;
  scanf("%s", str);
  len = strlen(str);
  x.reserve(len);
  for (int i = 0; i < len; ++i) {
    int it = i + 1;
    while (it < len && str[it] == str[i])
      ++it;
    x.push_back(make_pair(str[i], it - i));
    i = it - 1;
  }
  int ans = 0;
  for (int i = 0; i < (int)x.size() - 2; ++i) {
    pair<char, int> jjj = x[i], ooo = x[i + 1], iii = x[i + 2];
    if (jjj.first != 'J' || ooo.first != 'O' || iii.first != 'I')
      continue;
    if (ooo.second > jjj.second || ooo.second > iii.second)
      continue;
    ans = max(ans, min(min(jjj.second, ooo.second), iii.second));
  }

  printf("%d\\n", ans);
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <vector>
using namespace std;

int main() {
  static char str[1200000];
  int len;
  vector<pair<char, int>> x;
  scanf("%s", str);
  len = strlen(str);
  x.reserve(len);
  for (int i = 0; i < len; ++i) {
    int it = i;
    while (it < len && str[it] == str[i])
      ++it;
    x.push_back(make_pair(str[i], it - i));
    i = it - 1;
  }
  int ans = 0;
  for (int i = 0; i < x.size() - 2; ++i) {
    pair<char, int> jjj = x[i], ooo = x[i + 1], iii = x[i + 2];
    if (jjj.first != 'J' || ooo.first != 'O' || iii.first != 'I')
      continue;
    if (ooo.second > jjj.second || ooo.second > iii.second)
      continue;
    ans = max(ans, min(min(jjj.second, ooo.second), iii.second));
  }

  printf("%d\\n", ans);
  return 0;
}
Problem p00494 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;

int main() {
  char str;
  int n[3] = {}, ans = 0, x;
  while (cin >> str) {
    if (str == 'J' && n[1] == 0 && n[2] == 0)
      n[0]++;
    else if (str == 'O' && n[1] < n[0] && n[2] == 0)
      n[1]++;
    else if (str == 'I' && n[2] < min(n[0], n[1])) {
      n[2]++;
      if (n[2] == min(n[0], n[1]))
        ans = max(ans, min(n[0], n[1]));
    } else {
      for (int i = 0; i < 3; i++)
        n[i] = 0;
      if (str == 'J')
        n[0] = 1;
    }
  }
  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
string str;
int serch(int n, int wo) {
  string word = "OI";
  for (int i = 0; i < n && i + wo + n < str.size(); i++) {
    if (str[i + wo] != word[0] || str[i + wo + n] != word[1])
      return 0;
    if (i + 1 == n)
      return n;
  }
  return 0;
}
int main() {
  cin >> str;
  int ans = 0;
  for (int i = 0; i < str.size(); i++) {
    if (str[i] == 'J') {
      for (int j = 1;; j++) {
        if (str[i + j] != 'J') {
          for (int k = 0, kawa; ans < j - k; k++) {
            if ((kawa = serch(j - k, i + j)) != 0) {
              ans = max(kawa, ans);
              j += kawa * 2 - 1;
              break;
            }
          }
          i += j;
          break;
        }
      }
    }
  }
  cout << ans << endl;
}
Problem p00494 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define r(i, a, n) for (i = 0; i < n;)
using namespace std;
int main() {
  string s;
  int i, j, k, l, sum = 0, a, b, c;
  cin >> s;
  r(i, 0, s.size()) {
    a = b = c = 0;
    for (; s[i] == 'J'; a++)
      i++;
    for (; s[i] == 'O'; b++)
      i++;
    for (; s[i] == 'I'; c++)
      i++;
    if (b <= a && b <= c)
      sum = max(b, sum);
  }
  cout << sum << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define r(i, a, n) for (i = a; i < n; i++)
using namespace std;
int main() {
  string s;
  int i, j, k, l, sum = 0, a, b, c;
  cin >> s;
  r(i, 0, s.size()) {
    a = b = c = 0;
    if (s[i] == 'J') {
      r(j, i, s.size()) {
        if (s[j] == 'J')
          a++;
        else
          break;
      }
      if (s[j] == 'O')
        r(k, j, s.size()) {
          if (s[k] == 'O')
            b++;
          else
            break;
        }
      if (s[k] == 'I')
        r(l, k, s.size()) {
          if (s[l] == 'I')
            c++;
          else
            break;
        }
    }
    if (b <= a && b <= c)
      sum = max(b, sum);
  }
  cout << sum << endl;
}
Problem p00494 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
int main() {
  string s;
  cin >> s;
  int ans = 0, n = s.size();
  for (int i = 0; i < n; i++) {
    int j = -1;
    if (s[i] == 'O') {
      j = i;
      while (s[j + 1] == 'O')
        j++;
    }
    int l = j - i + 1;
    if (i - l >= 0 && j + l < n) {
      bool flag = true;
      for (int k = i - l; k < i; k++)
        if (s[k] != 'J')
          flag = false;
      for (int k = j + 1; k <= j + l; k++)
        if (s[k] != 'I')
          flag = false;
      if (flag)
        ans = max(ans, l);
    }
    if (j != -1)
      i = j + l;
  }
  cout << ans << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
#include <string>
using namespace std;
int main() {
  string s;
  cin >> s;
  int ans = 0;
  for (int i = 1; i <= s.size() / 3; i++) {
    string str;
    for (int j = 0; j < i; j++)
      str += 'J';
    for (int j = 0; j < i; j++)
      str += 'O';
    for (int j = 0; j < i; j++)
      str += 'I';
    if (s == str || s.find(str) < s.size())
      ans = i;
  }
  cout << ans << endl;
}
Problem p00494 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>
#include <iostream>
#include <string>
using namespace std;
#define rep(i, n) REP(i, 0, n)
#define REP(i, x, n) for (int i = x; i < n; i++)

int main() {
  const string joi = "JOI";

  string str;
  cin >> str;

  int ans = 0;
  for (int i = 0; i < str.size();) {
    int cs[3] = {0};

    rep(j, 3) for (; i < str.size() && str[i] == joi[j]; i++) cs[j]++;

    if (cs[0] >= cs[1] && cs[2] >= cs[1])
      ans = max(ans, cs[1]);
  }

  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
#define rep(i, n) REP(i, 0, n)
#define REP(i, x, n) for (int i = x; i < n; i++)

int table[1000001][3];

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

  return -1;
}

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

  rep(i, str.size()) rep(j, 3) table[i + 1][j] =
      table[i][j] + (j == ord(str[i]));

  int ans = 0;
  rep(head, str.size()) for (int size = 3; head + size <= str.size();
                             size += 3) {

    if (table[head + size / 3][0] - table[head][0] != size / 3)
      continue;
    if (table[head + size / 3 * 2][1] - table[head + size / 3][1] != size / 3)
      continue;
    if (table[head + size][2] - table[head + size / 3 * 2][2] != size / 3)
      continue;

    ans = max(ans, size / 3);
  }

  cout << ans << endl;
}