백준 1764번

김경욱·2026년 1월 25일

백준

목록 보기
120/121

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;

// Press Shift twice to open the Search Everywhere dialog and type show whitespaces,
// then press Enter. You can now see whitespace characters in your code.
public class Main {
public static void main(String[] args) throws IOException {

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

    StringTokenizer st = new StringTokenizer(br.readLine());
    int x = Integer.parseInt(st.nextToken());
    int y = Integer.parseInt(st.nextToken());

    Set set = new HashSet();

    for (int i = 0; i < x; i++) {
        set.add(br.readLine());
    }
    int count = 0;
    List<String> names = new ArrayList<>();

    for (int i = 0; i < y; i++) {
       String nosee = br.readLine();

        if(set.contains(nosee))
        {
            count++;
            names.add(nosee);
        }
    }

    Collections.sort(names);

    System.out.println(count);
    for (String name : names) {
        System.out.println(name);
    }





}

} 정렬을 이용할때는 list를 써서! 구해야한다! 오름차순은 Collections.sort(list)이고 내림차순은 Collections.sort(list, Collections.reverseOrder());을 이용하면 된다.

0개의 댓글