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 (= 106)   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

問題文と自動審判に使われるデータは、情報オリンピック日本委員会が作成し公開している問題文と採点用テストデータです。



Sample input:
OJJOOIIOJOI


Sample output:
2

Submission status: Wrong Answer

Solution code:
s=input()
i=0
maxlv=0
while i<len(s):
    lbuf=0
    if s[i]=='J':
        j=1
        while i+j<len(s) and s[i+j]=="J":j+=1
        lbuf=j
        i=i+j
        j=0
        while i+j<len(s) and s[i+j]=="O":j+=1
        lbuf=min((lbuf,j))
        if lbuf==0:continue
        i=i+j
        j=0
        while i+j<len(s) and s[i+j]=="I":j+=1
        lbuf=min((lbuf,j))
        if lbuf==0:continue
        elif lbuf>maxlv:
            maxlv=lbuf
            i=i+j
    else:i+=1
print(maxlv)

Submission status: Wrong Answer

Solution code:
s=input()
i=0
maxlv=0
while i<len(s):
    lbuf=0
    if s[i]=='J':
        j=1
        while i+j<len(s) and s[i+j]=="J":j+=1
        lbuf=j
        i=i+j
        j=0
        while i+j<len(s) and j<=lbuf and s[i+j]=="O":j+=1
        lbuf=min((lbuf,j))
        if lbuf==0:continue
        i=i+j
        j=0
        while i+j<len(s) and j<=lbuf and s[i+j]=="I":j+=1
        lbuf=min((lbuf,j))
        if lbuf==0:continue
        elif lbuf>maxlv:
            maxlv=lbuf
            i=i+j
    else:i+=1
print(maxlv)

Submission status: Wrong Answer

Solution code:
s=input()
i=0
maxlv=0
while i<len(s):
    lbuf=0
    if s[i]=='J':
        j=1
        while i+j<len(s) and s[i+j]=="J":j+=1
        lbuf=j
        i=i+j
        j=0
        while i+j<len(s) and j<lbuf and s[i+j]=="O":j+=1
        lbuf=min((lbuf,j))
        if lbuf==0:continue
        i=i+j
        j=0
        while i+j<len(s) and j<lbuf and s[i+j]=="I":j+=1
        lbuf=min((lbuf,j))
        if lbuf==0:continue
        elif lbuf>maxlv:
            maxlv=lbuf
            i=i+j
    else:i+=1
print(maxlv)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict, Counter
from itertools import product, groupby, count, permutations, combinations
from math import pi, sqrt
from collections import deque
from bisect import bisect, bisect_left, bisect_right
from string import ascii_lowercase
from functools import lru_cache
import sys
sys.setrecursionlimit(10000)
INF = float("inf")
YES, Yes, yes, NO, No, no = "YES", "Yes", "yes", "NO", "No", "no"


def main():
    S = input()
    t = "JOI"
    idx = 0
    ans = 0
    n = 10 ** 10
    for k, g in groupby(S):
        if k == t[idx]:
            n = min(n, len(list(g)))
            if k == "I":
                ans = max(ans, n)
                n = 10 ** 10
                idx = 0
            else:
                idx += 1
        else:
            idx = 0
            n = 10 ** 10

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
from collections import defaultdict, Counter
from itertools import product, groupby, count, permutations, combinations
from math import pi, sqrt
from collections import deque
from bisect import bisect, bisect_left, bisect_right
from string import ascii_lowercase
from functools import lru_cache
import sys
sys.setrecursionlimit(10000)
INF = float("inf")
YES, Yes, yes, NO, No, no = "YES", "Yes", "yes", "NO", "No", "no"


def main():
    S = input()
    t = "JOI"
    idx = 0
    ans = 0
    n = 10 ** 10
    for k, g in groupby(S):
        if k == t[idx]:
            n = min(n, len(list(g)))
            if k == "I":
                ans = max(ans, n)
                n = 10 ** 10
                idx = 0
            else:
                idx += 1
        else:
            if k == "J":
                idx = 1
                n = len(list(g))
            else:
                idx = 0
                n = 10 ** 10

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
S=raw_input()+"n"
A=[]
M="JOI"

i=0
m=M[0]
c=S[0]
n=0
for s in S:
    if c==s:
        n+=1
    else:
        if c==m:
            A.append(n)
            i+=1
        else:
            A.append(0)
            i=0
        m=M[i%3]
        c=s
        n=1
A+=[0,0,0]
ans= max([min(A[x:x+3]) for x in range(len(A)-2) ])
print ans

Submission status: Wrong Answer

Solution code:
S=raw_input()+"n"
A=[]
M="JOI"

i=0
m=M[0]
c=S[0]
n=0
for s in S:
    if c==s:
        n+=1
    else:
        if c==m:
            A.append(n)
            i+=1
        else:
            A.append(0)
            i=0
            if c==M[0]:
                A.append(n)
            i=1
        m=M[i%3]
        c=s
        n=1
A+=[0,0,0]
print A
ans= max([min(A[x:x+3]) for x in range(len(A)-2) ])
print ans

Submission status: Wrong Answer

Solution code:
S=raw_input()+"n"
A=[]
M="JOI"

i=0
m=M[0]
c=S[0]
n=0
for s in S:
    if c==s:
        n+=1
    else:
        if c==m:
            A.append(n)
            i+=1
        else:
            A.append(0)
            i=0
            if c==M[0]:
                A.append(n)
            i=1
        m=M[i%3]
        c=s
        n=1
A+=[0,0,0]
ans= max([min(A[x:x+3]) for x in range(len(A)-2) ])
print ans

Submission status: Wrong Answer

Solution code:
S=raw_input()
N=len(S)
l,r=0,N
m=0
while l<=r:
    k=(l+r)/2
    s="J"*k+"O"*k+"I"*k
    if S.count(s)==0:
        r=k-1
    else:
        m=max(k,m)
        l=k+1
print m

Submission status: Wrong Answer

Solution code:
import re

string = raw_input()
mutch = re.findall("J*O*I*", string)

print mutch
max = 0
for s in mutch:
	k = min([s.count(c) for c in ("J", "O", "I")])
	if k > max:
		max = k

print max

Submission status: Wrong Answer

Solution code:
import re

string = raw_input()
mutch = re.findall("J*O*I*", string)

max = 0
for s in mutch:
	k = min([s.count(c) for c in ("J", "O", "I")])
	if k > max:
		max = k

print max

Submission status: Wrong Answer

Solution code:
s = raw_input()
k = x = 0
try:
        while 1:
                cj = co = ci = 0
                while s[x] == "J":
                        cj += 1
                        x += 1
                while s[x] == "O":
                        co += 1
                        x += 1
                while s[x] == "I":
                        ci += 1
                        x += 1
                if co <= cj and co <= ci:
                        k = max(co,k)
except IndexError:
        k = max(co,k)
        print(k)

Submission status: Wrong Answer

Solution code:
s = raw_input()
j = 0
o = 0
i = 0
l = 0
for c in s:
	if c == 'J':
		j += 1
		o = 0
		i = 0
	if c == 'O':
		o += 1
		i = 0
	if c == 'I':
		i += 1
	if o == i and j >= o and l < o:
		l = o
print l

Submission status: Wrong Answer

Solution code:

JOI = "JOI"
t = 0
ans = 0
n = [0, 0, 0]
s = raw_input()
for i, c in enumerate(s):
  if c == JOI[t]:
    n[t] += 1
  elif t == 2:
    if n[0] >= n[1] <= n[2]:
      ans = max(ans, n[1])
    t = 0
  elif c == JOI[t + 1]:
    t += 1
    n[t] += 1
  else:
    t = 0
    n = [0, 0, 0]
if n[0] >= n[1] <= n[2]:
  ans = max(ans, n[1])
print ans

Submission status: Wrong Answer

Solution code:

JOI = "JOI"
t = 0
ans = 0
n = [0, 0, 0]
s = raw_input()
for i, c in enumerate(s):
  if c == JOI[t]:
    n[t] += 1
  elif t == 2:
    if n[0] >= n[1] <= n[2]:
      ans = max(ans, n[1])
    t = 0
    n = [0, 0, 0]
  elif c == JOI[t + 1]:
    t += 1
    n[t] += 1
  else:
    t = 0
    n = [0, 0, 0]
if n[0] >= n[1] <= n[2]:
  ans = max(ans, n[1])
print ans

Submission status: Wrong Answer

Solution code:

JOI = "JOI"
t = 0
ans = 0
n = [0, 0, 0]
s = raw_input()
for i, c in enumerate(s):
  if c == JOI[t]:
    n[t] += 1
  elif t == 2:
    if n[0] >= n[1] <= n[2]:
      ans = max(ans, n[1])
    t = 0
    n = [0, 0, 0]
    if c == JOI[t]:
      n[t] += 1
  elif c == JOI[t + 1]:
    t += 1
    n[t] += 1
  else:
    t = 0
    n = [0, 0, 0]
if n[0] >= n[1] <= n[2]:
  ans = max(ans, n[1])
print ans

Submission status: Accepted

Solution code:
s = raw_input()
d = []
cnt = 1
for i in range(len(s)-1):
    if s[i+1] == s[i]: cnt += 1
    else:
        d.append((s[i], cnt))
        cnt = 1
d.append((s[-1],cnt))
ans = 0
for i in range(len(d)-2):
    if d[i][0] == "J" and d[i+1][0] == "O" and d[i+2][0] == "I":
        if d[i+1][1] <= d[i][1] and d[i+1][1] <= d[i+2][1]:
            ans = max(ans, min(d[i][1], min(d[i+1][1], d[i+2][1])))
print ans

Submission status: Accepted

Solution code:
s = raw_input()
d = []
cnt = 1
for i in range(len(s)-1):
    if s[i+1] == s[i]: cnt += 1
    else:
        d.append((s[i], cnt))
        cnt = 1
d.append((s[-1],cnt))
ans = 0
for i in range(len(d)-2):
    if d[i][0] == "J" and d[i+1][0] == "O" and d[i+2][0] == "I":
        if d[i+1][1] <= d[i][1] and d[i+1][1] <= d[i+2][1]:
            ans = max(ans, d[i+1][1])
print ans

Submission status: Accepted

Solution code:
s=input()
i=0
maxlv=0
while i<len(s):
    lbuf=0
    if s[i]=='J':
        j=1
        while i+j<len(s) and s[i+j]=="J":j+=1
        lbuf=j
        i=i+j
        j=0
        while i+j<len(s) and j<lbuf and s[i+j]=="O":j+=1
        lbuf=min((lbuf,j))
        if lbuf==0:continue
        i=i+j
        j=0
        while i+j<len(s) and j<lbuf and s[i+j]=="I":j+=1
        i=i+j
        if j!=lbuf:continue
        elif lbuf>maxlv:
            maxlv=lbuf
    else:i+=1
print(maxlv)

Submission status: Accepted

Solution code:
from collections import defaultdict, Counter
from itertools import product, groupby, count, permutations, combinations
from math import pi, sqrt
from collections import deque
from bisect import bisect, bisect_left, bisect_right
from string import ascii_lowercase
from functools import lru_cache
import sys
sys.setrecursionlimit(10000)
INF = float("inf")
YES, Yes, yes, NO, No, no = "YES", "Yes", "yes", "NO", "No", "no"


def main():
    S = input()
    t = "JOI"
    idx = 0
    ans = 0
    num = [0, 0, 0]
    for k, g in groupby(S):
        if k == t[idx]:
            num[idx] = len(list(g))
            if k == "I":
                if num[1] <= num[0] and num[1] <= num[2]:
                    ans = max(ans, num[1])
                idx = 0
            else:
                idx += 1
        else:
            if k == "J":
                idx = 1
                num[0] = len(list(g))
            else:
                idx = 0

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from collections import defaultdict, Counter
from itertools import product, groupby, count, permutations, combinations
from math import pi, sqrt
from collections import deque
from bisect import bisect, bisect_left, bisect_right
from string import ascii_lowercase
from functools import lru_cache
import sys
sys.setrecursionlimit(10000)
INF = float("inf")
YES, Yes, yes, NO, No, no = "YES", "Yes", "yes", "NO", "No", "no"


def main():
    S = input()
    rlc = [(k, len(list(g))) for k, g in groupby(S)]
    ans = 0
    for i in range(1, len(rlc) - 1):
        a, b, c = rlc[i - 1], rlc[i], rlc[i + 1]
        if a[0] == "J" and b[0] == "O" and c[0] == "I":
            if b[1] <= a[1] and b[1] <= c[1]:
                ans = max(ans, b[1])
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
s = input()
length = len(s)
ind = 0
ans = 0
while ind < length:
  j_num = 0
  o_num = 0
  i_num = 0
  while ind < length and s[ind] != "J":
    ind += 1
  while ind < length and s[ind] == "J":
    j_num += 1
    ind += 1
  while ind < length and s[ind] == "O":
    o_num += 1
    ind += 1
  while ind < length and s[ind] == "I":
    i_num += 1
    ind += 1
  if o_num <= i_num and o_num <= j_num:
    ans = max(ans, o_num)
print(ans)

Submission status: Accepted

Solution code:
def main():
  s = input()
  end = len(s)
  ind = 0
  ans = 0
  while ind < end:
    j_num = 0
    o_num = 0
    i_num = 0
    while ind < end and s[ind] != "J":
      ind += 1
    while ind < end and s[ind] == "J":
      j_num += 1
      ind += 1
    while ind < end and s[ind] == "O":
      o_num += 1
      ind += 1
    while ind < end and s[ind] == "I":
      i_num += 1
      ind += 1
    if o_num <= i_num and o_num <= j_num:
      ans = max(ans, o_num)
  print(ans)
main()

Submission status: Accepted

Solution code:
def main():
  s = input()
  end = len(s)
  ind = 0
  ans = 0
  while ind < end:
    j_num = 0
    o_num = 0
    i_num = 0
    while ind < end and s[ind] != "J":
      ind += 1
    while ind < end and s[ind] == "J":
      j_num += 1
      ind += 1
    while ind < end and s[ind] == "O":
      o_num += 1
      ind += 1
    while ind < end and s[ind] == "I":
      i_num += 1
      ind += 1
    if o_num <= i_num and o_num <= j_num and o_num > ans:
      ans = o_num
  print(ans)
main()

Submission status: Accepted

Solution code:
# AOJ 0571 JJOOII
# Python3 2018.6.20 bal4u

import re

pat = "(J*)(O*)(I*)"
ans = 0
for s in re.findall(pat, input()):
	j = len(s[0])
	o = len(s[1])
	i = len(s[2])
	if j >= o and i >= o: ans = max(ans, o)
print(ans)

Submission status: Accepted

Solution code:
s = raw_input()
p = 0
ans = 0
while p < len(s):
    joi = 'JOI'
    cnt = [0] * 3
    for i in xrange(3):
        while p < len(s) and s[p] == joi[i]:
            cnt[i] += 1
            p += 1

    if cnt[0] >= cnt[1] and cnt[1] <= cnt[2]:
        ans = max(ans, cnt[1])

print ans

Submission status: Accepted

Solution code:
import re
s = raw_input()

ans = 0
for joi_str in re.findall('(J*)(O*)(I*)', s):
    j = len(joi_str[0])
    o = len(joi_str[1])
    i = len(joi_str[2])
    if j >= o and o <= i:
        ans = max(ans, o)

print ans

Submission status: Accepted

Solution code:
import re
s = raw_input()

joi_pattern = '(J*)(O*)(I*)'

ans = 0

for joi_group in re.findall(joi_pattern, s):
    j = len(joi_group[0])
    o = len(joi_group[1])
    i = len(joi_group[2])
    if j >= o and o <= i:
        ans = max(ans, o)

print ans

Submission status: Accepted

Solution code:
S=raw_input()
N=len(S)
k=0
i=0
while i<N:
    cntj,cnto,cnti=0,0,0
    while i<N and S[i]=="J":
        cntj+=1
        i+=1
    while i<N and S[i]=="O":
        cnto+=1
        i+=1
    while i<N and S[i]=="I":
        cnti+=1
        i+=1
    if cntj>=cnto and cnti>=cnto:
        k=max(cnto,k)
print k

Submission status: Accepted

Solution code:
S=raw_input()
N=len(S)
i,k=0,0
while i<N:
    L=[0]*3
    s="JOI"
    for j in range(len(s)):
        while i<N and S[i]==s[j]:
            L[j]+=1
            i+=1
    if L[0]>=L[1] and L[2]>=L[1]:
        k=max(L[1],k)
print k

Submission status: Accepted

Solution code:
s = raw_input()
k = x = 0
try:
        while 1:
                cj = co = ci = 0
                while s[x] == "J":
                        cj += 1
                        x += 1
                while s[x] == "O":
                        co += 1
                        x += 1
                while s[x] == "I":
                        ci += 1
                        x += 1
                if co <= cj and co <= ci:
                        k = max(co,k)
except IndexError:
        if co <= cj and co <= ci:
                k = max(co,k)
        print(k)

Submission status: Accepted

Solution code:
a=raw_input()
l=len(a)
i=ans=0
while i<l:
	lenj=leno=leni=0
	while i<l and a[i]=="J":
		lenj+=1
		i+=1
	while i<l and a[i]=="O":
		leno+=1
		i+=1
	while i<l and a[i]=="I":
		leni+=1
		i+=1
	if lenj>=leno and leni>=leno:
		ans=max(ans,leno)
print ans

Submission status: Runtime Error

Solution code:
JOI = raw_input()

JJOOII = ["J", "O", "I"]

max = 0
now = 1
while len(JJOOII) <= len(JOI):
	if "".join(JJOOII) in JOI : max = now
	JJOOII.insert(JJOOII.index("J") + 1, "J")
	JJOOII.insert(JJOOII.index("O") + 1, "O")
	JJOOII.insert(JJOOII.index("I") + 1, "I")
	now += 1
	
print max

Submission status: Runtime Error

Solution code:
JOI = raw_input()

JJOOII = "JOI"

max = 0
n = 1
while len(JJOOII) <= len(JOI):
	if JJOOII in JOI : max = n
	JJOOII = JJOOII[0:n] + "J" + JJOOII[n:n*2] + "O" + JJOOII[n*2:n*3] + "I"
	n += 1
	
print max

Submission status: Runtime Error

Solution code:
S=raw_input()+"n"
A=[]
M="JOI"

i=0
m=M[0]
c=S[0]
n=0
for s in S:
    if c==s:
        n+=1
    else:
        if c==m:
            A.append(n)
            i+=1
        else:
            A.append(0)
            i=0
        m=M[i%3]
        c=s
        n=1
ans= max([min(A[x:x+3]) for x in range(len(A)-2) ])
print ans

Submission status: Runtime Error

Solution code:
S=raw_input()
N=len(S)
for k in range(N/3,0,-1):
    s="J"*k+"O"*k+"I"*k
    if S.count(s)>0:
        print k
        break
else:
    print 0

Submission status: Runtime Error

Solution code:
import re

string = raw_input()
mutch = re.findall("J*O*I*", string)

max = 0
for s in mutch:
	if s.count("O") == min([s.count(c) for c in ("J", "O", "I")]):
		k = s.count("O")
		
	if k > max:
		max = k

print max

Submission status: Runtime Error

Solution code:
import re

string = raw_input()
match = re.findall("J+O+I+", string)

max = 0
for s in match:
	if s.count("O") == min([s.count(c) for c in ("J", "O", "I")]):
		k = s.count("O")
		
	if k > max:
		max = k

print max

Submission status: Runtime Error

Solution code:
import re

string = raw_input()
match = re.findall("J+O+I+", string)

max = 0
for s in match:
	k = 0
	if s.count("O") == min([s.count(c) for c in ("J", "O", "I")]):
		k = s.count("O")
		
	if k > max:
		max = k

print max

Submission status: Runtime Error

Solution code:
s = raw_input()
k = x = 0
while 1:
        x += 1
        if "J"*x+"O"*x+"I"*x in s:
                k = x
        elif "J"*x in s:
                pass
        elif "O"*x in s:
                pass
        elif "I"*x in s:
                pass
        else:
                print(k)
                break

Submission status: Runtime Error

Solution code:
s = raw_input()
k = x = 0
while 1:
        x += 1
        if "J"*x+"O"*x+"I"*x in s:
                k = x
        elif not "J"*x in s:
                print(k)
                break
        elif not "O"*x in s:
                print(k)
                break
        elif not "I"*x in s:
                print(k)
                break

Submission status: Runtime Error

Solution code:
s = raw_input()
k = x = 0
while x < len(s)*0.4:
        x += 1
        if "J"*x+"O"*x+"I"*x in s:
                k = x
        elif not "J"*x in s:
                print(k)
                break
        elif not "O"*x in s:
                print(k)
                break
        elif not "I"*x in s:
                print(k)
                break

Submission status: Runtime Error

Solution code:
s = raw_input()
for i in reversed(range(1,len(s)/3+1)):
	if 'J' * i + 'O' * i + 'I' * i in s:
		print i
		exit()
print 0

Submission status: Runtime Error

Solution code:
a=raw_input()
for k in range(len(a)//3,0,-1):
	joi="J"*k+"O"*k+"I"*k
	if joi in a:
		print k
		break
else:
	print 0

Submission status: Runtime Error

Solution code:
a=raw_input()
sp=min(a.count("J"),a.count("I"),a.count("O"))
for k in range(sp,0,-1):
	joi="J"*k+"O"*k+"I"*k
	if joi in a:
		print k
		break
else:
	print 0