백준 1764 (듣보잡)

김경욱·2025년 8월 24일

백준

목록 보기
54/121

import java.util.;
import java.io.
;

// 1 10 13 16
public class Main {
public static void main(String[] args) throws IOException {

    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

    StringTokenizer st = new StringTokenizer(br.readLine());
    int count = 0;
    int N = Integer.parseInt(st.nextToken());
    int M = Integer.parseInt(st.nextToken());

    HashSet<String> sets = new HashSet<>();
    String[] s = new String[N + M];
    String[] x = new String[N + M];

    for (int i = 0; i < N; i++) {
        sets.add(br.readLine());

    }

    for (int i = 0; i < M; i++) {

        s[i] = br.readLine();

        if (sets.contains(s[i])) {
            count++;
            x[i] = s[i];

        }
    }

    List<String> list = new ArrayList<>();

    for (int i = 0; i < M; i++)
    {
        if (x[i] != null)
        {
            list.add(x[i]);
        }
    }

    System.out.println(count);

    Collections.sort(list);

    for (String s2  : list) {

        System.out.println(s2);
    }










}

}
hash함수를 이용하여 count를 구한 후 list를 이용하여 null값이 아닌 것만 add로 추가한 후 Collections.sort(list)를 하여 내림차순으로 정렬시켰다.

0개의 댓글