728x90
๐๋ฌธ์ ๋ณด๋ฌ๊ฐ๊ธฐ
๐จ๐ปํ์ด ๊ณผ์
์ ๋ ฅ ๋ฐ์ดํฐ ๊ฐ์๊ฐ ์ต๋ 10๋ง ๊ฐ์ด๊ธฐ์ ๊ฒ์์ ๋น ๋ฅด๊ฒ ํ๊ธฐ ์ํด, ํด์(Hash)๋ฅผ ์ฌ์ฉํ์ต๋๋ค. ๊ตฌํํ๋ ๋ถ๋ถ์ด ์กฐ๊ธ ๊ธธ ๋ฟ, ๋ฌธ์ ์์ฒด๋ ์ด๋ ต์ง ์์์ต๋๋ค.
- for๋ฌธ์ผ๋ก record๋ฅผ ์ํํ๋ฉด์, <id, UserData> ๊ฐ์ฒด๋ฅผ ์์ฑํ์ฌ ํด์์ ์ ์ฅํฉ๋๋ค.
- ๋ค์ for๋ฌธ์ผ๋ก record๋ฅผ ์ํํ๋ฉฐ, ๊ฐ ๋ช ๋ น์ด(Enter, Leave, Change)์ ํด๋นํ๋ ๊ธฐ๋ฅ์ ์ํํด์ฃผ๋ฉด ๋ฉ๋๋ค.
โ๏ธ์์ค ์ฝ๋ ๋ฐ ๊ฒฐ๊ณผ
#include <string>
#include <vector>
#include <sstream>
#include <unordered_map>
using namespace std;
const int INSTRUCTION = 0;
const int ID = 1;
const int NICKNAME = 2;
unordered_map<string, string> ChatMessages { {"Enter", "๋์ด ๋ค์ด์์ต๋๋ค."}, {"Leave", "๋์ด ๋๊ฐ์ต๋๋ค."} };
class User {
public:
User() { }
User(string id, string nickName) : _id(id), _nickName(nickName) { }
string GetMessage(const string& instruction) { return _nickName + ChatMessages[instruction]; }
void ChangeNickName(const string& newNickName) { _nickName = newNickName; }
string GetId() { return _id; }
string GetNickName() { return _nickName; }
private:
string _id;
string _nickName;
};
vector<string> Split(const string& log) {
vector<string> result;
stringstream tokenizer(log);
string buffer;
while (getline(tokenizer, buffer, ' '))
result.push_back(buffer);
return result;
}
vector<string> solution(vector<string> record) {
vector<string> answer;
unordered_map<string, User> users;
// ์ ์ ๋ฑ๋ก ๋ฐ ๋๋ค์ ๋ณ๊ฒฝ ์์
for (const auto log : record) {
vector<string> contents = Split(log);
auto it = users.find(contents[ID]);
if (contents[INSTRUCTION] == "Leave")
continue;
// ์ฒซ ์ ์
if (it == users.end()) {
users.emplace(contents[ID], User(contents[ID], contents[NICKNAME]));
continue;
}
// ๋๊ฐ๋ค ๋ค์ด์๊ฑฐ๋, ๋๋ค์์ ๋ณ๊ฒฝํ๋ ๊ฒฝ์ฐ
if (contents[INSTRUCTION] == "Enter" or contents[INSTRUCTION] == "Change")
users[contents[ID]].ChangeNickName(contents[NICKNAME]);
}
// ์ถ๋ ฅ ๊ฒฐ๊ณผ๋ฌผ ๋ด๊ธฐ
for (const auto log : record) {
vector<string> informations = Split(log);
if(informations[INSTRUCTION] != "Change")
answer.emplace_back(users[informations[ID]].GetMessage(informations[INSTRUCTION]));
}
return answer;
}
728x90
๋ฐ์ํ
'๐คAlgorithm > Programmers' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Programmers] Lv2. ์ฃผ์๊ฐ๊ฒฉ | C++ (0) | 2023.06.22 |
---|---|
[Programmers] Lv2. ๋ ๋ฐ๋จน๊ธฐ | C++ (0) | 2023.06.22 |
[Programmers] Lv2. [3์ฐจ] n์ง์ ๊ฒ์ | C++ (0) | 2023.06.21 |
[Programmers] Lv2. ๋ ๋งต๊ฒ | C++ (0) | 2023.06.21 |
[Programmers] Lv2. k์ง์์์ ์์ ๊ฐ์ ๊ตฌํ๊ธฐ | C++ (0) | 2023.06.21 |