[BaekJoon] 14425번 - 문자열 집합 [Java][C++]
[BaekJoon] 14425번 - 문자열 집합 [Java][C++]
1. 문제 풀이
집합 $S$ 에 대해 문자열이 집합 $S$ 에 포함됐는지 여부를 구하는 문제로 집합에 포함됐는지 확인할 수 있는 함수나 메서드를 활용하면 간단하게 해결할 수 있다.
2. 코드
1. 풀이 [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
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());
Set<String> set = new HashSet<>();
while (n-- > 0) {
set.add(br.readLine());
}
int cnt = 0;
while (m-- > 0) {
if (set.contains(br.readLine())) cnt++;
}
System.out.println(cnt);
}
}
2. 풀이 [C++]
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
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int n, m;
cin >> n >> m;
unordered_set<string> st;
while (n--) {
string s;
cin >> s;
st.insert(s);
}
int cnt = 0;
while (m--) {
string s;
cin >> s;
if (st.count(s)) cnt++;
}
cout << cnt;
}
This post is licensed under CC BY 4.0 by the author.