Problem:

マルバツスタンプ (Circle Cross Stamps)

問題文

JOI 君はマルスタンプ，バツスタンプ，マルバツスタンプの3種類のスタンプをそれぞれ 0 個以上持っている．これらはマルやバツのマークを紙に印字することができるスタンプである．

マルスタンプを使うとマルが 1 つ印字され，バツスタンプを使うとバツが 1 つ印字される．マルバツスタンプを使うとマルとバツが横一列に 1 つずつ印字され，スタンプの向きを変えることで，マルの右にバツが来るようにも，バツの右にマルが来るようにも印字できる．

JOI 君は，持っているスタンプをそれぞれちょうど 1 回ずつ適当な順番で使い，紙に横一列にマルとバツを印字した．印字されたマルとバツの列は文字列 S で表される．S は O と X から構成された長さ N の文字列であり，S_i =  O ならば JOI 君が印字したマークのうち左から i 番目のものがマルであることを表し，S_i = X ならばそれがバツであることを表す (1 ≦ i ≦ N)．

あなたは，JOI 君が持っているスタンプの個数は分からないが，JOI 君が印字したマルとバツの列は知っている．印字されたマルとバツの列から，JOI 君が持っているマルバツスタンプの個数としてあり得るもののうち最大値を求めよ．

 制約

1 ≦ N ≦ 100000 (= 10^5)

S は長さ N の文字列である．

S の各文字は O か X である．

 入力・出力

入力

入力は以下の形式で標準入力から与えられる．

N

S

出力

JOI 君が持っているマルバツスタンプの個数としてあり得るもののうち最大値を出力せよ．

入出力例

入力例 1

5
OXXOX

出力例 1

2

JOI 君が印字したマークは，左から順に，マル，バツ，バツ，マル，バツである．JOI 君がマルスタンプ，バツスタンプ，マルバツスタンプをそれぞれ 0, 1, 2 個持っているとすると，以下の順番でスタンプを使えば，そのようにマークを印字することができる．

1 つ目のマルバツスタンプを使ってマルとバツをこの順に印字する．

この右に，2 つ目のマルバツスタンプを使ってバツとマルをこの順に印字する．

最後に，この右に，バツスタンプを使ってバツを印字する．

マルバツスタンプを 3 個以上持っているケースは考えられないので，2 を出力する．

入力例 2

14
OXOXOXOXXOXOXO

出力例 2

7

入力例 3

10
OOOOOOOOOO

出力例 3

0

情報オリンピック日本委員会作 『第 18 回日本情報オリンピック JOI 2018/2019 予選競技課題』



Sample input:
5
OXXOX


Sample output:
2

Submission status: Accepted

Solution code:
N = int(input())
S = list(input().strip())

cnt1 = 0
cnt2 = 1
stamp_0x = 0


#print(S[cnt1]==S[cnt2])
while cnt1 < N:
    if cnt1 < N-1:
        if S[cnt1] != S[cnt2]:
            stamp_0x += 1
            cnt1 += 2
            cnt2 += 2
        
        else:
            cnt1 += 1
            cnt2 += 1
            continue
    else:
        cnt1 += 1

print(stamp_0x)

Submission status: Accepted

Solution code:
n = int(input())
s = input()

i = 0
ans = 0
while i+1 < n:
    if s[i:i+2] in ['OX','XO']:
        i += 2
        ans += 1
    else:
        i += 1
print(ans)

Submission status: Accepted

Solution code:
#標準入力
i = int(input())
str1 = input()

#まるばつスタンプの数を数えまるばつスタンプを消す
i = str1.count("OX")
line = str1.split("OX")

#リストに残った要素で文字列を作り、ばつまるスタンプの数を数える
str2 = "".join(line)
print(i + str2.count("XO"))

Submission status: Accepted

Solution code:
N = int(input())
S = list(input())
# print(N)
# print(S)

from collections import deque

cnt = 0  # マルバツスタンプの数

que = deque()

for s in S:
	if len(que) == 0:
		que.append(s)
	else:
		t = que.pop()
		if s != t:
			cnt = cnt + 1
		else:
			que.append(t)
#			que.append(s)

print(cnt)

Submission status: Accepted

Solution code:
N=int(input())
s=input()
n=0
cnt=0
while n<=len(s)-2:
    if s[n:n+2]=="OX"or s[n:n+2]=="XO":
        cnt+=1
        n+=2
    else:
        n+=1
print(cnt)

Submission status: Accepted

Solution code:
N = input("")
S = input("")

maru_batsu = 0
judge_skip = False
for index in range(1, int(N)):
    s0 = S[index - 1]
    s1 = S[index]
    if judge_skip == True:
        judge_skip = False
        continue
    if s0 != s1:
        maru_batsu += 1
        judge_skip = True
print(maru_batsu)

Submission status: Accepted

Solution code:
n = int(input())
s = input()

i = 0
ans = 0
while i+1 < n:
    if s[i:i+2] in ['OX','XO']:

        i += 2
        ans += 1
    else:
        i += 1
print(ans)

Submission status: Accepted

Solution code:
def found_min(x, y):
    if x == -1 and y == -1:
        return -1
    elif x == -1:
        return y
    elif y == -1:
        return x
    else:
        if x >= y:
            return y
        else:
            return x
n = int(input())
Stamp = input()

OXStamp = 0
index = 0

while True:
    
    i = found_min(Stamp[index:].find("OX"), Stamp[index:].find("XO"))
    if i == -1:
        break
    else:
        OXStamp += 1
        index += i + 2
print(OXStamp)

Submission status: Accepted

Solution code:
n=int(input())
i=0
a=0
s=input()
while i<n-1:
	if s[i]+s[i+1]=="OX" or s[i]+s[i+1]=="XO":
		i+=2
		a+=1
	else:
		i+=1
print(a)

Submission status: Accepted

Solution code:
N = int(input())
S = input()
dp = [0]*(N+1)
dp[0] = 0
for i in range(N):
    if i < N-1 and S[i] != S[i+1]:
        dp[i+2] = dp[i]+1
    dp[i+1] = max(dp[i+1], dp[i])
print(dp[N])

Submission status: Accepted

Solution code:
N = int(input())
S  = input()

INF = 10**10
dp = [-INF] * (N+1)
dp[0] = 0
prev = S[0]
for i in range(N):
    # i = 0のときここを通ることはないのでOK
    if S[i] != prev:
        dp[i+1] = dp[i-1] + 1
    else:
        dp[i+1] = dp[i]
    prev = S[i]
print(dp[N])

Submission status: Accepted

Solution code:
input()
s = input()
ans = 0
prev = None
flag = False
for c in s:
    if flag and prev != c:
        ans += 1
        flag = False
    else:
        flag = True
    prev = c
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
inji = input()

i = 0
cnt = 0
while i + 1 < N:
    if inji[i:i+2] in ["XO","OX"]:
        i += 2
        cnt += 1
    else:
        i+=1
print(cnt)

Submission status: Accepted

Solution code:
n = int(input())
s = input()
ans = s.count("OX")
s = s.replace("OX","")
ans += s.count("XO")
print(ans)

Submission status: Accepted

Solution code:
strlen = int(input())
stamp_book = input()

MARU = "O"
BATSU = "X"

count = 0
index = 0

while True:
    if (index + 1) >= strlen:
        break

    if stamp_book[index] != stamp_book[index + 1]:
        count += 1
        index += 2
    else:
        index += 1

print(count)

Submission status: Accepted

Solution code:
N = int(input())
S = input()

ans = 0
i = 0
while i < N-1:
    if S[i] == S[i+1]:
        i += 1
    else:
        ans += 1
        i += 2

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s = input()[::-1]

cnt = 0
while True:
    if s[:2] in  ["OX", "XO"]:
        s = s[2:]
        cnt += 1
    else:
        s = s[1:]

    if s == "":
        break

print(cnt)

Submission status: Accepted

Solution code:
n = int(input())
s = input()
flag = 0
ans = 0
for i,v in enumerate(s):
    if i == 0:
        temp = v   
    else:
        if v != temp:
            if flag == 0:
                ans += 1
                flag = 1
            elif flag == 1:
                flag = 0
        else:
            if flag == 1:
                flag = 0
        temp = v

print(ans)

Submission status: Accepted

Solution code:
N=int(input())
ox=input()



count=0
skip=[False]*N
for i in range(N):
    if skip[i]:
        continue
    if i==N-1:
        break
    if ox[i]=='O':
        if ox[i+1]=='X':
            count+=1
            if i==N-1:
                break
            skip[i+1]=True
            
            continue
        else:
            continue
    else:
        if ox[i+1]=='O':
            count+=1
            if i==N-1:
                break
            skip[i+1]=True
            continue
        else:
            continue

print(count)

Submission status: Accepted

Solution code:
from collections import Counter

N = int(input())
S = input()

counter = 0
prev = S[0]
i = 1
while i < N:
    if S[i] == prev:
        prev = S[i]
        i += 1
    else:
        if i < N - 1:
            prev = S[i + 1]        
        counter += 1
        i += 2
print(counter)

Submission status: Accepted

Solution code:
# coding: utf-8
n=int(input())
s=input()
dp=[0 for i in range(n)]
for i in range(1,n):
    if s[i-1]!=s[i]:
        dp[i]=max(dp[i-1],dp[i-2]+1 if i-2>=0 else 1)
    else:
        dp[i]=dp[i-1]
print(dp[-1])

Submission status: Accepted

Solution code:
n = int(input())
s = input()

count = 0
flag = 0
for i in range(1, n):
    if flag==1:
        flag = 0
        continue
    if s[i-1] != s[i]:
        count += 1
        flag = 1
print(count)

Submission status: Accepted

Solution code:
#!/usr/bin/python3

import array
from fractions import Fraction
import functools
import itertools
import math
import os
import sys


def main():
    N = read_int()
    S = inp()
    print(solve(N, S))


def solve(N, S):
    i = 0
    count = 0
    while i < N - 1:
        if S[i] != S[i + 1]:
            count += 1
            i += 2
        else:
            i += 1
    return count


###############################################################################
# AUXILIARY FUNCTIONS

DEBUG = 'DEBUG' in os.environ


def inp():
    return sys.stdin.readline().rstrip()


def read_int():
    return int(inp())


def read_ints():
    return [int(e) for e in inp().split()]


def dprint(*value, sep=' ', end='\n'):
    if DEBUG:
        print(*value, sep=sep, end=end)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n = int(input())
s = input()
cnt = 0
i = 0
while i+1 < n:
    if s[i:i+2] in ['OX', 'XO']:
        cnt += 1
        i += 2
    else:
        i += 1
print(cnt)

Submission status: Accepted

Solution code:
N=int(input())
S=input()
i=0
oxcnt=0
while i+1<N:
    if S[i:i+2]in['OX','XO']:
        i+=2
        oxcnt+=1
    else:
        i+=1
print(oxcnt)

Submission status: Accepted

Solution code:
N=int(input())
S=input()
count=0
i=0
while i<N-1:
    if S[i]+S[i+1]=="OX" or S[i]+S[i+1]=="XO":
        count+=1
        i+=2
    else:
        i+=1
print(count)

Submission status: Accepted

Solution code:
#
# 　　  ⋀_⋀　 
#　　  (･ω･)  
# .／ Ｕ ∽ Ｕ＼
#  │＊　合　＊│
#  │＊　格　＊│ 
#  │＊　祈　＊│ 
#  │＊　願　＊│ 
#  │＊　　　＊│ 
#      ￣
#
import sys
sys.setrecursionlimit(10**6)
input=sys.stdin.readline
from math import floor,ceil,sqrt,factorial,log #log2ないｙｐ
from heapq import heappop, heappush, heappushpop
from collections import Counter,defaultdict,deque
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from bisect import bisect_left,bisect_right
from copy import deepcopy
inf=float('inf')
mod = 10**9+7
def pprint(*A): 
    for a in A:     print(*a,sep='\n')
def INT_(n): return int(n)-1
def MI(): return map(int,input().split())
def MF(): return map(float, input().split())
def MI_(): return map(INT_,input().split())
def LI(): return list(MI())
def LI_(): return [int(x) - 1 for x in input().split()]
def LF(): return list(MF())
def LIN(n:int): return [I() for _ in range(n)]
def LLIN(n: int): return [LI() for _ in range(n)]
def LLIN_(n: int): return [LI_() for _ in range(n)]
def LLI(): return [list(map(int, l.split() )) for l in input()]
def I(): return int(input())
def F(): return float(input())
def ST(): return input().replace('\n', '')
def main():
    N = I()
    S = ST()+"//"
    i=0
    ans =0
    while i < N:
        if S[i:i+2] in ("OX","XO"):
            ans += 1
            i +=1
        i+=1
    print(ans)
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n=int(input())
S=[]
S=input().split()
a=[]
for d in S[0]:
    a.append(d)
p=[]
for i in range(0,n):
    if a[i]=='O':
        p.append(1)
    else:
        p.append(0)
i=0
k=0
while i<n-1:
    if p[i]+p[i+1]==1:
        k=k+1
        i=i+2
    else:
        i=i+1
print(k)

Submission status: Accepted

Solution code:
N=int(input())
M=input()
x=list(M)
cnt=0
a=list()
for i in x:
    a.append(i)
    try:
        if (a[0]=='O' and a[1]=='X') or (a[0]=='X' and a[1]=='O'):
            cnt+=1  
            a=[]
        elif a[1]=='O':
            a=[]
            a.append(i)
        elif a[1]=='X':
            a=[]
            a.append(i)
    except:
        continue
print(cnt)

Submission status: Accepted

Solution code:
n = int(input())
mb = str(input())

cnt = 0
g = 0

for c in mb:
  if c == "O":
    if g == -1:
      cnt = cnt + 1
      g = 0
    else:
      g = 1
  else:
    if g == 1:
      cnt = cnt + 1
      g = 0
    else:
      g = -1

print(cnt)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
# 学籍番号 17D8101029G 石塚 博之 ID: mcatpolo 言語: python3

N = int(input())
S = list(input())
cnt = int(0)
S.append('Z')
i = int(0)

while i<N:
    if S[i] == 'O' and S[i+1] == 'X':
        cnt+=1
        i+=2
    elif S[i] == 'X' and S[i+1] == 'O':
        cnt+=1
        i+=2
    else:
        i+=1

print(cnt)

Submission status: Accepted

Solution code:
n = int(input())
s = input()

ans, i = 0, 1
while i < n:
    if s[i-1] != s[i]:
        ans += 1
        i += 1
    
    i += 1

print(ans)

Submission status: Accepted

Solution code:
n=int(input())
s=list(input())
cnt=0
def jesus(a,b):
    if (a=='O' and b=='X') or (b=='O' and a=='X'):
        return True
    else:
        return False
i=0
while i+1<n:
    if jesus(s[i],s[i+1]):
        cnt+=1
        i+=1
    i+=1
print(cnt)

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    S = input()
    pre = False
    ans = 0
    for i in range(N-1):
        if pre:
            pre = False
            continue
        if (S[i] == 'O' and S[i+1] == 'X') or (S[i] == 'X' and S[i+1] == 'O'):
            ans += 1
            pre = True
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n = int(input())
s = input()
i = 0
x = 0
while i+1 < n:
    if s[i:i+2] in ['OX','XO']:
        i += 2
        x += 1
    else:
        i += 1
print(x)

Submission status: Accepted

Solution code:
input()
s = [i for i in input()]
cnt = 0
j = 0
for i in range(len(s)-1):
    if j == 1:
        j = 0
    elif s[i]+s[i+1] == 'OX' or s[i]+s[i+1] == 'XO':
        cnt += 1
        j = 1
print(cnt)

Submission status: Accepted

Solution code:
import re
input()
print(re.sub(r'OX|XO','@',input()).count('@'))

Submission status: Accepted

Solution code:
times = input()
str = input()

depth  = 0
i = 1

while i < len(str):
    if str[i - 1] != str[i]:
        depth += 1
        i += 2
    else:
        i += 1

print(depth)

Submission status: Accepted

Solution code:
check = True
cnt = 0

n = int(input())
stamps = input()

while len(stamps)>1:
    if check:
        if stamps[:2] in ["XO", "OX"]:
            cnt += 1
            check = False
    else:
        check = True
    stamps = stamps[1:]

print(cnt)

Submission status: Accepted

Solution code:
num=int(input())
ox=input()
ans=0
while True:
    if len(ox)<=1:
        break
    a=ox.find("OX",0,2)
    b=ox.find("XO",0,2)
    if a==0 or b==0:
        ox=ox[2:]
        ans+=1
    else:
        ox=ox[1:]

print(ans)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline

def main():
    N = int(input())
    s = "**" + input().strip()

    dp = [0] * (N+2)

    for i in range(N+2):
        if s[i-1:i+1] == "OX" or s[i-1:i+1] == "XO":
            dp[i] = max(dp[i-1], dp[i-2] + 1)
        else:
            dp[i] = dp[i-1]
    # print(dp)
    print(dp[-1])

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
N = int(input())
S = input()
cnt = 0
while N > 1:
    stamp = S[N-2:N]
    if stamp == "OX" or stamp == "XO":
        cnt += 1
        N -= 2
    else:
        N -= 1
print(cnt)

Submission status: Accepted

Solution code:
n=int(input())
s=input()
i=0
cnt=0
while i<=n:
    if i==n:
        break
    if s[i:i+2]=='OX' or s[i:i+2]=='XO':
        cnt+=1
        i+=2
    else:
        i+=1
print(cnt)

Submission status: Accepted

Solution code:
n = int(input())
s = list(input())
if n%2: s+=["?"];n+=1
ans = 0
i = 0
while i<n-1:
    if s[i]+s[i+1] in["OX","XO"]: ans+=1
    else: i-=1
    i+=2
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
s = input()

cnt = 0
i = 0

while i < N - 1:
    if not s[i] == s[i + 1]:
        cnt += 1
        i += 2
    else:
        i += 1

print(cnt)

Submission status: Accepted

Solution code:
N = int(input())
S = input()

x0, x1 = -1, -1
cnt = 0
flag = False

for i in range(N):
    if flag == False:
        if i > 0 and S[i] != S[i-1]:
            cnt += 1
            flag = True
    else:
        flag = False
    x0 = x1

print(cnt)

Submission status: Accepted

Solution code:
n=int(input())
s=input()

i,cnt=0,0
while i<n-1:
    t=s[i]+s[i+1]
    if t=="OX" or t=="XO":
        cnt+=1
        i+=1
    i+=1
print(cnt)

Submission status: Accepted

Solution code:

def solve():
    N = int(input())
    S = input()
    stomp = 0
    for i in range(1, len(S)):
        pass
    i = 0
    while True:
        if i >= len(S) - 1:
            break
        if S[i] != S[i + 1]:
            stomp += 1
            i += 1
        i += 1
    print(stomp)


if __name__ == '__main__':
    solve()

Submission status: Accepted

Solution code:
n=int(input())
k=input()
cnt=0
i=0
while i<len(k)-1:
    t=k[i]+k[i+1]
    if t=='OX' or t=='XO':
        cnt+=1
        i+=2
    else:
        i+=1
print(cnt)

Submission status: Accepted

Solution code:
N = int(input())
S = list(input())
cnt = 0

for i in range(N - 1) :
  if(S[i] == "O" and S[i + 1] == "X") :
    S[i] = "l"
    S[i + 1] = "l"
    cnt += 1
  elif(S[i] == "X" and S[i + 1] == "O") :
    S[i] = "l"
    S[i + 1] = "l"
    cnt += 1
  else :
    pass

print(cnt)

Submission status: Accepted

Solution code:
N=int(input())
S=input()
l=len(S)

flag=""
count=0

for i in range(l):
    if S[i]=="O" and flag=="X":
        count+=1
        flag=""
    elif S[i]=="O" and flag=="":
        flag="O"
    elif S[i]=="X" and flag=="O":
        count+=1
        flag=""
    elif S[i]=="X" and flag=="":
        flag="X"
print(count)

Submission status: Accepted

Solution code:
n=int(input())
input_str = input()
i = 0
count = 0
while True:
    if (n - i) <= 1:
        print(count)
        break
    if input_str[i:(i+2)] == 'OX' or input_str[i:(i+2)] == 'XO':
        count += 1
        i += 2
    else:
        i += 1

Submission status: Accepted

Solution code:
#! /usr/bin/env python
# -*- coding: utf-8 -*-
import sys
import math 

def main():
   n= int(input())
   str = input()
   ans = 0
   last =str[0]
   for i in range(n):
       if( str[i] != last):
           ans +=1
           if(i+1 >= n): break;
           i += 1
           last = str[i]
       else:
           last = str[i] 
   print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!

#数学オリンピック_〇×スタンプ

kosu=int(input())

stamp=input()

i=0;
ans=0;

while i<len(stamp)-1:
    if stamp[i]!=stamp[i+1]:
        ans+=1
        i+=2
    else:
        i+=1

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s = input()
pre_c = ""
flag = False
ans = 0

for c in s:
    if flag:
        if pre_c != c:
            ans += 1
            flag = False
            pre_c = c
    else:
        flag = True
        pre_c = c

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s = input()

i = 0
ans = 0
while i+1 < n:
    if s[i:i+2] in ['OX','XO']:
        i += 2
        ans += 1
    else:
        i += 1
print(ans)

Submission status: Accepted

Solution code:
N=int(input())
s=str(input())
S=[]
for i in s:
    S.append(i)

i=0
count=0
while i<N-1:
    if (S[i]=='O' and S[i+1]=='X') or (S[i]=='X' and S[i+1]=='O'):
        count+=1
        i+=2
    else:
        i+=1
print(count)

Submission status: Accepted

Solution code:
n=int(input())
s=input()
ans=0
ans+=s.count("OX")
s=s.replace('OX','')
ans+=s.count("XO")
print(ans)