https://programmers.co.kr/learn/courses/30/lessons/42888
#include <string>
#include <vector>
#include <map>
#include <algorithm>
using namespace std;
vector<string> strtok(string str, char delim = ' '){ // strtok
vector<string> ret;
int prev=0;
for(int i=0;i<str.size();i++){
if(str[i]==delim){
ret.push_back(str.substr(prev,i-prev));
prev=i+1;
}
}
if(str.size()!=prev)
ret.push_back(str.substr(prev,str.size()-prev));
return ret;
}
vector<string> solution(vector<string> record) {
vector<string> answer;
map<string, string> id;
for(int i= 0;i<record.size();i++){
vector<string> temp = strtok(record[i]);
if(temp[0] != "Leave"){
if(id.insert(make_pair(temp[1], temp[2])).second == false)
id[temp[1]] = temp[2];
}
}
for(int i=0;i<record.size();i++){
vector<string> temp = strtok(record[i]);
string name = id[temp[1]];
if(temp[0] == "Enter"){
answer.push_back(name+"님이 들어왔습니다.");
}else if(temp[0] == "Leave"){
answer.push_back(name+"님이 나갔습니다.");
}
}
return answer;
}
#include <string>
#include <vector>
#include <sstream>
#include <iostream>
#include <map>
using namespace std;
vector<string> solution(vector<string> record) {
vector<string> answer;
string command;
string ID;
string uid;
map<string,string> m;
for(string input:record)
{
stringstream ss(input);
ss>>command;
ss>>uid;
if(command=="Enter" || command=="Change")
{
ss>>ID;
m[uid]=ID;
}
}
for(string input:record)
{
stringstream ss(input);
ss>>command;
ss>>uid;
if(command=="Enter")
{
ID=(m.find(uid)->second);
string temp = ID+"님이 들어왔습니다.";
answer.push_back(temp);
}
else if(command=="Leave")
{
ID=(m.find(uid)->second);
string temp = ID+"님이 나갔습니다.";
answer.push_back(temp);
}
}
return answer;
}