[백준 알고리즘] 1764번 듣보잡 (java)

Ash·2020년 8월 1일
0
post-thumbnail

풀이방법

듣도 못한 사람 ∩ 보도 못한 사람

  1. 듣도 못한 사람들의 이름을 HashSet에 저장
  2. 보도 못한 사람들의 이름들 중 HashSet에 있는 이름들을 ArrayList 저장 -> 듣도 보도 못한 사람들
  3. ArrayList 에 있는 이름들을 사전순으로 정렬

소스코드

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));
		List<String> list = new ArrayList<String>();
		String[] cnt = br.readLine().split(" ");
		int n = Integer.parseInt(cnt[0]);
		int m = Integer.parseInt(cnt[1]);
		Set<String> set = new HashSet<String>();
		
		while(n-- >0) set.add(br.readLine());
        
		while(m -- > 0) {
			String name = br.readLine();
			if(set.contains(name)) {
				list.add(name);
			}
		}
		
		Collections.sort(list);
		
		System.out.println(list.size());
		if(list.size() != 0) list.forEach(item -> System.out.println(item));
	}
	
}
profile
기록남기기👩‍💻

0개의 댓글