https://www.acmicpc.net/problem/21314
21314번: 민겸 수
민겸 수 하나가 주어진다. 민겸 수는 대문자 M과 K로만 이루어진 문자열이며, 길이는 3,000을 넘지 않는다.
www.acmicpc.net
가장 큰 수를 구하는 방법
- 문자열을 하나씩 탐색하다가 K를 만나면 끊어준다.
- 만약 문자열이 K로 끝나지 않으면? K로 끊은 이후의 문자열은 하나의 M으로 취급해 준다. (예를 들어 KMKMM인 경우 K, MK, M, M)
가장 작은 수를 구하는 법
- 문자열을 하나씩 끊어준다.
- 단, M이 연속되는 경우는 묶어준다. (예를 들어 MKKMMK인 경우 M, K, K, MM, K)
정답 코드
import sys
input = sys.stdin.readline
strings = list(input().strip())
# 가장 큰 수를 구하는 로직
temp = ""
before = 0
big_values = []
for index, string in enumerate(strings):
temp += string
if string == "K":
big_values.append(temp)
temp = ""
if index == len(strings) - 1 and temp != "":
for _ in range(len(temp)):
big_values.append("M")
answer = ""
for value in big_values:
if "K" in value:
answer += str(5 * (10 ** value.count("M")))
else:
answer += str(10 ** (value.count("M")-1))
print(answer)
# 가장 작은 수를 구하는 로직
temp = ""
before = 0
small_values = []
for index, string in enumerate(strings):
if string == "M":
temp += string
if string == "K":
small_values.append(temp)
small_values.append("K")
temp = ""
if index == len(strings) - 1 and temp != "":
small_values.append(temp)
small_values = [value for value in small_values if value != ""]
answer = ""
for value in small_values:
if "K" in value:
answer += str(5 * (10 ** value.count("M")))
else:
answer += str(10 ** (value.count("M")-1))
print(answer)
'PS > 백준' 카테고리의 다른 글
[백준] 2138번 전구와 스위치 (python) (1) | 2024.03.29 |
---|---|
[백준] 1080번 행렬 (python) (0) | 2024.03.27 |
[백준] 14719번 빗물 (python) (0) | 2024.03.22 |
[백준] 16918번 봄버맨 (python) (0) | 2024.03.22 |
[백준] 16948번 데스 나이트 (python) (0) | 2024.02.18 |