👉 https://www.acmicpc.net/problem/9442
입력에서 주어진 알파벳 순서대로 단어들을 재정렬한다.
base = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
base_dic = dict()
for idx, val in enumerate(list(base)):
base_dic[idx] = val
start = 1
while True:
come = input()
come = list(come.split())
N = come[0]
if int(N) == 0:
break
string = come[1]
print("year", start)
start += 1
dic = dict()
for idx, val in enumerate(list(string)):
dic[val] = idx
alpha = [input() for _ in range(int(N))]
tmp = []
x = 0
for i in range(len(alpha)):
result = ""
for j in range(len(alpha[i])):
idx = dic[alpha[i][j]]
result += base_dic[idx]
tmp.append((result, x))
x += 1
tmp.sort(key=lambda x: (x[0], len))
for value, idx in tmp:
print(alpha[idx])
⚡ 정렬
base_dic
딕셔너리와 입력으로 받은 알파벳 순서를 저장해둔 dic
딕셔너리 2개를 이용해 문제를 풀었다.(단어, 인덱스)
를 튜플로 저장해 정렬시켰다.