프로그래머스 92334 신고 결과 받기

1 개요[ | ]

프로그래머스 92334 신고 결과 받기

2 C++[ | ]

#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <algorithm>
using namespace std;

vector<int> solution(vector<string> id_list, vector<string> report, int k) {
    map<string,set<string>> userBads;
    for(string user: id_list) {
        userBads[user] = set<string>{};
    }
    for(string r: report) {
        int idx=0;
        while(r[idx] != ' ') idx++;
        string user = r.substr(0,idx);
        string bad = r.substr(idx+1);
        userBads[user].insert(bad);
    }
    map<string,int> badCount;
    for(auto& ub: userBads) {
        for(auto& bad: ub.second) {
            badCount[bad]++;
        }
    }
    vector<string> banned;
    for(auto& it: badCount) {
        if(it.second >= k) banned.push_back(it.first);
    }
    vector<int> result;
    for(string user: id_list) {
        int mail = 0;
        for(auto& bad: userBads[user]) {
            if(find(banned.begin(), banned.end(), bad) != banned.end()) {
                mail++;
            }
        }
        result.push_back(mail);
    }
    return result;
}
#include <iostream>
#include <vector>
#include <map>
#include <algorithm>
#include <sstream>
using namespace std;

vector<int> solution(vector<string> id_list, vector<string> report, int k) {
    sort(report.begin(), report.end());
    report.erase(unique(report.begin(), report.end()), report.end());

    const int n = id_list.size();
    map<string, int> user;
    for (int i=0; i<n; i++) {
        user[id_list[i]] = i;
    }

    vector<pair<int, int>> v;
    for (const auto& s : report) {
        stringstream in(s);
        string a, b; in >> a >> b;
        v.push_back({ user[a], user[b] });
    }

    vector<int> counter(n), result(n);
    for (const auto& [a, b] : v) {
        counter[b]++;
    }
    for (const auto& [a, b] : v) {
        if (counter[b] >= k) result[a]++;
    }
    return result;
}
문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}