๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
4๏ธโƒฃ Python/Problem Solving

[Python Algorithm] IOIOI BOJ #5525

by seolhee2750 2022. 2. 3.
๋ฌธ์ œ

https://www.acmicpc.net/problem/5525

 

5525๋ฒˆ: IOIOI

N+1๊ฐœ์˜ I์™€ N๊ฐœ์˜ O๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ์œผ๋ฉด, I์™€ O์ด ๊ต๋Œ€๋กœ ๋‚˜์˜ค๋Š” ๋ฌธ์ž์—ด์„ PN์ด๋ผ๊ณ  ํ•œ๋‹ค. P1 IOI P2 IOIOI P3 IOIOIOI PN IOIOI...OI (O๊ฐ€ N๊ฐœ) I์™€ O๋กœ๋งŒ ์ด๋ฃจ์–ด์ง„ ๋ฌธ์ž์—ด S์™€ ์ •์ˆ˜ N์ด ์ฃผ์–ด์กŒ์„ ๋•Œ, S์•ˆ์— PN์ด ๋ช‡

www.acmicpc.net

 

๋‚ด ๋ฌธ์ œ ํ’€์ด
import sys

n = int(sys.stdin.readline())
m = int(sys.stdin.readline())
s = sys.stdin.readline().strip()
idx = 0
count = 0
result = 0

while idx <= m-2:
    if s[idx : idx+3] == "IOI":
        idx += 2
        count += 1
        if count == n:
            result += 1
            count -= 1
    else:
        idx += 1
        count = 0

print(result)

๐Ÿ‘‰ ๋ฌธ์ž์—ด ๋ฌธ์ œ๋กœ, ํŒจํ„ด์„ ์ฐพ์•„์„œ ์นด์šดํŠธํ•ด์ฃผ๋Š”๊ฒŒ ํฌ์ธํŠธ

๋ฌธ์ œ์—์„œ ์ฃผ์–ด์ง€๋Š” P๋Š” 'IOI'๋ฅผ ํŒจํ„ด์œผ๋กœ ๊ฐ–๋Š”๋‹ค.
๋”ฐ๋ผ์„œ ์ฃผ์–ด์ง„ s๋ฅผ ๋Œ๋ฉฐ 'IOI'ํŒจํ„ด์ด ๋งŒ์กฑํ•˜๋Š” ๊ณณ์„ ์ฐพ๊ณ ,

๋งŒ์กฑํ•œ๋‹ค๋ฉด ๋‘ ์นธ ๋’ค๋กœ ๊ฐ€์„œ, ๋˜ 'IOI'ํŒจํ„ด์„ ๋งŒ์กฑํ•˜๋Š”์ง€ ๊ฒ€์‚ฌํ•œ๋‹ค.

๊ฒ€์‚ฌํ•˜๋ฉด์„œ count๊ฐ€ n๊ฐœ๊ฐ€ ๋˜๋ฉด result๋ฅผ 1 ์ฆ๊ฐ€์‹œํ‚ค๊ณ , count๋Š” 1 ๊ฐ์†Œ์‹œํ‚จ๋‹ค.

 

๐Ÿ’ก ํ”ผ๋“œ๋ฐฑ
  • ์ฒ˜์Œ์—๋Š” ๋ฒ ์ŠคํŠธ์…€๋Ÿฌ ๋ฌธ์ œ์™€ ๋น„์Šทํ•ด์„œ ๋˜‘๊ฐ™์ด ํ’€์ดํ–ˆ๋Š”๋ฐ,
    ์ด ๋ฌธ์ œ์˜ ๊ฒฝ์šฐ IOI๋ผ๋Š” ํŠน์ •ํ•œ ํŒจํ„ด์„ ๊ฐ€์ง€๋ฏ€๋กœ ๋” ํšจ์œจ์ ์ธ ํ’€์ด๊ฐ€ ๊ฐ€๋Šฅํ–ˆ๋‹ค.

๋Œ“๊ธ€