본문 바로가기
Problem Solving

[c++][프로그래머스] 신고 결과 받기

by wadekang 2022. 1. 20.

프로그래머스 신고 결과 받기 [2022 KAKAO BLIND RECRUITMENT]
https://programmers.co.kr/learn/courses/30/lessons/92334
 

코딩테스트 연습 - 신고 결과 받기

문제 설명 신입사원 무지는 게시판 불량 이용자를 신고하고 처리 결과를 메일로 발송하는 시스템을 개발하려 합니다. 무지가 개발하려는 시스템은 다음과 같습니다. 각 유저는 한 번에 한 명의

programmers.co.kr

 

문제 풀이

  • 문제를 해결하기 위해 두 개의 unordered_map 과 stringstream 을 사용했습니다.
    • 첫 번째 맵은 unordered_map<string, int> 형태로 각 멤버의 index 를 저장하기 위해 사용
    • 두 번째 맵은 unordered_map<string, set<string>> 형태로 각 멤버를 신고한 멤버들을 set에 저장
    • "한 유저를 여러 번 신고할 수도 있지만, 동일한 유저에 대한 신고 횟수는 1회로 처리됩니다." 라는 제약조건 때문에 vector 대신 set을 사용
    • stringstream을 사용해 문자열 parsing
  • 먼저 report의 정보들을 parsing 해 신고 정보를 저장하는 맵에 전부 담고, 신고 정보 맵을 돌면서 set size가 k 이상이면 set 안의 멤버들의 count를 증가시키는 방식

코드

#include <string>
#include <vector>
#include <unordered_map>
#include <set>
#include <sstream>

using namespace std;

vector<int> solution(vector<string> id_list, vector<string> report, int k) {
    vector<int> answer(id_list.size(), 0);
    
    unordered_map<string, int> idx_map;
    for(int i=0; i<id_list.size(); ++i) idx_map[id_list[i]] = i; // 멤버 index 저장
    
    unordered_map<string, set<string>> report_map;
    stringstream ss;
    for(auto rep: report) {
        ss.str(rep);
        string first, second; ss >> first >> second; // 문자열 parsing
        
        /* 
        	신고 정보 저장
        	first가 second를 신고 -> second의 set에는 second를 신고한 사람들
        */
        report_map[second].insert(first);
        
        ss.clear();
    }
    
    for(auto it: report_map) { // it.second = set<string>
        if(it.second.size() >= k) { // 어떤 멤버를 신고한 사람이 k명 이상이면
            for(auto set_it: it.second) { // 신고한 사람들의
                int idx = idx_map[set_it];                
                answer[idx]++; // count 증가 (메일 전송)
            }            
        }
    }    
    
    return answer;
}

실행 결과

 

댓글