Coding Test - cpp/String
[프로그래머스 lv 2 ] - 오픈채팅방
에드윈H
2021. 9. 8. 23:31
https://programmers.co.kr/learn/courses/30/lessons/42888
#include <string>
#include <vector>
#include <unordered_map>
#include <queue>
using namespace std;
string strTok(string& str,int& i)
{
string result;
for( ;i<str.size();i++)
{
if(str[i]==' ')
{
break;
}
result+=str[i];
}
return result;
}
vector<string> solution(vector<string> record) {
vector<string> answer;
unordered_map<string,string> m; //uid,nick
queue<pair<char,string>> q; //명령어 저장
//전체 명령 파악하기
for(auto a: record)
{
string curRecord=a; //현재 기록 (ex : Enter인지, Leave인지 등등..)
string uid;
string nick;
string curOrder;
int i=0;
if(curRecord[0]=='E')
{
i=6;
uid=strTok(curRecord,i);
i++;
nick=strTok(curRecord,i);
q.push( pair<char,string>('E',uid) );
}else if(curRecord[0]=='L')
{
i=6;
uid=strTok(curRecord,i);
curOrder+=uid;
q.push( pair<char,string>('L',uid) );
nick = m[uid];
}else if(curRecord[0]=='C')
{
curOrder+="C ";
i=7;
uid=strTok(curRecord,i);
i++;
nick=strTok(curRecord,i);
}
//최종 닉네임 map
m[uid]=nick;
}
//들어왔던 명령 순서대로 출력 저장하기
while( !q.empty() )
{
char order = q.front().first;
string id = q.front().second;
q.pop();
string result;
if( order == 'E' )
{
result = m[id] + "님이 들어왔습니다.";
answer.push_back( result );
}if( order == 'L' )
{
result = m[id] + "님이 나갔습니다.";
answer.push_back( result );
}
}
return answer;
}