코딩테스트

[백준] 25757번: 임스와 함께하는 미니게임

시제이 2024. 3. 1. 09:29
728x90
반응형

 

간단한 구현 문제이다.

 

 

 

 

 

 

 

 

처음 생각했던 접근방식(해결)

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
#include <iostream>
#include <map>
 
using namespace std;
 
 
int main(){
    ios::sync_with_stdio(false);
    cin.tie(NULL);
 
    int numRequest;
    char game;
    cin >> numRequest >> game;
    int numPlayer;
    int num = 0;
    int numPlay = 0;
    map<stringint> with;
 
    if(game == 'Y') numPlayer = 1;
    else if(game == 'F') numPlayer = 2;
    else if(game == 'O') numPlayer = 3;
    else return 0;
 
    while(numRequest--){
        string player;
        cin >> player;
        if(with.find(player)== with.end()){
            with[player] = 1;
            num++;
        }
        if(num == numPlayer){
            numPlay++;
            num = 0;
        }
    }
    cout << numPlay;
    
    return 0;
}
 
 
cs

 

게임의 종류에 따라 임스를 제외한 필요한 플레이어 수를 정의하고, 맵을 선언하여 이미 플레이 했던 사람이 없다면 사람의 수 num을 증가시키고, 필요한 플레이어 수와 같다면 플레이 할 수 있으므로 numRequest가 끝날 때 까지 조건에 맞게 반복해서 더해주었다.

그러나 계속 플레이어의 중복을 피해서 검색한다는 부분이 비효율적이라는 생각이 들었다. unordered_map을 사용해서 O(1)의 검색속도의 이점을 활용하는 것도 방법이겠지만, 꼭 탐색을 하는 코드가 필요한지 더 고민해보았다.

 

 

 

 

다른 사람의 풀이

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
#include <iostream>
#include <set>
 
using namespace std;
 
int n;
char game;
set<string> s;
 
void input() {
    cin >> n >> game;
    for(int i=0; i<n; i++) {                                                            
        string str;
        cin >> str;
        s.insert(str);
    }
}
 
void solve() {
    if(game == 'Y') {
        cout << s.size();
    }
    else if(game == 'F') {
        cout << s.size()/2;
    }
    else {
        cout << s.size()/3;
    }
}
 
int main() {
    input();
    solve();
cs

 

이 분은 중복을 허용하지 않는 set 자료구조를 활용하여 플레이어들을 삽입하고, 게임의 종류에 따라 나누어주면 간단하게 해결된다.

문제의 핵심적인 논리구조를 분석해서 자료구조를 잘 선택하는 것이 코드의 가독성을 이렇게까지 올려준다는 것을 다시한번 느끼는 계기가 되었다.

 

 

 

 

 

반응형