본문 바로가기

개발/알고리즘

프로그래머스 오픈채팅방 C++

문제

programmers.co.kr/learn/courses/30/lessons/42888

 

코드

#include <string>
#include <vector>
#include <map>
#include <sstream>

using namespace std;

vector<pair<string, pair<string, string>>> v;

void split(string s)
{
    stringstream ss(s);
    string temp;
    vector<string> tempString;
   
    while (getline(ss, temp, ' '))
    {
        tempString.push_back(temp);
    }

    if (tempString.size() < 3)
    {
        tempString.push_back("");
    }
    v.push_back({ tempString[0], { tempString[1],  tempString[2] }});
}

vector<string> solution(vector<string> record) {
    vector<string> answer;
    map<string, string> userInfo;
        
    for (int i = 0; i < record.size(); i++)
    {
        split(record[i]);
        if (v[i].first.compare("Leave") == 0)
            continue;

        userInfo[v[i].second.first] = v[i].second.second;
    }
  
    for (int i = 0; i < record.size(); i++)
    {
        if (v[i].first.compare("Change") == 0)
            continue;
      
        auto iter = userInfo.find(v[i].second.first);
        if (iter == userInfo.end())
            continue;

        string s;
        if (v[i].first.compare("Enter") == 0)
        {
            s += iter->second;
            s += "님이 들어왔습니다.";
        }
        else
        {
            s += iter->second;
            s += "님이 나갔습니다.";
        }
        answer.push_back(s);
    }
    
    return answer;
}

결과

userID의 마지막 닉네임만 기억하면된다.

'개발 > 알고리즘' 카테고리의 다른 글

백준 15961 회전초밥 C++  (0) 2021.03.10
백준 1072번 게임 C++  (0) 2021.03.07
백준 7795번 먹을 것인가 먹힐 것인가  (0) 2021.03.05
백준 6236 용돈관리 C++  (0) 2021.03.04
프로그래머스 가사 검색 C++  (0) 2021.03.03