본문 바로가기

Coding Test

백준 듣보잡(1764)

 

이전 문제가 쉬운거 같아서 새로 풀어봤는데 더 쉽다..

 

https://www.acmicpc.net/problem/1764

 

1764번: 듣보잡

첫째 줄에 듣도 못한 사람의 수 N, 보도 못한 사람의 수 M이 주어진다. 이어서 둘째 줄부터 N개의 줄에 걸쳐 듣도 못한 사람의 이름과, N+2째 줄부터 보도 못한 사람의 이름이 순서대로 주어진다.

www.acmicpc.net

 

해시 테이블로 관리하면 된다.

 

package struct;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.Scanner;

public class Q1764 {

    public static void main(String[] args) {
        struct();
    }

    public static void struct() {
        Scanner sc = new Scanner(System.in);
        int n = Integer.parseInt(sc.next());
        int m = Integer.parseInt(sc.next());
        sc.nextLine();

        ArrayList<String> answer = new ArrayList<>();
        HashSet<String> names = new HashSet<>();
        for (int i=0; i< n; i++) {
            names.add(sc.nextLine().trim());
        }
        for (int i=0; i< m; i++) {
            String name = sc.nextLine().trim();
            if (names.contains(name)) {
                answer.add(name);
            }
        }

        System.out.println(answer.size());
        Collections.sort(answer);
        answer.forEach(it -> System.out.println(it));

    }
}

 

결과

'Coding Test' 카테고리의 다른 글

그리디 알고리즘  (0) 2024.02.29
백준 가운데를 말해요(1655)  (0) 2024.02.29
백준 카드2(2164)  (0) 2024.02.27
백준 수 정렬하기 2(2751)  (1) 2024.02.27
백준 숫자 카드(10815)  (1) 2024.02.26