https://programmers.co.kr/learn/courses/30/lessons/92334?language=cpp ref: https://wadekang.tistory.com/6
Ripasso C++
- unordered_map<T, T>
- stringstream
- Ranged-based for loops, auto
for (auto it: v) cout << it;
for (auto& it: v) it *= 2; // si possono usare puntatori
for (const auto& it: v) // se il costo della copia è alto, usare const pointerSoluzione
- Mappare ogni utente a un indice per poterlo identificare.
- Costruire una mappa “chiave: utente segnalato, valore: insieme degli utenti che lo hanno segnalato”.
- Usando le informazioni del punto 2, se k o più persone hanno segnalato qualcuno, aggiornare le informazioni sull’utente bannato in base agli indici del punto 1.
Codice
#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; // assegnare un indice a ogni utente for (int i = 0; i < id_list.size(); i++) idx_map[id_list[i]] = i;
unordered_map<string, set<string>> report_map; // memorizzare l'insieme dei segnalatori per utente segnalato stringstream ss;
for (auto rep: report) { ss.str(rep); string first, second; ss >> first >> second; // separare per spazio usando l'output di stringstream
report_map[second].insert(first); // memorizzare first (il segnalatore) nell'insieme di second ss.clear(); }
for (auto it: report_map) { if (it.second.size() >= k) { // per gli utenti segnalati da k o più persone for (auto set_it: it.second) { // trovare ogni segnalatore tramite indice e notificarlo int idx = idx_map[set_it]; answer[idx]++; } } } return answer;}