프로그래머스(Programmers)

신고 결과 받기 C++

cosmohoo 2022. 1. 17. 21:37
반응형

문제 설명

 

#include <string>
#include <vector>
#include <iostream>
#include <map>
#include <set>

using namespace std;

vector<int> solution(vector<string> id_list, vector<string> report, int k) {
    vector<int> answer(id_list.size());
    
    map<string, set<string>> reportMap; //key : 신고한 사람, value : key값에게 신고당한 아이들 
    map<string , int> reportCnt; // 신고당한 사람, 횟수
    for (string s: report)
    {
        string tmp;//key와 value 들어갈 string
        string reporter;
        for(char ch : s)
        {
            if(ch == ' ')
            {
                reporter=tmp;
                tmp="";
                continue;
            }
         
            tmp += ch;
        }
        
        if(reportMap[reporter].count(tmp))
        {
            continue;
        }
        
        reportMap[reporter].insert(tmp); 
        reportCnt[tmp]++;


    }
    
    set<string> nameList;//k번 이상 신고당한 이름 
    
    for(string s : id_list )
    {
        if(reportCnt[s] >= k)
        {
            nameList.insert(s);
        }
    }
    
   for (int i = 0; i < id_list.size(); i++) 
   {
       int cnt = 0;
       for (auto r : reportMap[id_list[i]]) 
       {
           if (nameList.find(r) != nameList.end()) 
           { 
               cnt++; 
           } 
       } 
       answer[i] = cnt; 
   }
    
    return answer;
}

 

=> SET과 MAP을 적절히 활용하여 풀어야 하는 문제였습니다. 

=> 누가 몇번신고당했는지 select 하는 법은 알아내었지만, 해당 변수를 answer에 넣는 방법에서 헤매었습니다. 

=> 해당 변수를 answer에 넣을 때의 부분은 다른 블로그를 참조하였습니다. 

 

https://jaimemin.tistory.com/2034

 

[Programmers] 신고 결과 받기

문제 링크입니다: https://programmers.co.kr/learn/courses/30/lessons/92334 코딩테스트 연습 - 신고 결과 받기 문제 설명 신입사원 무지는 게시판 불량 이용자를 신고하고 처리 결과를 메일로 발송하는 시스템

jaimemin.tistory.com

 

위 블로그를 참조하였습니다. 

 

 

 

concept 정리

 

=> 아래의 부분을 잘 기억해 다음 문제에서는 대입해보도록하겠습니다. 

 

반응형