알고리즘

<백준> 5525번 파이썬 알고리즘

changha. 2021. 11. 28. 22:45
n = int(input())
m = int(input())
s = input()

ans = 0
cnt = 0
for i in range(1, m - 1):
    if s[i - 1] == "I":
        if s[i] == "O" and s[i + 1] == "I":
          cnt += 1
          i += 2
          if n == cnt:
            ans += 1
            cnt -= 1
        else:
          cnt = 0

print(ans)

I로 시작했을 때 그뒤에 OI가 있으면 cnt += 1

 

그리고 cnt 가 n 과 일치할 때 ans += 1

 

이후에 또 판별해야되니까 cnt -= 1 하는 것이다