alexjercan/bugnet huggingface dataset:
Problem p00494 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
typedef double db;

#define fr first
#define sc second
#define pb push_back
#define rep(i, x) for (ll i = 0; i < x; i++)
#define rep1(i, x) for (ll i = 1; i <= x; i++)
#define rrep(i, x) for (ll i = x - 1; i >= 0; i--)
#define rrep1(i, x) for (ll i = x; i > 0; i--)

ll ans = 0;
ll n;
string s;
ll cnt = 0;
P p[1000001];
ll sz;
int main() {
  cin >> s;
  sz = 0;
  n = s.length();
  rep(i, n) {
    if (i == 0) {
      if (s[0] == 'J')
        p[sz] = P(1, 0);
      else if (s[0] == 'O')
        p[sz] = P(1, 1);
      else
        p[sz] = P(1, 2);
      sz++;
    } else {
      if (s[i] != s[i - 1]) {
        if (s[i] == 'J')
          p[sz] = P(1, 0);
        else if (s[i] == 'O')
          p[sz] = P(1, 1);
        else
          p[sz] = P(1, 2);
        sz++;
      } else {
        p[sz - 1].fr++;
      }
    }
  }
  rep(i, sz - 2) {
    if (p[i].sc == 0 && p[i + 1].sc == 1 && p[i + 2].sc == 2 &&
        p[i].fr >= p[i + 1].fr && p[i + 1].fr <= p[i + 2].fr) {
      ans = max(ans, p[i + 1].fr);
    }
  }
  cout << ans << endl;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
typedef double db;

#define fr first
#define sc second
#define pb push_back
#define rep(i, x) for (ll i = 0; i < x; i++)
#define rep1(i, x) for (ll i = 1; i <= x; i++)
#define rrep(i, x) for (ll i = x - 1; i >= 0; i--)
#define rrep1(i, x) for (ll i = x; i > 0; i--)

ll ans = 0;
ll n;
string s;
ll jud = -1;
ll cnt = 0;
vector<P> p;
int main() {
  cin >> s;
  n = s.length();
  rep(i, n) {
    if (i == 0) {
      if (s[0] == 'J')
        p.pb(P(1, 0));
      else if (s[0] == 'O')
        p.pb(P(1, 1));
      else
        p.pb(P(1, 2));
    } else {
      if (s[i] != s[i - 1]) {
        if (s[i] == 'J')
          p.pb(P(1, 0));
        else if (s[i] == 'O')
          p.pb(P(1, 1));
        else
          p.pb(P(1, 2));
      } else {
        p[p.size() - 1].fr++;
      }
    }
  }
  rep(i, p.size() - 2) {
    if (p[i].sc == 0 && p[i + 1].sc == 1 && p[i + 2].sc == 2 &&
        p[i].fr >= p[i + 1].fr && p[i + 1].fr <= p[i + 2].fr)
      ans = max(ans, min(min(p[i].fr, p[i + 1].fr), p[i + 2].fr));
  }
  cout << ans << endl;
}
Problem p00494 (C++) — buggy submission, verdict: Time Limit Exceeded

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

string ch;
int main() {
  // cout<<endl;
  cin >> ch;
  int ans = 0;
  int i = 0;
  int minl;
  ;
  while (i < ch.length()) {
    // cout<<"i == "<<i<<"max == "<<ans<<endl;
    int j = i;
    int cnt_j = 0, cnt_o = 0, cnt_i = 0;
    while (ch[j] == 'J') {
      cnt_j++;
      j++;
    }
    if (cnt_j == 0) {
      i++;
      continue;
    }

    while (ch[j] == 'O') {
      cnt_o++;
      j++;
    }
    if (cnt_o > cnt_j) {
      i += cnt_j + cnt_o;
      continue;
    }
    while (ch[j] == 'I') {
      cnt_i++;
      j++;
    }
    if (cnt_i < cnt_o) {
      i += cnt_o + cnt_j + cnt_i;
      continue;
    }
    minl = min(cnt_i, cnt_o);
    ans = max(minl, ans);
    i += cnt_j + cnt_o + cnt_i;
  }
  cout << ans << endl;
}

Error: TLE

Fixed submission:

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

string ch;
int main() {
  // cout<<endl;
  cin >> ch;
  int ans = 0;
  int i = 0;
  int minl;
  ;
  while (i < ch.length()) {
    // cout<<"i == "<<i<<"max == "<<ans<<endl;
    int j = i;
    int cnt_j = 0, cnt_o = 0, cnt_i = 0;
    while (ch[j] == 'J') {
      cnt_j++;
      j++;
    }
    if (cnt_j == 0) {
      i++;
      continue;
    }

    while (ch[j] == 'O') {
      cnt_o++;
      j++;
    }
    if (cnt_o > cnt_j) {
      i++;
      continue;
    }
    while (ch[j] == 'I') {
      cnt_i++;
      j++;
    }
    if (cnt_i < cnt_o) {
      i++;
      continue;
    }
    minl = min(cnt_i, cnt_o);
    ans = max(minl, ans);
    i += cnt_j + cnt_o + cnt_i;
  }
  cout << ans << endl;
}
Problem p00494 (C++) — buggy submission, verdict: Runtime Error

#include <cstdio>
#include <iostream>
#include <string>
using namespace std;
string S;
int X[1000005][3];
int z = 0;
int y = 0;
int main() {
  cin >> S;
  if (S[0] == 'J') {
    X[z][0]++;
    y = 0;
  }
  if (S[0] == 'O') {
    X[z][1]++;
    y = 1;
  }
  if (S[0] == 'I') {
    X[z][2]++;
    y = 2;
  }
  for (int i = 1; i < S.length(); i++) {
    if (S[i - 1] == S[i])
      X[z][y]++;
    else {
      z++;
      if (S[i] == 'J')
        y = 0;
      if (S[i] == 'O')
        y = 1;
      if (S[i] == 'I')
        y = 2;
      X[z][y]++;
    }
  }
  int ans = 0;
  for (int i = 0; i <= z; i++) {
    if (X[i][0] < X[i + 1][1])
      continue;
    if (X[i + 1][1] > X[i + 2][2])
      continue;
    ans = max(ans, X[i + 1][1]);
  }
  printf("%d\\n", ans);
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
typedef long long int ll;

int main() {
  int v[100000][3];
  string ss;
  cin >> ss;
  for (int i = 0; i < ss.length(); i++) {
    v[i][0] = v[i][1] = v[i][2] = 0;
  }
  int ans = 0;

  int x = 0;
  for (int i = 0; i < ss.length(); i++) {
    if (ss[i] == 'J')
      v[x][0]++;
    if (ss[i] == 'O')
      v[x][1]++;
    if (ss[i] == 'I')
      v[x][2]++;
    if ((i + 1) != ss.length() && ss[i] != ss[i + 1])
      x++;
  }

  for (int i = 0; i < ss.length(); i++) {
    if (v[i][0]) {
      if (v[i + 1][1] <= v[i][0] && v[i + 1][1] <= v[i + 2][2]) {
        ans = max(ans, v[i + 1][1]);
      }
    }
  }

  cout << ans << "\\n";
  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 cnt = 0;

  for (int i = 0; i < s.size(); ++i) {

    int temp = 0;
    bool judge = true;

    if (s[i] == 'J') {

      temp++;
      i++;

      while (1) {
        if (s[i] == 'J') {
          temp++;
          i++;
        }

        else if (s[i] == 'I')
          break;

        else if (s[i] == 'O') {

          int j = 0;
          while (1) {
            if (s[i] == 'O') {
              if (j >= temp) {
                judge = false;
                break;
              } else {
                i++;
                j++;
              }
            }

            else if (s[i] == 'J') {
              i--;
              judge = false;
              break;
            }

            else if (s[i] == 'I') {
              temp = j;

              for (int k = 0; k < temp; ++k) {
                if (s[i] != 'I') {
                  i--;
                  judge = false;
                  break;
                }
                if (k == temp - 1) {
                  if (cnt < temp)
                    cnt = temp;

                  judge = false;

                } else
                  i++;
              }

            } else
              break;

            if (judge == false)
              break;
          }

        } else
          break;

        if (judge == false)
          break;
      }
    }
  }

  cout << cnt << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

int main() {

  string s;

  cin >> s;

  int cnt = 0;

  for (int i = 0; i < s.size(); ++i) {

    int temp = 0;
    bool judge = true;

    if (s[i] == 'J') {

      temp++;
      i++;

      while (1) {
        if (s[i] == 'J') {
          temp++;
          i++;
        }

        else if (s[i] == 'I') {
          i++;
          break;
        }

        else if (s[i] == 'O') {
          int j = 0;
          while (1) {
            if (s[i] == 'O') {
              if (j > temp) {
                judge = false;
                break;
              } else {
                i++;
                j++;
              }
            }

            else if (s[i] == 'J') {
              i++;
              judge = false;
              break;
            } else if (s[i] == 'I') {
              temp = j;

              for (int k = 0; k < temp; ++k) {
                if (s[i] != 'I') {
                  judge = false;
                  break;
                }
                if (k == temp - 1) {
                  if (cnt < temp) {
                    cnt = temp;
                    judge = false;
                  }
                } else
                  i++;
              }

            } else
              break;

            if (judge == false)
              break;
          }

        } else
          break;

        if (judge == false)
          break;
      }
    }
  }

  cout << cnt << endl;
}
Problem p00494 (C++) — buggy submission, verdict: Time Limit Exceeded

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

vector<int> ve;
vector<char> vc;
char ary[1000010];
int main() {
  cin >> ary;
  int cnt, res, ves, sta;
  char nw = 'X';
  cnt = 0;
  sta = strlen(ary);
  for (int i = 0; i < sta; i++) {
    if (nw == ary[i]) {
      cnt++;
    } else {
      vc.push_back(nw);
      nw = ary[i];
      ve.push_back(cnt);
      cnt = 1;
    }
  }
  vc.push_back(nw);
  ve.push_back(cnt);
  ve.erase(ve.begin());
  vc.erase(vc.begin());
  res = 0;
  ves = ve.size();
  if (ves > 2)
    for (int i = 0; i < ves - 2; i++) {
      if (vc[i] == 'J' && vc[i + 1] == 'O' && vc[i + 2] == 'I' &&
          ve[i] >= ve[i + 1] && ve[i + 2] >= ve[i + 1]) {
        res = max(res, ve[i + 1]);
      }
    }
  else
    res = 0;
  cout << res << endl;
  return 0;
}

Error: TLE

Fixed submission:

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

vector<int> ve;
vector<char> vc;
char ary[1000010];
int main() {
  cin >> ary;
  int cnt, res;
  char nw = 'X';
  ve.reserve(1000010);
  vc.reserve(1000010);
  cnt = 0;
  for (int i = 0; i < strlen(ary); i++) {
    if (nw == ary[i]) {
      cnt++;
    } else {
      vc.push_back(nw);
      nw = ary[i];
      ve.push_back(cnt);
      cnt = 1;
    }
  }
  vc.push_back(nw);
  ve.push_back(cnt);
  ve.erase(ve.begin());
  vc.erase(vc.begin());
  res = 0;
  if (ve.size() > 2)
    for (int i = 0; i < ve.size() - 2; i++) {
      if (vc[i] == 'J' && vc[i + 1] == 'O' && vc[i + 2] == 'I' &&
          ve[i] >= ve[i + 1] && ve[i + 2] >= ve[i + 1]) {
        res = max(res, ve[i + 1]);
      }
    }
  else
    res = 0;
  cout << res << endl;
  return 0;
}
Problem p00494 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <vector>

using namespace std;

int main(int argc, char *argv[]) {
  string str;
  int ans = 0;
  int n1, n2, n3;
  cin >> str;

  for (int i = 0; i < str.length(); ++i) {
    n1 = n2 = n3 = 0;
    while (str[i] == 'J') {
      n1++;
      i++;
    }
    while (str[i] == 'O') {
      n2++;
      i++;
    }
    while (str[i] == 'I') {
      n3++;
      i++;
    }
    if (n2 <= n1 && n2 <= n3)
      ans = max(ans, n2);
    i--;
  }
  cout << ans << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <vector>

using namespace std;

int main(int argc, char *argv[]) {
  string str;
  string s = "JOIX";
  int ans = 0;
  cin >> str;

  for (int i = 0; i < str.length(); ++i) {
    if (str[i] == 'J') {
      int index = 0;
      int cnt[3] = {0};
      for (int j = i; j < str.length(); ++j) {
        // cout<<str[j];
        if (str[j] == s[index])
          cnt[index]++;
        else if (str[j] == s[index + 1]) {
          index++;
          cnt[index]++;
        } else
          break;
      }
      // cout<<endl;
      if (index == 2) {
        // cout<<index<<endl;
        // cout<<cnt[0]<<" "<<cnt[1]<<" "<<cnt[2]<<endl;
        if (cnt[1] <= cnt[0] && cnt[1] <= cnt[2])
          ans = max(ans, min({cnt[0], cnt[1], cnt[2]}));
      }
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p00494 (C++) — buggy submission, verdict: Time Limit Exceeded


#include <algorithm>
#include <stdio.h>
#include <string.h>
#include <utility>
#include <vector>

using namespace std;

char s[1234567];
vector<pair<char, int>> ss;

int main() {
  scanf("%s", s);
  int lastsame = 1;
  char thlett = s[0];
  int n = strlen(s);
  for (int i = 1; i < n; ++i) {
    if (thlett != s[i]) {
      ss.push_back(make_pair(thlett, lastsame));
      thlett = s[i];
      lastsame = 1;
    } else {
      ++lastsame;
    }
  }
  ss.push_back(make_pair(thlett, lastsame));
  if (ss.size() < 3)
    return 0 * printf("0\\n");
  int ans = 0;
  for (int j = 0; j < ss.size() - 2; ++j) {
    if (ss[j].first == 'J' && ss[j + 1].first == 'O' &&
        ss[j + 2].first == 'I') {
      if (ss[j + 1].second <= min(ss[j].second, ss[j + 2].second)) {
        ans = max(ans, ss[j + 1].second);
      }
    }
  }
  printf("%d\\n", ans);
  return 0;
}

Error: TLE

Fixed submission:


#include <algorithm>
#include <stdio.h>
#include <string.h>
#include <utility>
#include <vector>

using namespace std;

char s[1234567];
vector<pair<char, int>> ss;

int main() {
  scanf("%s", s);
  int lastsame = 1;
  char thlett = s[0];
  for (int i = 1; i < strlen(s); ++i) {
    if (thlett != s[i]) {
      ss.push_back(make_pair(thlett, lastsame));
      thlett = s[i];
      lastsame = 1;
    } else {
      ++lastsame;
    }
  }
  ss.push_back(make_pair(thlett, lastsame));
  if (ss.size() < 3)
    return 0 * printf("0\\n");
  int ans = 0;
  for (int j = 0; j < ss.size() - 2; ++j) {
    if (ss[j].first == 'J' && ss[j + 1].first == 'O' &&
        ss[j + 2].first == 'I') {
      if (ss[j + 1].second <= min(ss[j].second, ss[j + 2].second)) {
        ans = max(ans, ss[j + 1].second);
      }
    }
  }
  printf("%d\\n", ans);
  return 0;
}
Problem p00494 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
typedef long long ll;
typedef pair<char, int> P;

int main() {
  vector<P> v;
  string s;
  cin >> s;
  if (s.size() <= 2) {
    puts("0");
    return 0;
  }
  int cnt = 0;
  char back = s[0];
  for (char c : s) {
    if (c == back)
      cnt++;
    else {
      v.push_back(P(back, cnt));
      cnt = 1;
      back = c;
    }
  }
  v.push_back(P(back, cnt));
  int Max = 0;
  if (v.size() <= 2) {
    puts("0");
    return 0;
  }
  rep(i, v.size() - 2) {
    if (v[i].first == 'J' && v[i + 1].first == 'O' && v[i + 2].first == 'I' &&
        v[i + 1].second <= v[i].second && v[i + 1].second <= v[i + 2].second) {
      Max = max(Max, v[i + 1].second);
    }
  }
  cout << Max << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
typedef long long ll;
typedef pair<char, int> P;

int main() {
  vector<P> v;
  string s;
  cin >> s;
  if (s.size() <= 2) {
    puts("0");
    return 0;
  }
  int cnt = 0;
  char back = s[0];
  for (char c : s) {
    if (c == back)
      cnt++;
    else {
      v.push_back(P(back, cnt));
      cnt = 1;
      back = c;
    }
  }
  v.push_back(P(back, cnt));
  int Max = 0;
  rep(i, v.size() - 2) {
    if (v[i].first == 'J' && v[i + 1].first == 'O' && v[i + 2].first == 'I' &&
        v[i + 1].second <= v[i].second && v[i + 1].second <= v[i + 2].second) {
      Max = max(Max, v[i + 1].second);
    }
  }
  cout << Max << endl;
  return 0;
}
Problem p00494 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int k = 0, sum = 0, a[1000000];
string s;

int main() {
  cin >> s;

  for (int i = 0; i < s.size(); i++) {
    sum++;
    if (s[i] != s[i + 1]) {
      for (int j = i - sum + 1; j <= i; j++) {
        a[j] = sum;
        sum--;
      }
      sum = 0;
    }
  }

  for (int i = 0; i < s.size(); i++) {
    if (a[i] > k && s[i] == 'J') {
      if (a[i + a[i]] == a[i] && s[i + a[i]] == 'O' &&
          a[i + a[i] * 2] >= a[i] && s[i + a[i] * 2] == 'I') {
        k = a[i];
      }
    }
  }

  cout << k << endl;

  return 0;
}

Error: TLE

Fixed submission:

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

int k, loop = 0, ans = 0;
char joi[4] = "JOI";
string s;

void solve(int x) {
  for (int i = 1; i <= s.size() - x; i++) {
    if (s[x + i] != joi[loop]) {
      if (s[x + i] == joi[loop + 1] || loop == 2) {
        if (loop >= 1 && k != i || k > i) {
          return;
        }

        k = i;

        loop++;
        if (loop == 3) {
          ans = max(ans, k);
          return;
        }

        solve(x + i);
      }
      return;

    } else if (loop == 2 && i == k) {
      ans = max(ans, k);
      return;
    }
  }
}

int main() {
  cin >> s;

  for (int i = 0; i < s.size(); i++) {
    if (s[i] == 'J') {
      solve(i);
      k = 0;
      loop = 0;
    }
  }

  cout << ans << endl;

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

#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
char c[1000000];
int main() {

  int o, flag, ma = 0;
  scanf("%s", c);

  for (int i = 1; i < strlen(c) - 1; i++) {
    if (c[i] == 'O') {
      o = 1;
      for (int j = i + 1; j < strlen(c) - 1; j++) {
        if (c[j] == 'O')
          o++;
        else
          break;
      }
      flag = 0;
      for (int j = 1; j <= o; j++) {
        if (!(c[i - j] == 'J' && c[i + o - 1 + j] == 'I')) {
          flag++;
          break;
        }
      }
      if (flag == 0) {
        ma = max(ma, o);
      }
      i += o - 1;
    }
  }

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

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
char c[1000000];
int main() {

  int o, flag, ma = 0;
  scanf("%s", c);

  for (int i = 1; i < strlen(c) - 1; i++) {
    if (c[i] == 'O') {
      o = 1;
      for (int j = i + 1; j < strlen(c) - 1; j++) {
        if (c[j] == 'O')
          o++;
        else
          break;
      }
      flag = 0;
      for (int j = 1; j <= o; j++) {
        if (!(c[i - j] == 'J' && c[i + o - 1 + j] == 'I')) {
          flag++;
          break;
        }
      }
      if (flag == 0) {
        ma = max(ma, o);
      }
    }
  }

  printf("%d\\n", ma);
  return (0);
}
Problem p00494 (C++) — buggy submission, verdict: Runtime Error

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

typedef struct {
  char c;
  int num;
} C_NUM;

char str[2000000];
C_NUM c_num[2000000];

int main() {
  scanf("%s", str);
  int len = strlen(str);

  int j = 0;
  for (int i = 0; i < len; i++) {
    if (j == 0 || str[i] != c_num[j - 1].c) {
      c_num[j].c = str[i];
      c_num[j].num = 1;
      j++;
    } else if (str[i] == c_num[j - 1].c) {
      c_num[j - 1].num++;
    }
  }

  int max_k = 0;
  for (int i = 0; i < j; i++) {
    //		printf("%c %d\\n", c_num[i].c, c_num[i].num);
    if (c_num[i].c == 'O') {
      if ((c_num[i - 1].c == 'J' && c_num[i - 1].num >= c_num[i].num) &&
          (c_num[i + 1].c == 'I' && c_num[i + 1].num >= c_num[i].num) &&
          (max_k < c_num[i].num)) {
        max_k = c_num[i].num;
      }
    }
  }

  printf("%d\\n", max_k);
}

Error: 0

Fixed submission:

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

typedef struct {
  char c;
  int num;
} C_NUM;

int main() {
  char str[2048];
  C_NUM c_num[2048];

  scanf("%s", str);
  int len = strlen(str);

  int j = 0;
  for (int i = 0; i < len; i++) {
    if (j == 0 || str[i] != c_num[j - 1].c) {
      c_num[j].c = str[i];
      c_num[j].num = 1;
      j++;
    } else if (str[i] == c_num[j - 1].c) {
      c_num[j - 1].num++;
    }
  }

  int max_k = 0;
  for (int i = 0; i < j; i++) {
    //		printf("%c %d\\n", c_num[i].c, c_num[i].num);
    if (c_num[i].c == 'O') {
      if ((c_num[i - 1].c == 'J' && c_num[i - 1].num >= c_num[i].num) &&
          (c_num[i + 1].c == 'I' && c_num[i + 1].num >= c_num[i].num) &&
          (max_k < c_num[i].num)) {
        max_k = c_num[i].num;
      }
    }
  }

  printf("%d\\n", max_k);
}
Problem p00494 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
struct char_cont {
  char ch;
  int num;
};

string S;

int main() {
  vector<char_cont> va;
  cin >> S;
  for (int i = 0; i < S.length(); i++) {
    if (i == 0) {
      va.push_back((char_cont){S[i], 1});
    } else {
      if (va.back().ch == S[i]) {
        va.back().num++;
      } else {
        va.push_back((char_cont){S[i], 1});
      }
    }
  }
  int ans = 0;
  for (int i = 0; i < va.size() - 2; i++) {
    if (i + 2 > va.size())
      break;
    if (va[i].ch == 'J' && va[i + 1].ch == 'O' && va[i + 2].ch == 'I') {
      if (va[i].num < va[i + 1].num || va[i + 1].num > va[i + 2].num)
        continue;
      ans = max(ans, va[i + 1].num);
    }
  }
  cout << ans << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  string s;
  string s1 = "";
  cin >> s;
  for (int i = 0; i < s.size(); i++) {
    int cnt = 1;
    while (s[i + 1] == s[i]) {
      i++;
      cnt++;
    }
    s1 += s[i];
    s1 += ('0' + cnt);
  }
  int ans = 0;
  for (int i = 2; i < s1.size() - 3; i += 2) {
    if (s1[i] == 'O' && s1[i - 2] == 'J' && s1[i + 2] == 'I' &&
        s1[i + 1] <= s1[i + 3] && s1[i + 1] <= s1[i - 1]) {
      ans = max(ans, s1[i + 1] - '0');
    }
  }
  cout << ans << endl;
  return 0;
}