반응형
문제
https://www.acmicpc.net/problem/31562
해설
문자열 파싱과 해시 사용이 핵심이었다.
Solution 1.
def solution(songs, trial):
song_dict = {song[0]: song[1] for song in songs}
for t in trial:
cnt = 0
matched_song = ""
for k, v in song_dict.items():
if v.startswith(t):
matched_song = k
cnt = cnt + 1
if cnt > 1:
print("?")
elif cnt == 1:
print(matched_song)
else:
print("!")
N, M = map(int, input().split())
song_lst = [input().split(" ", 2)[1:] for _ in range(N)]
trial_lst = [input() for _ in range(M)]
solution(song_lst, trial_lst)
Solution 2.
# 미리 answer 만들어놓고 시작
def solution(songs, trial):
song_dict = {song[0]: song[1] for song in songs}
answer = ["!", "", "?"]
for t in trial:
cnt = 0
for k, v in song_dict.items():
if v.startswith(t):
answer[1] = k
cnt = cnt + 1
if cnt > 1:
break
print(answer[cnt])
N, M = map(int, input().split())
song_lst = [input().split(" ", 2)[1:] for _ in range(N)]
trial_lst = [input() for _ in range(M)]
solution(song_lst, trial_lst)
반응형