728x90
문제-https://www.acmicpc.net/problem/1181
1181번: 단어 정렬
첫째 줄에 단어의 개수 N이 주어진다. (1 ≤ N ≤ 20,000) 둘째 줄부터 N개의 줄에 걸쳐 알파벳 소문자로 이루어진 단어가 한 줄에 하나씩 주어진다. 주어지는 문자열의 길이는 50을 넘지 않는다.
www.acmicpc.net
코드
i = int(input())
alphabet = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
dic = {}
for j in range(i):
m = input()
if(m not in dic):
v=''
for a in range(len(m)):
v += str(10+alphabet.index(m[a]))
dic[m] = int(v)
dic = sorted(dic.items(), key=lambda x:x[1])
for d in dic:
print(d[0])
| i | 입력할 단어의 수 |
| alphabet | 알파벳 저장 리스트 |
| v | 입력 단어의 값을 정렬 가능하도록 저장 |
| dic | 입력값과 정렬 기준을 딕셔너리 형태로 저장 |
-민식어(https://dev-hh.tistory.com/10)문제와 비슷하게 풀었다. 이를 참고해도 좋을 듯하다. 하지만 여기서는 문자열을 수로 변환 후 딕셔너리의 값을 순차정렬(sorted(dic.items(), key=lambda x:x[1]))하여 리스트 형태로 저장하였다.
결과
input:
13
but
i
wont
hesitate
no
more
no
more
it
cannot
wait
im
your
ouput:
i
im
it
no
but
more
wait
wont
yours
cannot
hesitate
728x90
'백준 문제 > 파이썬' 카테고리의 다른 글
| 백준 10773 제로(python)-[sliver(4)] (5) | 2023.10.06 |
|---|---|
| 백준 2108 통계학(python)-[sliver(3)] (5) | 2023.10.04 |
| 백준 1940 주몽(python)-[sliver(4)] (1) | 2023.09.04 |
| 백준 1599 민식어(python)-[Gold(5)] (5) | 2023.08.29 |
| 백준 4659 비밀번호 발음하기(python)-[silver(5)] (6) | 2023.08.25 |