[백준 / java] 1764 : 듣보잡

chaen-ing·2024년 4월 16일
0

1일1백준

목록 보기
15/18

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

package boj1764;

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

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 N = Integer.parseInt(st.nextToken());
        int M = Integer.parseInt(st.nextToken());

        HashSet<String> hashSet = new HashSet<>();

        while(N --> 0){
            hashSet.add(br.readLine());
        }

        ArrayList<String> arrayList = new ArrayList<>();
        int cnt = 0;

        while(M --> 0){
            String str = br.readLine();
            if(hashSet.contains(str)){
                arrayList.add(str);
                cnt++;
            }
        }

        Collections.sort(arrayList);

        System.out.println(cnt);
        for(String i : arrayList){
            System.out.println(i);
        }

    }
}

hashset : 집합 자료구조

검색빠르다는 장점

중복 X

  • add : 추가
  • contains : 있으면 true, 없으면 false
profile
💻 개발 공부 기록장

0개의 댓글

관련 채용 정보