Problem:
JJOOII (JJOOII)
JOI (日本情報オリンピック) の本選に向けてプログラミングの練習をしていたあなたは，今年度のJOI の予選の問題には数値を扱う問題ばかりが出題され，文字列を扱う問題がなかったことに気がついた．そこであなたは，こっそり文字列の問題に強くなってライバルたちに差をつけることにした．
JOI の過去問を眺めていると，J, O, I の3 種類の文字からなる文字列に慣れておく必要がありそうなことがわかった．そこで，そのような文字列について考えよう．あなたは「与えられた文字列がJOI という部分文字列をもつかどうかを答えよ」という問題を思いついたものの，これはすぐに解けてしまった．もっとレベルの高い問題を解きたいあなたは，以下のような問題を作った．
文字列
t
が文字列
s
の
部分文字列
であるとは，
t
の先頭および末尾に何文字か(0 文字でもよい) を付け足すと
s
になることである．たとえば，JJOOII はOJJOOIIOJOI の部分文字列である．一方，JOI はJOOI の部分文字列ではない．
また，0 以上の整数
k
に対し，
レベル
k
のJOI 列
とは，
k
個の文字J，
k
個の文字O，
k
個の文字I をこの順に並べた文字列のことであるとする．たとえば，JJOOII はレベル2 のJOI 列である．与えられた文字列の部分文字列であるJOI 列のうち，レベルが最大のものを求めたい．
課題
J, O, I の3 種類の文字からなる長さ
N
の文字列
S
が与えられたとき，レベル
k
のJOI 列が
S
の部分文字列であるような最大の
k
の値を求めるプログラムを作成せよ．
制限
1 ≤
N
≤ 1000000 (= 10
6
)
S
の長さ
入力
標準入力から以下のデータを読み込め．
1 行目にはJ, O, I の3 種類の文字からなる文字列
S
が書かれている．
出力
標準出力に，レベル
k
のJOI 列が
S
の部分文字列であるような最大の
k
の値を表す整数を1 行で出力せよ．
採点基準
採点用データのうち，配点の20%分については，
N
≤ 100 を満たす．
入出力例
入力例 1
OJJOOIIOJOI
出力例 1
2
OJJOOIIOJOI はレベル2 のJOI 列であるJJOOII を部分文字列として含んでおり，レベル3 以上のJOI列は部分文字列として含まない．
入力例 2
IJJIIJJJ
出力例 2
0
入力例 3
JOIJOIJOIJOIJOI
出力例 3
1
入力例 4
OOJJJJJJJOOOOIIIII
出力例 4
4
問題文と自動審判に使われるデータは、
情報オリンピック日本委員会
が作成し公開している問題文と採点用テストデータです。

Submitted solutions:

code_snippet: #include <iostream>
#include <vector>
#include <algorithm>
#include<utility>
#include<set>
#include<deque>
#include <unordered_map>
#include<string>
#include<cmath>

using namespace std;
int mm = -100000;
const int MOD = 10000;
int main() {
	string S;
	cin >> S;
	vector<pair<char, int>>L;

	int j = 1;
	char c = S[0];
	int p = 0;
	for (int i = 0; i < S.size();) {
		j = i;
		while (j<S.size() and S[i]==S[j]) {
			j++;

		}
		L.push_back({ S[i],j - i });

		i = j;


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

	cout << ans << endl;
}
accuracy: 1
time_ms: 10
memory_kb: 15088
final_rank: 12

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

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

    // Run-Length Encoding
    vector<pair<char,int>> rle;
    for (int i = 0; i < N; ) {
        int j = i;
        while (j < N && S[j] == S[i]) j++;
        rle.push_back({S[i], j - i});
        i = j;
    }

    int ans = 0;
    for (int i = 0; i + 2 < (int)rle.size(); i++) {
        if (rle[i].first == 'J' && rle[i+1].first == 'O' && rle[i+2].first == 'I') {
            int a = rle[i].second;
            int b = rle[i+1].second;
            int c = rle[i+2].second;
            if (a >= b && b <= c) {
                ans = max(ans, b);
            }
        }
    }
    cout << ans << endl;
}
accuracy: 1
time_ms: 10
memory_kb: 14596
final_rank: 11

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)

int main(){
    string s;
    cin >> s;
    int flag=0;
    int n=s.size();

    int i=0;
    int a=0,b=0,c=0;
    int res=0;
    while(i<n){
        if(flag==0){
            if(s[i]=='J'){
                a++;
            }
            else if(s[i]=='O'){
                flag=1;
                b++;
            }
            else{
                a=0;
                b=0;
                c=0;
                flag=0;
            }
        }
        else if (flag==1){
            if(s[i]=='J'){
                a=1;
                b=0;
                c=0;
                flag=0;
            }
            else if(s[i]=='O'){
                b++;
            }
            else{
                flag=2;
                c++;
            }
        }
        else{
            if(s[i]=='J'){
                if(a>=b and c>=b) res=max(res,b);
                flag=0;
                a=1;
                b=0;
                c=0;
            }
            else if(s[i]=='O'){
                if(a>=b and c>=b) res=max(res,b);
                flag=0;
                a=0;
                b=0;
                c=0;
            }
            else{
                c++;
            }
        }
        i++;
    }
    if(a>=b and c>=b) res=max(res,b);
    cout << res << endl;
    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 5148
final_rank: 1

code_snippet: #include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <map>

using namespace std;

// 使用される文字列と対応するインデックス
const static char c[] = {'J', 'O', 'I'};
int N; // 文字列の長さ
string S; // 入力文字列
int T[3][1000001]; // 各文字の連続した長さを格納する配列
vector<pair<int, int>> v; // 'J' の連続長さとその開始位置のペアを格納

// 指定された位置 p から始まる部分文字列がレベルkかどうかを確認
bool isLevelK(int k, int p) {
    // pからk, pから2kの位置が配列Sの範囲内にあるか確認
    if (p + k >= N || p + 2 * k >= N) return false;
    
    // 'J', 'O', 'I' がそれぞれ連続しているか確認
    return T[1][p + k] == k && T[2][p + 2 * k] >= k;
}

int main() {
    // 入力の受け取り
    cin >> S;
    N = S.size();

    // 'J', 'O', 'I' の各文字について、後ろから順に連続長さを計算
    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;
                
                // 'J' の場合、連続長さとその位置を保存
                if (p == 0) 
                    v.push_back(make_pair(-T[p][i], i));
            } else {
                T[p][i] = 0;
            }
        }
    }

    // 'J' の連続長さが大きい順にソート
    sort(v.begin(), v.end());

    int ans = 0; // 最終的な解答
    // 各 'J' の連続部分についてレベルkか確認
    for (int i = 0; i < v.size(); ++i) {
        if (isLevelK(-v[i].first, v[i].second)) {
            ans = -v[i].first; // レベルkが見つかればそれが解答
            break;
        }
    }

    // 結果を出力
    cout << ans << endl;
    return 0;
}
accuracy: 1
time_ms: 40
memory_kb: 24556
final_rank: 20

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

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

  int a = 0, b = 0, c = 0, d = 0, maxi = 0;
  int leng = x.size();

  for (a = 0; a < leng; a++) {
    if (x[a] == 'J') {
      b = 0;
      while (x[a] == 'J' && a < leng) {
        b++;
        a++;
      }
      c = 0;
      while (x[a] == 'O' && a < leng) {
        c++;
        a++;
      }
      d = 0;
      while (x[a] == 'I' && a < leng) {
        d++;
        a++;
      }
      a--; // adjust a back to the last valid position
      if (b >= c && c <= d) {
        maxi = max(maxi, c);
      }
    }
  }

  cout << maxi << "\n";
  return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 3976
final_rank: 5

code_snippet: #include <algorithm>
#include <cstdio>
#include <cstring>
#include <string>
using namespace std;
char x[1000005];
int counts[3] = {0, 0, 0};  // counts for J, O, I respectively
int a = 0, b = 0, c = 0, d = 0, e, f, i, j, k, maxi = 0, leng = 0;
int main() {
  // read the string and find its length
  while(scanf("%c", &x[leng]), x[leng] != '\n'){
    ++leng;
  }
  x[leng] = '\0';
  while (a < leng) {
    if (x[a] != 'J') {
      a++;
    } else {
      for (int i = 0; i < 3; ++i) {
        while (x[a] == "JOI"[i]) {
          ++counts[i];
          ++a;
        }
      }
      if (counts[0] >= counts[1] && counts[1] <= counts[2]) {
        maxi = max(maxi, counts[1]);
      }
      for (int i = 0; i < 3; ++i) {
        counts[i] = 0;
      }
    }
  }
  printf("%d\n", maxi);
  scanf("%d", &a);
  return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 3704
final_rank: 4

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

int main() {
  string x;
  cin >> x;
  int a = 0, b = 0, c = 0, d = 0, maxi = 0;
  for (int a = 0; a < x.length();) {
    if (x[a] != 'J') {
      a++;
    } else {
      for (; x[a] == 'J'; a++) {
        b++;
      }
      for (; x[a] == 'O'; a++) {
        c++;
      }
      for (; x[a] == 'I'; a++) {
        d++;
      }
      if (b >= c && c <= d) {
        maxi = max(maxi, c);
      }
      b = c = d = 0;
    }
  }
  cout << maxi << "\n";
  return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 3976
final_rank: 5

code_snippet: #include <bits/stdc++.h>
#include <random>
using namespace std;
//#include <atcoder/all>
//using namespace atcoder;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vll>;
using vvvll = vector<vvll>;
using vvvvll = vector<vvvll>;
using vvvvvll = vector<vvvvll>;
using vvvvvvll = vector<vvvvvll>;
using vb = vector<bool>;
using vvb = vector<vb>;
using vvvb = vector<vvb>;
using vvvvb = vector<vvvb>;
using vd = vector<double>;
using vvd = vector<vd>;
using vvvd = vector<vvd>;
using vvvvd = vector<vvvd>;
using vvvvvd = vector<vvvvd>;
#define all(A) A.begin(),A.end()
#define ALL(A) A.begin(),A.end()
#define rep(i, n) for (ll i = 0; i < (ll) (n); i++)
using pqr = priority_queue<pair<ll, ll>, vector<pair<ll, ll>>, greater<pair<ll, ll>>>;
#define tN(t) (t==1?XN:YN)
#define tS(t) (t==1?XS:YS)
#define tA(t) (t==1?XA:YA)

template<class T>
bool chmax(T& p, T q, bool C = 1) {
    if (C == 0 && p == q) {
        return 1;
    }
    if (p < q) {
        p = q;
        return 1;
    }
    else {
        return 0;
    }
}

template<class T>
bool chmin(T& p, T q, bool C = 1) {
    if (C == 0 && p == q) {
        return 1;
    }
    if (p > q) {
        p = q;
        return 1;
    }
    else {
        return 0;
    }
}

ll gcd(ll(a), ll(b)) {
    if (b == 0)return a;
    ll c = a;
    while (a % b != 0) {
        c = a % b;
        a = b;
        b = c;
    }
    return b;
}


vector<ll> fact, factinv, inv;
ll mod = 998244353;
void prenCkModp(ll n) {
    fact.resize(n + 5);
    factinv.resize(n + 5);
    inv.resize(n + 5);
    fact.at(0) = fact.at(1) = 1;
    factinv.at(0) = factinv.at(1) = 1;
    inv.at(1) = 1;
    for (ll i = 2; i < n + 5; i++) {
        fact.at(i) = (fact.at(i - 1) * i) % mod;
        inv.at(i) = mod - (inv.at(mod % i) * (mod / i)) % mod;
        factinv.at(i) = (factinv.at(i - 1) * inv.at(i)) % mod;
    }

}
ll nCk(ll n, ll k) {
    if (n < k) return 0;
    return fact.at(n) * (factinv.at(k) * factinv.at(n - k) % mod) % mod;
}
ll modPow(long long a, long long n, long long p = mod) {
    if (n == 0) return 1; // 0乗にも対応する場合
    if (n == 1) return a % p;
    if (n % 2 == 1) return (a * modPow(a, n - 1, p)) % p;
    long long t = modPow(a, n / 2, p);
    return (t * t) % p;
}



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

    string S;
    cin >> S;

    ll N = S.size();
    vll J(N, 0), I(N, 0);

    ll j = 0;
    rep(i, N) {
        if (S[i] == 'J') {
            j++;
        }
        else {
            j = 0;
        }
        J[i] = j;
    }

    reverse(all(S));
    j = 0;
    rep(i, N) {
        if (S[i] == 'I') {
            j++;
        }
        else {
            j = 0;
        }
        I[i] = j;
    }
    reverse(all(I));
    reverse(all(S));
    ll L = -1;

    ll an = 0;
    rep(i, N) {
        if (S[i] == 'O') {

            if (L == -1)L = i;

            ll d = i - L + 1;
            if (L>0&&i<N-1&&J[L-1] >= d && I[i+1] >= d)chmax(an, d);
        }
        else L = -1;

    }
    cout << an << endl;


}
accuracy: 1
time_ms: 10
memory_kb: 19788
final_rank: 16

code_snippet: #include <bits/stdc++.h>
using namespace std;
const long long MOD1 = 1000000007;
const long long MOD2 = 998244353;
typedef long long ll;
typedef pair<long long, long long> P;
const long long INF = 1e18;
const ll MOD = INF;
template <typename T>
void input_arr(vector<T>& A, ll N) {
	for (ll i = 0; i < N; i++) {
		cin >> A[i];
	}
}
template <typename T, typename Q>
void input_arr(vector<pair<T, Q>>& A, ll N) {
	for (ll i = 0; i < N; i++) {
		cin >> A[i].first >> A[i].second;
	}
}
template <typename T>
void input_arr(vector<vector<T>>& A, ll H, ll W) {
	for (ll i = 0; i < H; i++) {
		for (ll j = 0; j < W; j++) {
			cin >> A[i][j];
		}
	}
}
template <typename T>
struct CumlativeSum2D {
    vector<vector<T>> data;

    explicit CumlativeSum2D(ll H, ll W) : data(H + 1, vector<ll>(W + 1, 0)) {}
    void add(ll x, ll y, T z) {
        x++; y++;
        if (x >= data.size() || y >= data[0].size()) return;
        data[x][y] += z;
    }
    void build() {
        for (int i = 1; i < data.size(); i++) {
            for (int j = 1; j < data[i].size(); j++) {
                data[i][j] += data[i][j-1] + data[i-1][j] - data[i-1][j-1];
            }
        }
    }
    T sum(ll sx, ll sy, ll gx, ll gy) {
        return (data[gx][gy] - data[sx][gy] - data[gx][sy] + data[sx][sy]);
    }
};
int main() {
	string S; cin >> S;
	vector<pair<char, ll>>A;
	A.push_back({S[0], 1});
	for (ll i = 1; i < S.size(); i++) {
		if (A.back().first == S[i]) A.back().second++;
		else A.emplace_back(S[i], 1LL);
	}
	ll ans = 0;
	for (ll i = 1; i < A.size() - 1; i++) {
		if (A[i - 1].first == 'J' && A[i].first == 'O' && A[i + 1].first == 'I') {
			if (A[i - 1].second >= A[i].second && A[i].second <= A[i + 1].second) {
				ans = max(ans, A[i].second);
			}
		}
	}
	cout << ans << endl;
}
accuracy: 1
time_ms: 10
memory_kb: 23220
final_rank: 18

code_snippet: #include <bits/stdc++.h>
using namespace std;
using lint = long long;
#define rep(i, n) for(int i = 0; i < int(n); i++)

int main(){
    string s;
    cin >> s;
    vector<int> lens;
    vector<char> ch{'X'};
    for(char c: s){
        if(ch.back() == c){
            lens.back()++;
        }else{
            lens.push_back(1);
            ch.push_back(c);
        }
    }
    ch.erase(ch.begin());
    
    int sz = int(lens.size());
    int ans = 0;
    rep(i, sz - 2){
        if(ch[i] == 'J' && ch[i + 1] == 'O' && ch[i + 2] == 'I'){
            int len = lens[i + 1];
            if(lens[i] < len || lens[i + 2] < len){
                continue;
            }
            
            ans = max(ans, len);
        }
    }
    
    cout << ans << endl;
}
accuracy: 1
time_ms: 10
memory_kb: 11352
final_rank: 7

code_snippet: #include<iostream>
#include<vector>
#include<map>

using namespace std;

int main()
{
	string s;
	cin >> s;
	int sLen = s.length();
	vector<pair<int, int>> seg;
	int index = 0;
	int len = 1;
	map<char,int> MAX_LEN;
	MAX_LEN['J'] = 0;
	MAX_LEN['O'] = 0;
	MAX_LEN['I'] = 0;
	for (int i = 1; i < sLen; i++)
	{
		if (s[i] != s[i - 1])
		{
			seg.push_back(make_pair(index, len));
			MAX_LEN[s[i - 1]] = max(MAX_LEN[s[i - 1]],len);
			index = i;
			len = 1;
		}
		else
			len++;
	}
	seg.push_back(make_pair(index,len));
	MAX_LEN[s.back()] = max(MAX_LEN[s.back()], len);
	int counter = 1;
	int ans = 0;
	int MAX = min(MAX_LEN['J'], min(MAX_LEN['O'], MAX_LEN['I']));
	while ( counter <= MAX)
	{
		for (int i = 0; i < seg.size() - 2; i++)
		{
			if (s[seg[i].first] == 'J' && seg[i].second >= counter && s[seg[i + 1].first] == 'O' &&
				seg[i + 1].second == counter && s[seg[i + 2].first] == 'I' && seg[i + 2].second >= counter)
			{
				ans = counter;
				break;
			}
		}
		counter++;
	}
	cout << ans << endl;

	//system("pause");
}
accuracy: 1
time_ms: 30
memory_kb: 12512
final_rank: 19

code_snippet: #define _USE_MATH_DEFINES
#include<iostream>
#include<string>
#include<vector>
#include<stack>
#include<queue>
#include<set>
#include<map>
#include<list>
#include<algorithm>
#include<numeric>
#include<cmath>
#include<iomanip>
#include<sstream>
#include<regex>
#include<random>
using namespace std;
#define int long long
#define uint unsigned long long
const int mod=1e9+7;
const int Mod=998244353;
const int inf=mod*mod;
const double eps=1e-9;
signed main(){
	string s;
	cin>>s;
	vector<pair<char,int>>p;
	for(int i=0;i<s.size();i++){
		if(i==0||p.back().first!=s[i])
			p.emplace_back(s[i],1);
		else
			p.back().second++;
	}
	int ans=0;
	for(int i=1;i<p.size()-1;i++)
		if(p[i-1].first=='J'&&p[i].first=='O'&&p[i+1].first=='I'&&p[i-1].second>=p[i].second&&p[i].second<=p[i+1].second)
			ans=max(ans,p[i].second);
	cout<<ans<<endl;
}
accuracy: 1
time_ms: 10
memory_kb: 22792
final_rank: 17

code_snippet: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, N) for (int i = 0; i < N; i++)

int main()
{
    string S;
    cin >> S;
    vector<char> vc;
    vector<int> vi;
    char prev = S[0];
    S += "X";
    int cnt = 1;
    for (int i = 1; i < S.size(); i++)
    {
        if (S[i] == prev)
        {
            cnt++;
        }
        else
        {
            vc.push_back(prev);
            vi.push_back(cnt);
            cnt = 1;
            prev = S[i];
        }
    }
/*
  rep(i, vc.size())
  {
    cout << vc[i] << " " << vi[i] << endl;
  }
*/

    int maxv = 0;
    for(int i=0;i< (int)vi.size() - 2;i++)
    {
      //  rep(j,3) cout << vc[i+j] << " x ";
    //    cout << " (vc[i] == 'J' && vc[i + 1] == 'O' && vc[i + 2] == 'i') " << (vc[i] == 'J' && vc[i + 1] == 'O' && vc[i + 2] == 'I') << i <<  endl;
        if (vc[i] == 'J' && vc[i + 1] == 'O' && vc[i + 2] == 'I' && vi[i+1] <= vi[i] && vi[i+1] <= vi[i+2])
        {
            int level = min(vi[i], min(vi[i + 1], vi[i + 2]));
            maxv = max(maxv, level);
        }
    }
    cout << maxv << endl;
    
}
accuracy: 1
time_ms: 20
memory_kb: 9648
final_rank: 14

code_snippet: #pragma region Macros
#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define rep(i, n) for(ll i = 0; i < n; ++i)
#define drep(i,n) for(ll i = n-1;i >= 0;i--)
#define pii pair<int, int>
#define pll pair<ll, ll>
#define pb push_back
#define eb emplace_back
#define vi vector<int>
#define vll vector<ll>
#define vpi vector<pii>
#define vpll vector<pll>
#define endl '\n'
#define overload2(_1, _2, name, ...) name
#define vec(type, name, ...) vector<type> name(__VA_ARGS__)
#define VEC(type, name, size)\
    vector<type> name(size);\
    IN(name)
#define vv(type, name, h, ...) vector<vector<type>> name(h, vector<type>(__VA_ARGS__))
#define VV(type, name, h, w)\
    vector<vector<type>> name(h, vector<type>(w));\
    IN(name)
#define vvv(type, name, h, w, ...) vector<vector<vector<type>>> name(h, vector<vector<type>>(w, vector<type>(__VA_ARGS__)))
#define vvvv(type, name, a, b, c, ...)\
    vector<vector<vector<vector<type>>>> name(a, vector<vector<vector<type>>>(b, vector<vector<type>>(c, vector<type>(__VA_ARGS__))))
#define fi first
#define se second
#define all(c) begin(c), end(c)
#define lb(c, x) distance((c).begin(), lower_bound(all(c), (x)))
#define ub(c, x) distance((c).begin(), upper_bound(all(c), (x)))
using namespace std;
template <class T> using pq = priority_queue<T>;
template <class T> using pqg = priority_queue<T, vector<T>, greater<T>>;
#define si(c) (int)(c).size()
#define INT(...)\
    int __VA_ARGS__;\
    IN(__VA_ARGS__)
#define LL(...)\
    ll __VA_ARGS__;\
    IN(__VA_ARGS__)
#define STR(...)\
    string __VA_ARGS__;\
    IN(__VA_ARGS__)
#define CHR(...)\
    char __VA_ARGS__;\
    IN(__VA_ARGS__)
#define DBL(...)\
    double __VA_ARGS__;\
    IN(__VA_ARGS__)
int scan() { return getchar(); }
void scan(int &a) { cin >> a; }
void scan(long long &a) { cin >> a; }
void scan(char &a) { cin >> a; }
void scan(double &a) { cin >> a; }
void scan(string &a) { cin >> a; }
template <class T, class S> void scan(pair<T, S> &p) { scan(p.first), scan(p.second); }
template <class T> void scan(vector<T> &);
template <class T> void scan(vector<T> &a) {
    for(auto &i : a) scan(i);
}
template <class T> void scan(T &a) { cin >> a; }
void IN() {}
template <class Head, class... Tail> void IN(Head &head, Tail &... tail) {
    scan(head);
    IN(tail...);
}
template <class T, class S> inline bool chmax(T &a, S b) {
    if(a < b) {
        a = b;
        return 1;
    }
    return 0;
}
template <class T, class S> inline bool chmin(T &a, S b) {
    if(a > b) {
        a = b;
        return 1;
    }
    return 0;
}
vi iota(int n) {
    vi a(n);
    iota(all(a), 0);
    return a;
}
template <typename T> vi iota(vector<T> &a, bool greater = false) {
    vi res(a.size());
    iota(all(res), 0);
    sort(all(res), [&](int i, int j) {
        if(greater) return a[i] > a[j];
        return a[i] < a[j];
    });
    return res;
}
#define UNIQUE(x) sort(all(x)), x.erase(unique(all(x)), x.end())
template <class T> T POW(T x, int n) {
    T res = 1;
    for(; n; n >>= 1, x *= x)
        if(n & 1) res *= x;
    return res;
}
vector<pll> factor(ll x) {
    vector<pll> ans;
    for(ll i = 2; i * i <= x; i++)
        if(x % i == 0) {
            ans.push_back({i, 1});
            while((x /= i) % i == 0) ans.back().second++;
        }
    if(x != 1) ans.push_back({x, 1});
    return ans;
}
template <class T> vector<T> divisor(T x) {
    vector<T> ans;
    for(T i = 1; i * i <= x; i++)
        if(x % i == 0) {
            ans.pb(i);
            if(i * i != x) ans.pb(x / i);
        }
    return ans;
}
template <typename T> void zip(vector<T> &x) {
    vector<T> y = x;
    sort(all(y));
    for(int i = 0; i < x.size(); ++i) { x[i] = lb(y, x[i]); }
}
int popcount(ll x) { return __builtin_popcountll(x); }
int in() {
    int x;
    cin >> x;
    return x;
}
ll lin() {
    unsigned long long x;
    cin >> x;
    return x;
}
#define i128 __int128_t
#define ull unsigned long long int
template <typename T> static constexpr T inf = numeric_limits<T>::max() / 2;
struct Setup_io {
    Setup_io() {
        ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
        cout << fixed << setprecision(15);
    }
} setup_io;
template <typename A, typename B>
ostream& operator <<(ostream& out, const pair<A, B>& a) {
out << "(" << a.first << "," << a.second << ")";
return out;
}
template <typename T, size_t N>
ostream& operator <<(ostream& out, const array<T, N>& a) {
out << "["; bool first = true;
for (auto& v : a) { out << (first ? "" : ", "); out << v; first = 0;} out << "]";
return out;
}
template <typename T>
ostream& operator <<(ostream& out, const vector<T>& a) {
out << "["; bool first = true;
for (auto& v : a) { out << (first ? "" : ", "); out << v; first = 0;} out << "]";
return out;
}
template <typename T, class Cmp>
ostream& operator <<(ostream& out, const set<T, Cmp>& a) {
out << "{"; bool first = true;
for (auto& v : a) { out << (first ? "" :", "); out << v; first = 0;} out << "}";
return out;
}
template <typename U, typename T, class Cmp>
ostream& operator <<(ostream& out, const map<U, T, Cmp>& a) {
out << "{"; bool first = true;
for (auto& p : a) { out << (first ? "" : ", "); out << p.first << ":" << p.second; first = 0;} out << "}";
return out;
}
#define LOCAL
#ifdef LOCAL
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
#else
#define trace(...) 42
#endif
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
cerr << name << ": " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
const char* comma = strchr(names + 1, ',');
cerr.write(names, comma - names) << ": " << arg1 << " |";
__f(comma + 1, args...);
}
#pragma endregion
//#include<atcoder/all>
//using namespace atcoder;
int main(){
    
    STR(s);
    int n = s.size();    
    vector<int> J(n+1);
    rep(i,n){
        if(s[i] == 'J'){
            J[i] = 1;
            if(i)J[i] += J[i-1];
        }
    }
    vector<int> sufI(n+1);
    drep(i,n){
        if(s[i] == 'I'){
            sufI[i] = 1;
            sufI[i] += sufI[i+1];
        }
    }
    vector<int> O(n+1);
    rep(i,n){
        if(s[i] == 'O'){
            O[i] = 1;
            if(i)O[i] += O[i-1];
        }
    }
    int ret = 0;
    rep(i,n){
        if(s[i] != 'O')continue;
        if(i-O[i] >= 0 && J[i-O[i]] >= O[i] && sufI[i+1] >= O[i]){
            chmax(ret,O[i]);
        }
    }
    cout << ret << endl;
    
    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 15860
final_rank: 13

code_snippet: #include <bits/stdc++.h>
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define drep(i,j,n) for(int i=0;i<(int)(n-1);i++)for(int j=i+1;j<(int)(n);j++)
#define trep(i,j,k,n) for(int i=0;i<(int)(n-2);i++)for(int j=i+1;j<(int)(n-1);j++)for(int k=j+1;k<(int)(n);k++)
#define codefor int test;scanf("%d",&test);while(test--)
#define INT(...) int __VA_ARGS__;in(__VA_ARGS__)
#define LL(...) ll __VA_ARGS__;in(__VA_ARGS__)
#define yes(ans) if(ans)printf("yes\n");else printf("no\n")
#define Yes(ans) if(ans)printf("Yes\n");else printf("No\n")
#define YES(ans) if(ans)printf("YES\n");else printf("NO\n")
#define vector2d(type,name,h,...) vector<vector<type>>name(h,vector<type>(__VA_ARGS__))
#define vector3d(type,name,h,w,...) vector<vector<vector<type>>>name(h,vector<vector<type>>(w,vector<type>(__VA_ARGS__)))
#define umap unordered_map
#define uset unordered_set
using namespace std;
using ll = long long;
const int MOD=1000000007;
const int MOD2=998244353;
const int INF=1<<30;
const ll INF2=(ll)1<<60;
//入力系
void scan(int& a){scanf("%d",&a);}
void scan(long long& a){scanf("%lld",&a);}
template<class T,class L>void scan(pair<T, L>& p){scan(p.first);scan(p.second);}
template<class T> void scan(T& a){cin>>a;}
template<class T> void scan(vector<T>& vec){for(auto&& it:vec)scan(it);}
void in(){}
template <class Head, class... Tail> void in(Head& head, Tail&... tail){scan(head);in(tail...);}
//出力系
void print(const int& a){printf("%d",a);}
void print(const long long& a){printf("%lld",a);}
void print(const double& a){printf("%.15lf",a);}
template<class T,class L>void print(const pair<T, L>& p){print(p.first);putchar(' ');print(p.second);}
template<class T> void print(const T& a){cout<<a;}
template<class T> void print(const vector<T>& vec){if(vec.empty())return;print(vec[0]);for(auto it=vec.begin();++it!= vec.end();){putchar(' ');print(*it);}}
void out(){putchar('\n');}
template<class T> void out(const T& t){print(t);putchar('\n');}
template <class Head, class... Tail> void out(const Head& head,const Tail&... tail){print(head);putchar(' ');out(tail...);}
//デバッグ系
template<class T> void dprint(const T& a){cerr<<a;}
template<class T> void dprint(const vector<T>& vec){if(vec.empty())return;cerr<<vec[0];for(auto it=vec.begin();++it!= vec.end();){cerr<<" "<<*it;}}
void debug(){cerr<<endl;}
template<class T> void debug(const T& t){dprint(t);cerr<<endl;}
template <class Head, class... Tail> void debug(const Head& head, const Tail&... tail){dprint(head);cerr<<" ";debug(tail...);}
ll intpow(ll a, ll b){ ll ans = 1; while(b){ if(b & 1) ans *= a; a *= a; b /= 2; } return ans; }
ll modpow(ll a, ll b, ll p){ ll ans = 1; while(b){ if(b & 1) (ans *= a) %= p; (a *= a) %= p; b /= 2; } return ans; }
ll modinv(ll a, ll m) {ll b = m, u = 1, v = 0;while (b) {ll t = a / b;a -= t * b; swap(a, b);u -= t * v; swap(u, v);}u %= m;if (u < 0) u += m;return u;}
ll updivide(ll a,ll b){if(a%b==0) return a/b;else return (a/b)+1;}
template<class T> void chmax(T &a,const T b){if(b>a)a=b;}
template<class T> void chmin(T &a,const T b){if(b<a)a=b;}

int main(){
    string s;
    cin>>s;
    vector<pair<char,int>> num;
    int cnt=0,i=0,ans=0;
    while(i<s.size()){
        cnt=1;
        while(i+cnt<s.size()&&s[i+cnt]==s[i])cnt++;
        num.emplace_back(s[i],cnt);
        i+=cnt;
    }
    rep(i,num.size()-2){
        if(num[i].first=='J'&&num[i+1].first=='O'&&num[i+2].first=='I'){
            if(num[i+1].second>num[i].second||num[i+1].second>num[i+2].second)continue;
            chmax(ans,min({num[i].second,num[i+1].second,num[i+2].second}));
        }
    }
    out(ans);
}
accuracy: 1
time_ms: 20
memory_kb: 12656
final_rank: 15

code_snippet: #include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < (n); i++)
#define bit(n,k) ((n>>k)&1)
using namespace std;
typedef long long ll;

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

    string s; cin >> s;
    vector<pair<char, int>> v;
    rep(i,s.size()){
        int c = 1;
        while(i + 1 < s.size() && s[i] == s[i + 1]){ i++; c++; }
        v.emplace_back(s[i], c);
    }
    int ans = 0;
    rep(i,v.size())if(i > 0 && i < v.size() - 1){
        if(v[i].first == 'O'){
            int x = v[i].second;
            if(v[i-1].second >= x && v[i+1].second >= x){
                if(v[i-1].first == 'J' && v[i+1].first == 'I'){
                    ans = max(ans, x);
                }
            }
        }
    }
    cout << ans << endl;
}
accuracy: 1
time_ms: 10
memory_kb: 13080
final_rank: 9

code_snippet: #pragma region Macros
#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define rep(i, n) for(ll i = 0; i < n; ++i)
#define drep(i,n) for(ll i = n-1;i >= 0;i--)
#define pii pair<int, int>
#define pll pair<ll, ll>
#define pb push_back
#define eb emplace_back
#define vi vector<int>
#define vll vector<ll>
#define vpi vector<pii>
#define vpll vector<pll>
#define endl '\n'
#define overload2(_1, _2, name, ...) name
#define vec(type, name, ...) vector<type> name(__VA_ARGS__)
#define VEC(type, name, size)\
    vector<type> name(size);\
    IN(name)
#define vv(type, name, h, ...) vector<vector<type>> name(h, vector<type>(__VA_ARGS__))
#define VV(type, name, h, w)\
    vector<vector<type>> name(h, vector<type>(w));\
    IN(name)
#define vvv(type, name, h, w, ...) vector<vector<vector<type>>> name(h, vector<vector<type>>(w, vector<type>(__VA_ARGS__)))
#define vvvv(type, name, a, b, c, ...)\
    vector<vector<vector<vector<type>>>> name(a, vector<vector<vector<type>>>(b, vector<vector<type>>(c, vector<type>(__VA_ARGS__))))
#define fi first
#define se second
#define all(c) begin(c), end(c)
#define lb(c, x) distance((c).begin(), lower_bound(all(c), (x)))
#define ub(c, x) distance((c).begin(), upper_bound(all(c), (x)))
using namespace std;
template <class T> using pq = priority_queue<T>;
template <class T> using pqg = priority_queue<T, vector<T>, greater<T>>;
#define si(c) (int)(c).size()
#define INT(...)\
    int __VA_ARGS__;\
    IN(__VA_ARGS__)
#define LL(...)\
    ll __VA_ARGS__;\
    IN(__VA_ARGS__)
#define STR(...)\
    string __VA_ARGS__;\
    IN(__VA_ARGS__)
#define CHR(...)\
    char __VA_ARGS__;\
    IN(__VA_ARGS__)
#define DBL(...)\
    double __VA_ARGS__;\
    IN(__VA_ARGS__)
int scan() { return getchar(); }
void scan(int &a) { cin >> a; }
void scan(long long &a) { cin >> a; }
void scan(char &a) { cin >> a; }
void scan(double &a) { cin >> a; }
void scan(string &a) { cin >> a; }
template <class T, class S> void scan(pair<T, S> &p) { scan(p.first), scan(p.second); }
template <class T> void scan(vector<T> &);
template <class T> void scan(vector<T> &a) {
    for(auto &i : a) scan(i);
}
template <class T> void scan(T &a) { cin >> a; }
void IN() {}
template <class Head, class... Tail> void IN(Head &head, Tail &... tail) {
    scan(head);
    IN(tail...);
}
template <class T, class S> inline bool chmax(T &a, S b) {
    if(a < b) {
        a = b;
        return 1;
    }
    return 0;
}
template <class T, class S> inline bool chmin(T &a, S b) {
    if(a > b) {
        a = b;
        return 1;
    }
    return 0;
}
vi iota(int n) {
    vi a(n);
    iota(all(a), 0);
    return a;
}
template <typename T> vi iota(vector<T> &a, bool greater = false) {
    vi res(a.size());
    iota(all(res), 0);
    sort(all(res), [&](int i, int j) {
        if(greater) return a[i] > a[j];
        return a[i] < a[j];
    });
    return res;
}
#define UNIQUE(x) sort(all(x)), x.erase(unique(all(x)), x.end())
template <class T> T POW(T x, int n) {
    T res = 1;
    for(; n; n >>= 1, x *= x)
        if(n & 1) res *= x;
    return res;
}
vector<pll> factor(ll x) {
    vector<pll> ans;
    for(ll i = 2; i * i <= x; i++)
        if(x % i == 0) {
            ans.push_back({i, 1});
            while((x /= i) % i == 0) ans.back().second++;
        }
    if(x != 1) ans.push_back({x, 1});
    return ans;
}
template <class T> vector<T> divisor(T x) {
    vector<T> ans;
    for(T i = 1; i * i <= x; i++)
        if(x % i == 0) {
            ans.pb(i);
            if(i * i != x) ans.pb(x / i);
        }
    return ans;
}
template <typename T> void zip(vector<T> &x) {
    vector<T> y = x;
    sort(all(y));
    for(int i = 0; i < x.size(); ++i) { x[i] = lb(y, x[i]); }
}
int popcount(ll x) { return __builtin_popcountll(x); }
int in() {
    int x;
    cin >> x;
    return x;
}
ll lin() {
    unsigned long long x;
    cin >> x;
    return x;
}
template <typename T> struct edge {
    int from, to;
    T cost;
    int id;
    edge(int to, T cost) : from(-1), to(to), cost(cost) {}
    edge(int from, int to, T cost) : from(from), to(to), cost(cost) {}
    edge(int from, int to, T cost, int id) : from(from), to(to), cost(cost), id(id) {}
    edge &operator=(const int &x) {
        to = x;
        return *this;
    }
    operator int() const { return to; }
};
template <typename T> using Edges = vector<edge<T>>;
using Tree = vector<vector<int>>;
using Graph = vector<vector<int>>;
template <class T> using Wgraph = vector<vector<edge<T>>>;
Graph getG(int n, int m = -1, bool directed = false, int margin = 1) {
    Tree res(n);
    if(m == -1) m = n - 1;
    while(m--) {
        int a, b;
        cin >> a >> b;
        a -= margin, b -= margin;
        res[a].emplace_back(b);
        if(!directed) res[b].emplace_back(a);
    }
    return move(res);
}
template <class T> Wgraph<T> getWg(int n, int m = -1, bool directed = false, int margin = 1) {
    Wgraph<T> res(n);
    if(m == -1) m = n - 1;
    while(m--) {
        int a, b;
        T c;
        cin >> a >> b >> c;
        a -= margin, b -= margin;
        res[a].emplace_back(b, c);
        if(!directed) res[b].emplace_back(a, c);
    }
    return move(res);
}
#define i128 __int128_t
#define ull unsigned long long int
template <typename T> static constexpr T inf = numeric_limits<T>::max() / 2;
struct Setup_io {
    Setup_io() {
        ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
        cout << fixed << setprecision(15);
    }
} setup_io;
template <typename A, typename B>
ostream& operator <<(ostream& out, const pair<A, B>& a) {
out << "(" << a.first << "," << a.second << ")";
return out;
}
template <typename T, size_t N>
ostream& operator <<(ostream& out, const array<T, N>& a) {
out << "["; bool first = true;
for (auto& v : a) { out << (first ? "" : ", "); out << v; first = 0;} out << "]";
return out;
}
template <typename T>
ostream& operator <<(ostream& out, const vector<T>& a) {
out << "["; bool first = true;
for (auto& v : a) { out << (first ? "" : ", "); out << v; first = 0;} out << "]";
return out;
}
template <typename T, class Cmp>
ostream& operator <<(ostream& out, const set<T, Cmp>& a) {
out << "{"; bool first = true;
for (auto& v : a) { out << (first ? "" :", "); out << v; first = 0;} out << "}";
return out;
}
template <typename U, typename T, class Cmp>
ostream& operator <<(ostream& out, const map<U, T, Cmp>& a) {
out << "{"; bool first = true;
for (auto& p : a) { out << (first ? "" : ", "); out << p.first << ":" << p.second; first = 0;} out << "}";
return out;
}
#define LOCAL
#ifdef LOCAL
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
#else
#define trace(...) 42
#endif
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
cerr << name << ": " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
const char* comma = strchr(names + 1, ',');
cerr.write(names, comma - names) << ": " << arg1 << " |";
__f(comma + 1, args...);
}
#pragma endregion
//#include<atcoder/all>
//using namespace atcoder;
int main(){
    STR(s);
    int n = s.size();
    vector<pii> res;
    for(int i = 0;i < n;){
        if(s[i] == 'O'){
            int from = i;
            while(i < n && s[i] == 'O')i++;
            int to = i;
            res.eb(from,to);
        }
        else{
            while(i < n && s[i] != 'O')i++;
        }
    }
    // trace(res);
    sort(all(res),[&](const pii &a,const pii &b){return (a.se - a.fi) > (b.se - b.fi);});
    auto check = [&](int l,int r,char c){
        if(l < 0)return false;
        if(r > n)return false;
        for(int i = l;i < r;i++){
            if(s[i] != c)return false;
        }
        return true;
    };
    for(auto [l,r]:res){
        int len = r-l;
        if(check(l-len,l,'J') && check(r,r+len,'I')){
            cout << len << endl;
            return 0;
        }
    }
    cout << 0 << endl;
    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 10416
final_rank: 3

code_snippet: // clang-format off
#include <bits/stdc++.h>
//#pragma GCC optimize("Ofast")
//#pragma GCC target("avx")
#define int long long
using namespace std;
#define stoi stoll
#define fi first
#define se second
#define rep(i, n) for(int i=0, i##_len=(n); i<i##_len; i++)
#define rep2(i, a, b) for (int i = (int)(a), i##_len=(b); i < i##_len; i++)
#define rep3(i, a, b) for (int i = (int)(a), i##_len=(b); i >= i##_len; i--)
#define FOR(i, a) for (auto &i: a)
#define ALL(obj) begin(obj), end(obj)
#define _max(x) *max_element(ALL(x))
#define _min(x) *min_element(ALL(x))
#define _sum(x) accumulate(ALL(x), 0LL)

const int MOD   = 1000000007;
// const int MOD    = 998244353;
const int INF    = (int)(1e13 + 7);
const double EPS = 1e-8;
const double PI  = 3.14159265358979;
template <class T> using V = vector<T>;
template <class T> using VV = vector<vector<T>>;
template <class T> using VVV = vector<vector<vector<T>>>;
template <class T, class S>  using P = pair<T, S>;
template<class T> bool chmax(T &a, const T &b) {if (a < b) {a = b;return true;}return false;}
template<class T> bool chmin(T &a, const T &b) {if (b < a) {a = b;return true;}return false;}
int _ceil(int a, int b) { return (a >= 0 ? (a + (b - 1)) / b : (a - (b - 1)) / b); }
int _mod(int a) {return a >= 0 ? a % MOD : a - (MOD * _ceil(a, MOD));}
int _pow(int a, int b) {int res = 1;for (a %= MOD; b; a = a * a % MOD, b >>= 1)if (b & 1) res = res * a % MOD;return res;}
struct mint {long long x;mint(long long x = 0) : x((x % MOD + MOD) % MOD) {}mint operator-() const { return mint(-x); }mint &operator+=(const mint a) {if ((x += a.x) >= MOD) x -= MOD;return *this;}mint &operator-=(const mint a) {if ((x += MOD - a.x) >= MOD) x -= MOD;return *this;}mint &operator*=(const mint a) { (x *= a.x) %= MOD;return *this; }mint operator+(const mint a) const { return mint(*this) += a; }mint operator-(const mint a) const { return mint(*this) -= a; } mint operator*(const mint a) const { return mint(*this) *= a; }mint pow(long long t) const {if (!t) return 1;mint a = pow(t >> 1);a *= a;if (t & 1) a *= *this;return a;}mint inv() const { return pow(MOD - 2); }mint &operator/=(const mint a) { return *this *= a.inv(); }mint operator/(const mint a) const { return mint(*this) /= a; }};istream &operator>>(istream &is, mint &a) { return is >> a.x; }ostream &operator<<(ostream &os, const mint &a) { return os << a.x; }
// clang-format on

int check(string &S, int l, int r) {
  int num = r - l + 1;
  if (l - num < 0) return -1;
  if (S.size() < l + num * 2) return -1;
  if (S.substr(l - num, num * 3) == string(num, 'J') + string(num, 'O') + string(num, 'I')) {
    return num;
  }
  return -1;
}

signed main() {
  cin.tie(nullptr);
  cout.tie(nullptr);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(15);
  string S;
  cin >> S;
  S += '#';
  int ans = 0;
  rep(i, S.size()) {
    if (S[i] == 'O') {
      int now = i;
      while (S[now + 1] == 'O') now++;
      chmax(ans, check(S, i, now));
      i = now;
    }
  }
  cout << ans << endl;
}
accuracy: 1
time_ms: 10
memory_kb: 6788
final_rank: 2

code_snippet: #include <iostream>
#include <vector>
#include <algorithm>
#include <utility>
using namespace std;

int main(){
    string s;
    cin >> s;
    vector<pair<char,int>> a;
    for(char c: s){
        if(a.empty() or a.back().first!=c){
            a.push_back({c, 1});
        }else{
            a.back().second++;
        }
    }
    int ans = 0;
    for(int i=0; i<(int)a.size()-2; i++){
        if(a[i].first=='J' and a[i+1].first=='O' and a[i+2].first=='I'){
            if(a[i].second>=a[i+1].second and a[i+2].second>=a[i+1].second){
                ans = max(ans, a[i+1].second);
            }
        }
    }
    cout << ans << endl;
    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 14516
final_rank: 10

code_snippet: #include <bits/stdc++.h>
using namespace std;
typedef long long int ll;

int main(){
	cin.tie(nullptr);
	ios::sync_with_stdio(false);
	string s; cin >> s;
	int n=s.size();
	vector<pair<char,int>> v;
	for(int i=0;i<n;){
		char c=s[i];
		int cnt=0;
		while(i<n and s[i]==c){
			cnt++; i++;
		}
		v.push_back(make_pair(c,cnt));
	}
	int res=0;
	for(int i=0;i+2<v.size();i++){
		if(v[i].first=='J' and v[i+1].first=='O' and  v[i+2].first=='I'){
			if(v[i+1].second<=v[i].second and v[i+1].second<=v[i+2].second){
				res=max(res,v[i+1].second);
			}
		}
	}
	cout << res << endl;
}
accuracy: 1
time_ms: 10
memory_kb: 13076
final_rank: 8