[백준] 9536. 여우는 어떻게 울지?

원숭2·2022년 2월 27일
0

백준

목록 보기
51/54

문제

풀이

  1. split함수를 사용하여 울음 소리의 입력을 sounds 배열로 변환함.
  2. 질문이 주어지기 전까지 동물 별 울음 소리를 animals 배열에 넣어줌.
  3. 질문이 주어지면, for문을 돌며 sounds에서 animals에 존재여부를 확인 후, 존재 시 값을 없앰.
  4. sounds에서 for문을 돌며 존재하는 값만 출력함.

코드

import sys

def solution() :
    t = int(sys.stdin.readline())
    
    for _ in range(t) :
        sounds = sys.stdin.readline().rstrip().split()
        
        animals = []
        while 1 :
            s = sys.stdin.readline().rstrip().split()
            if s[-1] == 'say?' :
                for i in range(len(sounds)) :
                    if sounds[i] in animals :
                        sounds[i] = ''
                for s in sounds :
                    if s != '' :
                        print(s, end = ' ')
                break
            else :
                animals.append(s[-1])
                
solution()

0개의 댓글