Files
baekjoon-study/workbook_8708/silver/1283-s1.py
2026-02-23 11:14:50 +09:00

61 lines
1.8 KiB
Python

# 단축키 지정
import sys
input = sys.stdin.readline
def check_word_first(words, shortcut):
for i in range(len(words)):
c = words[i][0].upper()
if not shortcut.get(c, 0):
shortcut[c] = 1
return i
return -1
def check_word(word, shortcut):
for i in range(len(word)):
c = word[i].upper()
if not shortcut.get(c, 0):
shortcut[c] = 1
return i
return -1
def solution():
n = int(input().rstrip())
shortcut = {}
for _ in range(n):
option = input().rstrip().split()
shortcut_idx = check_word_first(option, shortcut)
if shortcut_idx != -1:
option[shortcut_idx] = f"[{option[shortcut_idx][0]}]" + option[shortcut_idx][1:]
print(*option)
continue
idx = 0
while idx < len(option):
shortcut_idx = check_word(option[idx], shortcut)
if shortcut_idx != -1:
option[idx] = option[idx][:shortcut_idx] + f"[{option[idx][shortcut_idx]}]" + option[idx][shortcut_idx+1:]
break
idx += 1
print(*option)
return
solution()
"""
걸린 시간: 41분
시간 복잡도: 완전 탐색이기 때문에 O(5*10*n)이다.
해설: 30개의 옵션에 1개 옵션 당 5개 이하 단어와 1개의 단어에 10개 이하의 알파벳이면 다 돌아봤자 1500이다.
따라서 조건대로 구현하면 된다. 대소문자를 구분하지 않기 때문에 다 upper로 만들어서 현재 사용된 옵션을 기록한다.
단어 첫 글자들을 보며 옵션 설정하고, 한 단어씩 쭉 보면서 옵션 설정을 한다.
옵션을 설정하면 출력 형태를 맞춰서 리스트에 저장하고 마지막에 한번에 출력한다.
"""