문제 :
https://school.programmers.co.kr/learn/courses/30/lessons/42888
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
풀이 :
뭔가 복잡해 보였지만 정리하면 구현할 내용이 단순했다.
- MAP을 통해 유저 아이디와 닉네임을 맵핑: change나 enter 행동마다 갱신
- enter이나 leave 행동시에는 해당 동작과 유저 아이디를 저장하여 순서대로 저장.
- 최종 맵핑된 닉네임을 유저아이디로 불러와 문자열을 저장하면 된다.
#include <iostream>
#include <string>
#include <vector>
#include <map>
using namespace std;
map <string, string> dic;
vector <pair<int,string>> mes;
vector<string> solution(vector<string> record) {
vector<string> answer;
for(int i = 0; i < record.size(); i ++){
string op = "";
string id = "";
string name = "";
int num = 0;
for(int j = 0; j < record[i].size(); j ++){
if(record[i][j] == ' ')
{
num++;
continue;
}
if(num == 0)
op += record[i][j];
else if(num == 1)
id += record[i][j];
else if(num == 2)
name += record[i][j];
}
if(op == "Enter")
{
mes.push_back({0,id}); // 0은 입장
dic[id] = name;
}
else if(op == "Leave")
mes.push_back({1,id});
else if(op == "Change")
dic[id] = name;
}
for(int i = 0; i < mes.size(); i++){
string temp;
if(mes[i].first == 0){
temp = dic[mes[i].second]+"님이 들어왔습니다.";
}
else if(mes[i].first == 1){
temp = dic[mes[i].second]+"님이 나갔습니다.";
}
answer.push_back(temp);
}
return answer;
}
PS.
문자열 관련 문제를 별로 안풀어서 까다로웠는데 역시 C++...상상한 방법이 그대로 되서 다행이었다.
'알고리즘 문제 풀이 > 프로그래머스' 카테고리의 다른 글
[프로그래머스] LV2. 스킬트리 (C++) (0) | 2024.02.15 |
---|---|
[프로그래머스] LV2. 무인도 여행 (C++) (0) | 2024.02.15 |
[프로그래머스] LV3. 가장 먼 노드 (C++) (1) | 2024.02.15 |
문제 :
https://school.programmers.co.kr/learn/courses/30/lessons/42888
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
풀이 :
뭔가 복잡해 보였지만 정리하면 구현할 내용이 단순했다.
- MAP을 통해 유저 아이디와 닉네임을 맵핑: change나 enter 행동마다 갱신
- enter이나 leave 행동시에는 해당 동작과 유저 아이디를 저장하여 순서대로 저장.
- 최종 맵핑된 닉네임을 유저아이디로 불러와 문자열을 저장하면 된다.
#include <iostream>
#include <string>
#include <vector>
#include <map>
using namespace std;
map <string, string> dic;
vector <pair<int,string>> mes;
vector<string> solution(vector<string> record) {
vector<string> answer;
for(int i = 0; i < record.size(); i ++){
string op = "";
string id = "";
string name = "";
int num = 0;
for(int j = 0; j < record[i].size(); j ++){
if(record[i][j] == ' ')
{
num++;
continue;
}
if(num == 0)
op += record[i][j];
else if(num == 1)
id += record[i][j];
else if(num == 2)
name += record[i][j];
}
if(op == "Enter")
{
mes.push_back({0,id}); // 0은 입장
dic[id] = name;
}
else if(op == "Leave")
mes.push_back({1,id});
else if(op == "Change")
dic[id] = name;
}
for(int i = 0; i < mes.size(); i++){
string temp;
if(mes[i].first == 0){
temp = dic[mes[i].second]+"님이 들어왔습니다.";
}
else if(mes[i].first == 1){
temp = dic[mes[i].second]+"님이 나갔습니다.";
}
answer.push_back(temp);
}
return answer;
}
PS.
문자열 관련 문제를 별로 안풀어서 까다로웠는데 역시 C++...상상한 방법이 그대로 되서 다행이었다.
'알고리즘 문제 풀이 > 프로그래머스' 카테고리의 다른 글
[프로그래머스] LV2. 스킬트리 (C++) (0) | 2024.02.15 |
---|---|
[프로그래머스] LV2. 무인도 여행 (C++) (0) | 2024.02.15 |
[프로그래머스] LV3. 가장 먼 노드 (C++) (1) | 2024.02.15 |