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
input = sys.stdin.readline
n = int(input())
m = int(input())
s = input()
p = 'IO' * n + 'I'
p_length = len(p)
count = 0
for i in range(m - p_length + 1):
if s[i] == 'I':
if s[i: i + p_length] == p:
count += 1
print(count)
시간 초과..
조금더 빨리 바꿔보자
import sys
input = sys.stdin.readline
n = int(input())
m = int(input())
s = input()
answer, i, count = 0, 0, 0
while i < (m - 1):
if s[i : i + 3] == 'IOI':
i += 2
count += 1
if count == n:
answer += 1
count -= 1
else:
i += 1
count = 0
print(answer)
'백준 알고리즘 (Baekjoon Algorithm)' 카테고리의 다른 글
[파이썬] 백준 알고리즘 No.24479 알고리즘 수업 - 깊이 우선 탐색 1 (0) | 2023.02.08 |
---|---|
[파이썬] 백준 알고리즘 No.1753 최단경로 (0) | 2022.03.15 |
[파이썬] 백준 알고리즘 No.24498 blobnom - 블롭컵 대회 (0) | 2022.02.22 |
[파이썬] 백준 알고리즘 No.10815 숫자 카드 (0) | 2022.02.11 |
[파이썬] 백준 알고리즘 No.11286 절댓값 힙 (0) | 2022.02.04 |