[BOJ] 9536 여우는 어떻게 울지?

iinnuyh_s·2024년 1월 3일
0

문자열

목록 보기
12/12
post-thumbnail

여우는 어떻게 울지?

풀이

  • 첫 번째 줄 뒤에 나오는 다른 동물들의 울음소리를 따로 Set에 저장한 뒤, 첫번째 줄에 주어진 String에 포함되는지 확인하면 된다.
  • 포함 안되는 값들만 StringBuilder 선언 후 append 해주면 끝
    🙆‍♀️ 정답 풀이
    import java.util.*;
    import java.io.*;
    public class Main {
        public static void main(String[] args) throws Exception {
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            StringTokenizer st;
            int T = Integer.parseInt(br.readLine());
            while(T-->0){
                //첫줄
                Set<String> animal = new HashSet<>();
                String str = br.readLine();
                //what does the fox say 나올 때 까지 while
                String input="";
                while(true){
                    input = br.readLine();
                    if(input.equals("what does the fox say?")) break;
                    String[] token = input.split(" ");
                    animal.add(token[token.length-1]);   //다른 울음소리들 넣어두기
                }
                //str에 doggy안에 있는 울음소리들이 있으면 빼야 됨. . . .
                String[] arrayStr = str.split(" ");
                StringBuilder sb = new StringBuilder();
                for(String s: arrayStr){
                    if(!animal.contains(s)){
                        sb.append(s).append(" ");
                    }
                }
                System.out.println(sb.toString().trim());
            }
        }
    }

0개의 댓글