algorithm/문제 풀이

99클럽 코테 스터디 16일차 TIL - 프로그래머스 신고 결과 받기

ssoheeh 2024. 4. 16. 21:56

 

 

 

#include <bits/stdc++.h>

using namespace std;

vector<int> solution(vector<string> id_list, vector<string> report, int k) {
    vector<int> answer;
    map<string, set<string>> reportList;
    map<string, int> reportUser;
    set<string> temp;
    for(string id : id_list){
        reportList.insert(pair<string, set<string>>(id, temp));
        reportUser.insert(pair<string, int>(id, 0));
    }
    
    for(string s: report){
        stringstream ss(s);
        string x, y;
        ss >> x >>y;
        set<string> name = reportList.at(x);
        name.insert(y);
        reportList.erase(x);
        reportList.insert(pair<string, set<string>>(x, name));
      
         
    }
    
    for(string name : id_list){
        
        set<string> s = reportList.at(name);
        for (auto it = s.begin(); it != s.end(); it++) {
        int num = reportUser.at(*it);
        reportUser.erase(*it);
        reportUser.insert(pair<string,int>(*it, num+1));
		
	    }
        
            
            
    }
    
    for(string id : id_list){
        int sum = 0;
        set<string> s = reportList.at(id);
        for (auto it = s.begin(); it != s.end(); it++) {
            if(reportUser.at(*it)>=k){
                sum++;
            }
	    }
        
        answer.push_back(sum);
    }
    
    return answer;
}

map에 값 삽입할 때 map.insert(pair<>()) 이용

map에는 중복 키 삽입 불가능하므로 값 바꾸려면 erase 후 insert!

 

 

 

stringstream으로 빈칸 제거

stringstream ss(s);
string x, y;
ss >> x >> y;

 

 

map, set 값 조회 - at(Key)

 

 

set 순회

set<string> s;
for (auto it = s.begin(); it != s.end(); it++) {
	cout<< *it <<'\n';	
}

 

 

 

오늘의 회고

  • c++ 기본 문법을 ide 사용하지 않고도 자유롭게 쓸 수 있도록 연습 많이 하자.
  • 내일(4/17) 학습 예정 : 스프링부트 온라인 강의 듣기