백준 25192 인사성 밝은 곰곰이 / C++

이유참치·2025년 12월 15일

백준

목록 보기
191/249

문제 : 25192

풀이 point

입장한 후 채팅한 사람들의 수를 센다. 중복일 경우에는 곰곰티콘이 사용된 것이 아니므로 세지 않는다.

풀이 방법

set, map을 활용하여 풀 수 있다.

코드

//백준 25192, 인사성 밝은 곰곰이

/*
#include <iostream>
#include <unordered_map>

std::unordered_map<std::string, int> map;

int main(){
    std::ios_base :: sync_with_stdio(false);
    std::cin.tie(NULL);
    std::cout.tie(NULL);

    int N;
    std::cin >> N;
    std::string s;

    int gom{0};
    for(int i{0}; i<N; ++i){
        std::cin >> s;
        if(s == "ENTER"){
            map.clear();
        }
        else{
            if(map.find(s) != map.end()) continue;
            map[s] = 1;
            ++gom;
        }
    }

    std::cout << gom;

    return 0;
}
*/

//
#include <iostream>
#include <set>

std::set<std::string> set;

int main(){

    int N;
    std::string s;

    std::cin >> N;
    
    int gom{0};
    for(int i{0}; i<N; ++i){
        std::cin >> s;
        if(s == "ENTER"){
            gom += set.size();
            set.clear();
            continue;
        }
        set.insert(s); 
    }
    
    gom += set.size();

    std::cout << gom;

    return 0;
}

사족

unordered 즉, hashmap을 활용하면 속도가 확연히 떨어진다. 빠른 입력이 없으면 시간초과가 일어남... N의 범위가 10만이라 초기 용량이 중요한 hashmap의 사용이 많은 비용을 소모하는 듯... 또는 충돌 문제

profile
임아리 - 대학생

0개의 댓글