CS/Algorithm
[BOJ/Python] 백준 13022번 - 늑대와 올바른 단어
sohy
2022. 7. 1. 18:59
백준 #13022 늑대와 올바른 단어
13022번: 늑대와 올바른 단어
첫째 줄에 단어가 주어진다. 단어는 w, o, l, f로만 이루어져 있으며, 길이는 50을 넘지 않는다.
www.acmicpc.net
import sys
input = sys.stdin.readline
word = input().strip()
now = 0 # 현재 문자 인덱스
next = 1 # w: 1, o: 2, l: 3, f: 4
cnt = 0
cnt2 = 0
while now < len(word):
if word[now] == "w":
if next == 1:
while now < len(word) and word[now] == "w":
cnt += 1
now += 1
next = 2
else:
print(0)
exit()
elif word[now] == "o":
if next == 2:
while now < len(word) and word[now] == "o":
cnt2 += 1
now += 1
if cnt == cnt2:
next = 3
cnt2 = 0 # 초기화
else:
print(0)
exit()
else:
print(0)
exit()
elif word[now] == "l":
if next == 3:
while now < len(word) and word[now] == "l":
cnt2 += 1
now += 1
if cnt == cnt2:
next = 4
cnt2 = 0 # 초기화
else:
print(0)
exit()
else:
print(0)
exit()
elif word[now] == "f":
if next == 4:
while now < len(word) and word[now] == "f":
cnt2 += 1
now += 1
if cnt == cnt2:
next = 1
cnt, cnt2 = 0, 0 # 초기화
else:
print(0)
exit()
else:
print(0)
exit()
else:
print(0)
exit()
if next != 1: # 단어가 f로 끝나지 않은 경우
print(0)
else:
print(1)
- w의 개수를 세고 이후 o, l, f의 개수가 같은지 비교한다. 하나라도 같지 않으면 올바르지 않은 단어로 0을 출력하고 종료한다.
- w, o, l, f가 순서대로 위치해 있는지 next 변수로 확인한다.
- 마지막에 next가 1이 아닌 경우 단어가 f로 끝나지 않은 것을 의미한다. 따라서 올바르지 않은 단어로 0을 출력하고 종료한다.
코드 너무 더럽다 😰
☝🏻 주의 예제
wolfwo
>> 0