[BaekJoon] 25192번 - 인사성 밝은 곰곰이 [Java][C++]
[BaekJoon] 25192번 - 인사성 밝은 곰곰이 [Java][C++]
1. 문제 풀이
"ENTER" 이후 채팅을 친 유저들의 수를 구하면 되는 문제로 각 유저가 여러 번 채팅을 칠 수 있는데 이를 중복으로 세지 않아야 한다. 집합을 활용하면 해결할 수 있다.
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
25
26
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));
int N = Integer.parseInt(br.readLine());
Set<String> set = new HashSet<>();
int cnt = 0;
for (int i = 0; i < N; i++) {
String input = br.readLine();
if (input.equals("ENTER")) {
cnt += set.size();
set.clear();
} else {
set.add(input);
}
}
cnt += set.size();
System.out.println(cnt);
}
}
2. 풀이 [C++]
unordered_set의 경우 입력의 크기가 커서 성능 문제가 있어 set을 활용했다.
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
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
set<string> st;
int cnt = 0;
for (int i = 0; i < n; i++) {
string s;
cin >> s;
if (s == "ENTER") {
cnt += st.size();
st.clear();
} else {
st.insert(s);
}
}
cnt += st.size();
cout << cnt;
}
This post is licensed under CC BY 4.0 by the author.