알고리즘/해시

[백준] 1764번 듣보잡 #Java

VIPeveloper 2022. 5. 17. 01:56
728x90
반응형
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.*;

public class Main {

    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(br.readLine());

        int n = Integer.parseInt(st.nextToken());
        int m = Integer.parseInt(st.nextToken());

        HashMap<String,Integer> hashMap = new HashMap<>();
        for (int i = 0; i < n; i++) {
            hashMap.put(br.readLine(),1);
        }
        int answer = 0;
        ArrayList<String> res = new ArrayList<>();
        for (int i = 0; i < m; i++) {
            String s = br.readLine();
            int orDefault = hashMap.getOrDefault(s, 0);
            if(orDefault != 0){
                res.add(s);
                answer += orDefault;
            }
        }
        Collections.sort(res);
        System.out.println(answer);
        for (String s :
                res) {
            System.out.println(s);
        }
    }
}
728x90
반응형