https://www.acmicpc.net/problem/2870
1. 메인 아이디어
- prev를 도입하여 이전 숫자/단어가 알파벳인지 아닌지 검증한다.
- 알파벳이 아닐 경우 무조건 str += w로 담고본다.
- 알파벳 -> 숫자가 나오는 시점에는 str += w를 더한다.
- 숫자 -> 알파벳이 나오는 시점에는 digits 리스트에 str을 int형으로 담고, str을 ''로 초기화한다.
> digits 리스트에 str을 담을 때, str이 애초에 ''이 아닌지도 검증이 필요하다 (if str != '' and str_lstrip != '':)
> str 검증을 하지않으면 '' 빈 공백이 digits에 담기기 때문임
- digits 리스트의 오름차순으로 정렬한 후, print한다.
> digits 리스트의 원소 타입이 '' 스트링 타입이면, 숫자 크기가아닌 사전순 정렬이 되므로 주의한다.
> ex:) 0 2 2 4 23223 5 (X) -> 0 2 2 4 5 23223 (O)
- 안쪽 for 문이 끝나면 ( for w in s: ) input_digit_or_zero 함수를 한번더 호출한다
> 왜냐면, ... 002 이런식으로 문장이 끝나면 2를 담지 못하기 때문에 한번 더 담아줘야함
2. 소스 코드
import sys
input = sys.stdin.readline
alphabet = 'abcdefghijklmnopqrstuvwxyz'
n = int(input())
lines = []
digits = []
for _ in range(n):
lines.append(input().strip())
def input_digit_or_zero(str):
str_zero = str.lstrip('0')
if str != '':
if str_zero == '':
digits.append(0)
else:
digits.append(int(str_zero))
for s in lines:
str = ''
prev = -1
for w in s:
if prev == -1:
if w not in alphabet:
str += w
else:
if w not in alphabet:
str += w
elif prev not in alphabet and w in alphabet:
input_digit_or_zero(str)
str = ''
elif prev in alphabet and w not in alphabet:
str += w
prev = w
input_digit_or_zero(str)
for i in sorted(digits):
print(i)
'Coding Test > Baek Joon.' 카테고리의 다른 글
2480 - 주사위 세개 (0) | 2024.08.31 |
---|---|
2525 - 오븐시계 (0) | 2024.08.31 |
2910 - 빈도 정렬 (0) | 2024.08.24 |
2828 - 사과 담기 게임 (2) | 2024.08.23 |
1992 - 쿼드트리 (0) | 2024.08.22 |