728x90
SMALL
import sys; input=sys.stdin.readline
def is_subsequence(s1, s2):
it = iter(s2)
return all(c in it for c in s1)
def devil_game(original, dictionary):
best_word = ''
best_efficiency = 0
for w, g in dictionary:
if is_subsequence(original, w):
added_letters = len(w) - len(original)
if added_letters == 0:
continue
efficiency = g / added_letters
if efficiency > best_efficiency:
best_efficiency = efficiency
best_word = w
return best_word if best_word else 'No Jam'
m = input().strip()
n = int(input())
dictionary = [input().split() for _ in range(n)]
dictionary = [(w, int(g)) for w, g in dictionary]
result = devil_game(m, dictionary)
print(result)
- iter()와 all()을 사용하여 간단히 두 문자열에 대한 비교를 진행할 수 있습니다.
- 각 가성비에 대해서 max 값을 갱신하는데, 이 때 이 값보다 가성비가 큰 단어의 경우에만 갱신합니다.
- dictionary를 사전순으로 입력하였기에 결과적으로는 사전순으로 앞선 단어가 출력되게 됩니다.
728x90
LIST
'알고리즘 문제 > 랜덤 마라톤 (solved.ac)' 카테고리의 다른 글
🥈 2422번: 한윤정이 이탈리아에 가서 아이스크림을 사먹는데 (1) | 2024.07.18 |
---|---|
🥈 17176번: 암호해독기 (0) | 2024.07.17 |
🥉 29720번: 그래서 님 푼 문제 수가? (0) | 2024.07.10 |
🥈 14426번: 접두사 찾기 (2) | 2024.07.09 |
🥈 1940번: 주몽 (0) | 2024.07.08 |