목차

IOIOI

ps
링크acmicpc.net/…
출처BOJ
문제 번호5525
문제명IOIOI
레벨실버 2
분류

애드혹

시간복잡도O(n)
인풋사이즈n<=1,000,000
사용한 언어Python
제출기록31156KB / 184ms
최고기록140ms
해결날짜2021/08/09

풀이

코드

코드 1 - 정규표현식으로 매칭

"""Solution code for "BOJ 5525. IOIOI".

- Problem link: https://www.acmicpc.net/problem/5525
- Solution link: http://www.teferi.net/ps/problems/boj/5525

Tags: [RegEx]
"""

import re


def main():
    N = int(input())
    M = int(input())  # pylint: disable=unused-variable
    S = input()

    matches = re.finditer('I(OI)+', S)
    answer = sum(max(0, (m.end() - m.start()) // 2 - N + 1) for m in matches)

    print(answer)


if __name__ == '__main__':
    main()

코드 2 - 매칭 알고리즘 구현

"""Solution code for "BOJ 5525. IOIOI".

- Problem link: https://www.acmicpc.net/problem/5525
- Solution link: http://www.teferi.net/ps/problems/boj/5525
"""


def main():
    N = int(input())
    M = int(input())  # pylint: disable=unused-variable
    S = input()

    answer = 0
    cur_pattern_len = 0
    prev = 'O'
    for cur in S:
        if cur == prev:
            cur_pattern_len = 0
        elif cur == 'O':
            cur_pattern_len += 1
        elif cur_pattern_len >= N:
            answer += 1
        prev = cur

    print(answer)


if __name__ == '__main__':
    main()