Coding Test
백준 듣보잡(1764)
댕발바닥
2024. 2. 27. 22:10
이전 문제가 쉬운거 같아서 새로 풀어봤는데 더 쉽다..
https://www.acmicpc.net/problem/1764
1764번: 듣보잡
첫째 줄에 듣도 못한 사람의 수 N, 보도 못한 사람의 수 M이 주어진다. 이어서 둘째 줄부터 N개의 줄에 걸쳐 듣도 못한 사람의 이름과, N+2째 줄부터 보도 못한 사람의 이름이 순서대로 주어진다.
www.acmicpc.net
해시 테이블로 관리하면 된다.
package struct;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.Scanner;
public class Q1764 {
public static void main(String[] args) {
struct();
}
public static void struct() {
Scanner sc = new Scanner(System.in);
int n = Integer.parseInt(sc.next());
int m = Integer.parseInt(sc.next());
sc.nextLine();
ArrayList<String> answer = new ArrayList<>();
HashSet<String> names = new HashSet<>();
for (int i=0; i< n; i++) {
names.add(sc.nextLine().trim());
}
for (int i=0; i< m; i++) {
String name = sc.nextLine().trim();
if (names.contains(name)) {
answer.add(name);
}
}
System.out.println(answer.size());
Collections.sort(answer);
answer.forEach(it -> System.out.println(it));
}
}
결과