☕Language: Java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
|
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.HashSet;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.TreeSet;
public class Main {
public static void main(String[] args) throws IOException {
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
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());
Set<String> set = new HashSet<>();
for (int i = 0; i < N; i++) {
set.add(br.readLine());
}
int cnt = 0;
Set<String> result = new TreeSet<>();
while (M-- > 0) {
String s = br.readLine();
if (set.contains(s)) {
cnt++;
result.add(s);
}
}
bw.write(cnt + "\n");
for(String s : result) {
bw.write(s+"\n");
}
bw.flush();
bw.close();
}
}
|
🤔 해설
1. Set<String> set = new HashSet<>();
- 들은 적 없는 사람을 set에 저장
2. if (set.contains(s)) { ... }
- 본적도 없는 사람이 set(들은 적 없는 사람)에 존재 할 경우,
- cnt + 1 && result에 추가
- Set<String> result = new TreeSet<>();
- 사전순 정렬을 위해 Treeset 활용
😮 이 외의 풀이
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.TreeSet;
public class Main {
public static void main(String[] args) throws IOException {
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
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());
Map<String, Boolean> map = new HashMap<>();
for (int i = 0; i < N; i++) {
map.put(br.readLine(), false);
}
while (M-- > 0) {
String s = br.readLine();
if (map.containsKey(s)) {
map.replace(s, true);
}
}
Set<String> set = new TreeSet<>();
for (String s : map.keySet()) {
if (map.get(s)) {
set.add(s);
}
}
StringBuilder sb = new StringBuilder();
sb.append(set.size()).append("\n");
for (String s : set) {
sb.append(s).append("\n");
}
bw.write(sb.toString());
bw.flush();
bw.close();
}
}
|
- HashMap + TreeSet 사용
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
|
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.TreeSet;
public class Main {
public static void main(String[] args) throws IOException {
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
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());
Map<String, Boolean> map = new HashMap<>();
for (int i = 0; i < N; i++) {
map.put(br.readLine(), false);
}
while (M-- > 0) {
String s = br.readLine();
if (map.containsKey(s)) {
map.put(s, true);
}
}
List<String> list = new ArrayList<>();
for (String s : map.keySet()) {
if (map.get(s)) {
list.add(s);
}
}
StringBuilder sb = new StringBuilder();
sb.append(list.size()).append("\n");
Collections.sort(list);
for (String s : list) {
sb.append(s).append("\n");
}
bw.write(sb.toString());
bw.flush();
bw.close();
}
}
|
- HashMap + ArrayList 사용
🔗 소스 코드
GitHub
'Computer > Algorithm_Java' 카테고리의 다른 글
[BaekJoon] 15439번 베라의 패션 문제 풀이 (Success) (0) | 2023.09.06 |
---|---|
[BaekJoon] 1269번 대칭 차집합 문제 풀이 (Success) (0) | 2023.09.05 |
[BaekJoon] 2164번 카드2 문제 풀이 (Success) (0) | 2023.09.03 |
[BaekJoon] 28279번 덱2 문제 풀이 (Success) (0) | 2023.09.01 |
[BaekJoon] 18258번 큐2 문제 풀이 (Success) (0) | 2023.08.31 |